1
14
15 package com.liferay.portal.security.ldap;
16
17 import com.liferay.portal.kernel.log.Log;
18 import com.liferay.portal.kernel.log.LogFactoryUtil;
19 import com.liferay.portal.kernel.util.StringBundler;
20 import com.liferay.portal.kernel.util.StringPool;
21 import com.liferay.portal.kernel.util.Validator;
22 import com.liferay.portal.model.Contact;
23 import com.liferay.portal.model.User;
24 import com.liferay.portal.service.UserLocalServiceUtil;
25 import com.liferay.portal.util.PropsValues;
26 import com.liferay.util.ldap.Modifications;
27
28 import java.util.Properties;
29
30 import javax.naming.Binding;
31 import javax.naming.CompositeName;
32 import javax.naming.Name;
33 import javax.naming.directory.ModificationItem;
34 import javax.naming.ldap.LdapContext;
35
36
43 public class PortalLDAPExporter {
44
45 public static void exportToLDAP(Contact contact) throws Exception {
46 long companyId = contact.getCompanyId();
47
48 if (!LDAPSettingsUtil.isAuthEnabled(companyId) ||
49 !LDAPSettingsUtil.isExportEnabled(companyId)) {
50
51 return;
52 }
53
54 User user = UserLocalServiceUtil.getUserByContactId(
55 contact.getContactId());
56
57 long ldapServerId = PortalLDAPUtil.getLdapServerId(
58 companyId, user.getScreenName());
59
60 LdapContext ldapContext = PortalLDAPUtil.getContext(
61 ldapServerId, companyId);
62
63 try {
64 if (ldapContext == null) {
65 return;
66 }
67
68 Properties userMappings = LDAPSettingsUtil.getUserMappings(
69 ldapServerId, companyId);
70 Binding binding = PortalLDAPUtil.getUser(
71 ldapServerId, contact.getCompanyId(), user.getScreenName());
72 Name name = new CompositeName();
73
74 if (binding == null) {
75
76
78 _getDNName(
79 ldapServerId, companyId, user, userMappings, name);
80
81 LDAPUser ldapUser = (LDAPUser)Class.forName(
82 PropsValues.LDAP_USER_IMPL).newInstance();
83
84 ldapUser.setUser(user, ldapServerId);
85
86 ldapContext.bind(name, ldapUser);
87 }
88 else {
89
90
92 name.add(
93 PortalLDAPUtil.getNameInNamespace(
94 ldapServerId, companyId, binding));
95
96 Modifications modifications = Modifications.getInstance();
97
98 modifications.addItem(
99 userMappings.getProperty("firstName"),
100 contact.getFirstName());
101
102 String middleNameMapping = userMappings.getProperty(
103 "middleName");
104
105 if (Validator.isNotNull(middleNameMapping)) {
106 modifications.addItem(
107 middleNameMapping, contact.getMiddleName());
108 }
109
110 modifications.addItem(
111 userMappings.getProperty("lastName"),
112 contact.getLastName());
113
114 String fullNameMapping = userMappings.getProperty("fullName");
115
116 if (Validator.isNotNull(fullNameMapping)) {
117 modifications.addItem(
118 fullNameMapping, contact.getFullName());
119 }
120
121 String jobTitleMapping = userMappings.getProperty("jobTitle");
122
123 if (Validator.isNotNull(jobTitleMapping)) {
124 modifications.addItem(
125 jobTitleMapping, contact.getJobTitle());
126 }
127
128 ModificationItem[] modificationItems = modifications.getItems();
129
130 ldapContext.modifyAttributes(name, modificationItems);
131 }
132 }
133 catch (Exception e) {
134 throw e;
135 }
136 finally {
137 if (ldapContext != null) {
138 ldapContext.close();
139 }
140 }
141 }
142
143 public static void exportToLDAP(User user) throws Exception {
144 long companyId = user.getCompanyId();
145
146 if (!LDAPSettingsUtil.isAuthEnabled(companyId) ||
147 !LDAPSettingsUtil.isExportEnabled(companyId)) {
148
149 return;
150 }
151
152 long ldapServerId = PortalLDAPUtil.getLdapServerId(
153 companyId, user.getScreenName());
154
155 LdapContext ldapContext = PortalLDAPUtil.getContext(
156 ldapServerId, companyId);
157
158 try {
159 if (ldapContext == null) {
160 return;
161 }
162
163 Properties userMappings = LDAPSettingsUtil.getUserMappings(
164 ldapServerId, companyId);
165 Binding binding = PortalLDAPUtil.getUser(
166 ldapServerId, user.getCompanyId(), user.getScreenName());
167 Name name = new CompositeName();
168
169 if (binding == null) {
170
171
173 _getDNName(
174 ldapServerId, companyId, user, userMappings, name);
175
176 LDAPUser ldapUser = (LDAPUser) Class.forName(
177 PropsValues.LDAP_USER_IMPL).newInstance();
178
179 ldapUser.setUser(user, ldapServerId);
180
181 ldapContext.bind(name, ldapUser);
182
183 binding = PortalLDAPUtil.getUser(
184 ldapServerId, user.getCompanyId(), user.getScreenName());
185
186 name = new CompositeName();
187 }
188
189
191 name.add(
192 PortalLDAPUtil.getNameInNamespace(
193 ldapServerId, companyId, binding));
194
195 Modifications modifications = Modifications.getInstance();
196
197 modifications.addItem(
198 userMappings.getProperty("firstName"), user.getFirstName());
199
200 String middleNameMapping = userMappings.getProperty(
201 "middleName");
202
203 if (Validator.isNotNull(middleNameMapping)) {
204 modifications.addItem(middleNameMapping, user.getMiddleName());
205 }
206
207 modifications.addItem(
208 userMappings.getProperty("lastName"), user.getLastName());
209
210 String fullNameMapping = userMappings.getProperty("fullName");
211
212 if (Validator.isNotNull(fullNameMapping)) {
213 modifications.addItem(fullNameMapping, user.getFullName());
214 }
215
216 if (user.isPasswordModified() &&
217 Validator.isNotNull(user.getPasswordUnencrypted())) {
218
219 modifications.addItem(
220 userMappings.getProperty("password"),
221 user.getPasswordUnencrypted());
222 }
223
224 if (Validator.isNotNull(user.getEmailAddress())) {
225 modifications.addItem(
226 userMappings.getProperty("emailAddress"),
227 user.getEmailAddress());
228 }
229
230 String jobTitleMapping = userMappings.getProperty("jobTitle");
231
232 if (Validator.isNotNull(jobTitleMapping)) {
233 modifications.addItem(jobTitleMapping, user.getJobTitle());
234 }
235
236 ModificationItem[] modificationItems = modifications.getItems();
237
238 ldapContext.modifyAttributes(name, modificationItems);
239 }
240 catch (Exception e) {
241 _log.error(e, e);
242 }
243 finally {
244 if (ldapContext != null) {
245 ldapContext.close();
246 }
247 }
248 }
249
250 private static void _getDNName(
251 long ldapServerId, long companyId, User user,
252 Properties userMappings, Name name)
253 throws Exception {
254
255
257 StringBundler sb = new StringBundler(5);
258
259 sb.append(userMappings.getProperty("screenName"));
260 sb.append(StringPool.EQUAL);
261 sb.append(user.getScreenName());
262 sb.append(StringPool.COMMA);
263 sb.append(PortalLDAPUtil.getUsersDN(ldapServerId, companyId));
264
265 name.add(sb.toString());
266 }
267
268 private static Log _log = LogFactoryUtil.getLog(PortalLDAPExporter.class);
269
270 }