1
22
23 package com.liferay.portal.security.ldap;
24
25 import com.liferay.portal.kernel.util.Validator;
26 import com.liferay.portal.model.User;
27 import com.liferay.portal.util.PropsKeys;
28 import com.liferay.portal.util.PropsUtil;
29 import com.liferay.util.ldap.DummyDirContext;
30
31 import java.util.Properties;
32
33 import javax.naming.Name;
34 import javax.naming.NameNotFoundException;
35 import javax.naming.NamingException;
36 import javax.naming.directory.Attribute;
37 import javax.naming.directory.Attributes;
38 import javax.naming.directory.BasicAttribute;
39 import javax.naming.directory.BasicAttributes;
40
41
48 public class LDAPUser extends DummyDirContext {
49
50 public LDAPUser() {
51 super();
52 }
53
54 public User getUser() {
55 return _user;
56 }
57
58 public void setUser(User user) throws Exception {
59 _user = user;
60
61 Properties userMappings = PortalLDAPUtil.getUserMappings(
62 _user.getCompanyId());
63
64 _attrs = new BasicAttributes(true);
65
66 Attribute objectClass = new BasicAttribute("objectclass");
67
68 String[] defaultObjectClasses = PropsUtil.getArray(
69 PropsKeys.LDAP_USER_DEFAULT_OBJECT_CLASSES);
70
71 for (int i = 0; i < defaultObjectClasses.length; i++) {
72 objectClass.add(defaultObjectClasses[i]);
73 }
74
75 _attrs.put(objectClass);
76
77 _attrs.put(userMappings.getProperty("firstName"), _user.getFirstName());
78 _attrs.put(userMappings.getProperty("lastName"), _user.getLastName());
79
80 if (Validator.isNotNull(user.getPasswordUnencrypted())) {
81 _attrs.put(
82 userMappings.getProperty("password"),
83 _user.getPasswordUnencrypted());
84 }
85
86 _attrs.put(
87 userMappings.getProperty("emailAddress"), _user.getEmailAddress());
88 }
89
90 public Attributes getAttributes() {
91 return _attrs;
92 }
93
94 public Attributes getAttributes(String name) throws NamingException {
95 if (Validator.isNotNull(name)) {
96 throw new NameNotFoundException();
97 }
98
99 return (Attributes)_attrs.clone();
100 }
101
102 public Attributes getAttributes(Name name) throws NamingException {
103 return getAttributes(name.toString());
104 }
105
106 public Attributes getAttributes(String name, String[] ids)
107 throws NamingException {
108
109 if (Validator.isNotNull(name)) {
110 throw new NameNotFoundException();
111 }
112
113 Attributes attrs = new BasicAttributes(true);
114
115 for (int i = 0; i < ids.length; i++) {
116 Attribute attr = _attrs.get(ids[i]);
117
118 if (attr != null) {
119 attrs.put(attr);
120 }
121 }
122
123 return attrs;
124 }
125
126 public Attributes getAttributes(Name name, String[] ids)
127 throws NamingException {
128
129 return getAttributes(name.toString(), ids);
130 }
131
132 private User _user;
133 private Attributes _attrs;
134
135 }