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