4 |
4 |
import java.util.Map;
|
5 |
5 |
|
6 |
6 |
import javax.naming.Name;
|
|
7 |
import javax.naming.directory.Attributes;
|
|
8 |
import javax.naming.directory.BasicAttribute;
|
|
9 |
import javax.naming.directory.BasicAttributes;
|
7 |
10 |
|
8 |
11 |
import my.com.upass.MinimalConstants;
|
9 |
12 |
import my.com.upass.UPassException;
|
10 |
13 |
|
11 |
14 |
import org.apache.commons.logging.Log;
|
12 |
15 |
import org.apache.commons.logging.LogFactory;
|
13 |
|
import org.springframework.ldap.AuthenticationException;
|
14 |
16 |
import org.springframework.ldap.NameNotFoundException;
|
15 |
17 |
import org.springframework.ldap.NamingException;
|
16 |
18 |
import org.springframework.ldap.core.DirContextOperations;
|
17 |
19 |
import org.springframework.ldap.core.DistinguishedName;
|
18 |
20 |
import org.springframework.ldap.core.LdapTemplate;
|
19 |
21 |
|
20 |
|
public class MaybankLdapDAOImpl implements MaybankLdapDAO {
|
|
22 |
public class MaybankLdapDAOImpl implements MaybankLdapDAO{
|
21 |
23 |
|
22 |
24 |
private Log logger = LogFactory.getLog(MaybankLdapDAOImpl.class);
|
23 |
25 |
private LdapTemplate ldapTemplate = null;
|
24 |
26 |
private String base;
|
25 |
27 |
private String usernameAttrName;
|
26 |
|
|
|
28 |
|
27 |
29 |
public boolean isUserExist(String mbbuserid) {
|
28 |
30 |
Object person = null;
|
29 |
|
|
|
31 |
|
30 |
32 |
try {
|
31 |
33 |
person = ldapTemplate.lookup(buildDn(mbbuserid));
|
32 |
34 |
} catch (NameNotFoundException e) {
|
33 |
|
// not found
|
|
35 |
//not found
|
34 |
36 |
}
|
35 |
|
|
36 |
|
if (person != null) {
|
|
37 |
|
|
38 |
if(person != null){
|
37 |
39 |
return true;
|
38 |
40 |
}
|
39 |
|
|
|
41 |
|
40 |
42 |
return false;
|
41 |
43 |
}
|
42 |
|
|
|
44 |
|
43 |
45 |
public boolean authenticate(String mbbuserid, String password) throws UPassException {
|
44 |
46 |
boolean result = false;
|
45 |
47 |
try {
|
... | ... | |
56 |
58 |
return result;
|
57 |
59 |
}
|
58 |
60 |
|
59 |
|
public void updateUser(String mbbuserid, Map/* <String, String> */attributesMap) throws UPassException {
|
|
61 |
/**
|
|
62 |
* To be used in Load Test ONLY!
|
|
63 |
*/
|
|
64 |
public int createUser(String mbbuserid, String password) {
|
|
65 |
Name dn = buildDn(mbbuserid);
|
|
66 |
try {
|
|
67 |
ldapTemplate.bind(dn, null, buildAttributes(mbbuserid, password));
|
|
68 |
} catch (org.springframework.ldap.NamingException e) {
|
|
69 |
logger.warn("LDAP Error code:"+LdapStatusCodeParser.getCode(e.getExplanation()) + " - " + e.getExplanation());
|
|
70 |
return 1;
|
|
71 |
}
|
|
72 |
|
|
73 |
return 0;
|
|
74 |
}
|
|
75 |
|
|
76 |
/**
|
|
77 |
* To be used in Load Test ONLY!
|
|
78 |
*/
|
|
79 |
public int deleteUser(String mbbuserid) {
|
|
80 |
Name dn = buildDn(mbbuserid);
|
|
81 |
try {
|
|
82 |
ldapTemplate.unbind(dn);
|
|
83 |
} catch (org.springframework.ldap.NamingException e) {
|
|
84 |
logger.warn("LDAP Error code:"+LdapStatusCodeParser.getCode(e.getExplanation()) + " - " + e.getExplanation());
|
|
85 |
return 1;
|
|
86 |
}
|
|
87 |
|
|
88 |
return 0;
|
|
89 |
}
|
|
90 |
|
|
91 |
/**
|
|
92 |
* To be used in Load Test ONLY!
|
|
93 |
*/
|
|
94 |
private Attributes buildAttributes(String mbbuserid, String password) {
|
|
95 |
Attributes attrs = new BasicAttributes();
|
|
96 |
BasicAttribute basicAttribute = new BasicAttribute("objectclass");
|
|
97 |
basicAttribute.add("top");
|
|
98 |
basicAttribute.add("internetbanking");
|
|
99 |
attrs.put(basicAttribute);
|
|
100 |
attrs.put("ibpan", "0"+mbbuserid);
|
|
101 |
attrs.put("ibpan2", "1"+mbbuserid);
|
|
102 |
attrs.put("userPassword", password);
|
|
103 |
return attrs;
|
|
104 |
}
|
|
105 |
|
|
106 |
public void updateUser(String mbbuserid, Map/*<String, String>*/ attributesMap) throws UPassException{
|
60 |
107 |
Name dn = buildDn(mbbuserid);
|
61 |
108 |
DirContextOperations context = ldapTemplate.lookupContext(dn);
|
62 |
109 |
mapToContext(attributesMap, context);
|
63 |
|
|
|
110 |
|
64 |
111 |
try {
|
65 |
112 |
ldapTemplate.modifyAttributes(context);
|
66 |
|
|
67 |
113 |
} catch (NamingException e) {
|
68 |
|
logger.warn("LDAP Error code:" + LdapStatusCodeParser.getCode(e.getExplanation()) + " - "
|
69 |
|
+ e.getExplanation());
|
|
114 |
logger.warn("LDAP Error code:"+LdapStatusCodeParser.getCode(e.getExplanation()) + " - " + e.getExplanation());
|
70 |
115 |
throw new UPassException(MinimalConstants.ERR_LDAP);
|
71 |
116 |
}
|
72 |
117 |
}
|
73 |
|
|
74 |
|
protected void mapToContext(Map/* <String, String> */attributesMap, DirContextOperations context) {
|
|
118 |
|
|
119 |
protected void mapToContext(Map/*<String, String>*/ attributesMap, DirContextOperations context) {
|
75 |
120 |
Iterator it = attributesMap.entrySet().iterator();
|
76 |
|
while (it.hasNext()) {
|
77 |
|
Map.Entry pairs = (Map.Entry) it.next();
|
78 |
|
context.setAttributeValue((String) pairs.getKey(), pairs.getValue());
|
79 |
|
it.remove();
|
80 |
|
}
|
|
121 |
while (it.hasNext()) {
|
|
122 |
Map.Entry pairs = (Map.Entry)it.next();
|
|
123 |
context.setAttributeValue((String)pairs.getKey(), pairs.getValue());
|
|
124 |
it.remove();
|
|
125 |
}
|
81 |
126 |
}
|
82 |
|
|
|
127 |
|
83 |
128 |
protected Name buildDn(String mbbuserid) {
|
84 |
129 |
DistinguishedName dn = new DistinguishedName(base);
|
85 |
130 |
dn.add("mbbuserid", mbbuserid);
|