001
014
015 package com.liferay.portal.security.ldap;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.model.Contact;
020 import com.liferay.portal.model.User;
021 import com.liferay.portal.model.UserGroup;
022 import com.liferay.portal.service.UserGroupLocalServiceUtil;
023 import com.liferay.portal.service.UserLocalServiceUtil;
024
025 import java.io.Serializable;
026
027 import java.util.Map;
028 import java.util.Properties;
029
030 import javax.naming.Binding;
031 import javax.naming.CompositeName;
032 import javax.naming.Name;
033 import javax.naming.directory.Attributes;
034 import javax.naming.directory.ModificationItem;
035 import javax.naming.ldap.LdapContext;
036
037
042 public class PortalLDAPExporterImpl implements PortalLDAPExporter {
043
044 public void exportToLDAP(
045 Contact contact, Map<String, Serializable> contactExpandoAttributes)
046 throws Exception {
047
048 long companyId = contact.getCompanyId();
049
050 if (!LDAPSettingsUtil.isAuthEnabled(companyId) ||
051 !LDAPSettingsUtil.isExportEnabled(companyId)) {
052
053 return;
054 }
055
056 User user = UserLocalServiceUtil.getUserByContactId(
057 contact.getContactId());
058
059 long ldapServerId = PortalLDAPUtil.getLdapServerId(
060 companyId, user.getScreenName());
061
062 LdapContext ldapContext = PortalLDAPUtil.getContext(
063 ldapServerId, companyId);
064
065 try {
066 if (ldapContext == null) {
067 return;
068 }
069
070 Properties contactMappings = LDAPSettingsUtil.getContactMappings(
071 ldapServerId, companyId);
072 Properties contactExpandoMappings =
073 LDAPSettingsUtil.getContactExpandoMappings(
074 ldapServerId, companyId);
075
076 Binding binding = PortalLDAPUtil.getUser(
077 ldapServerId, contact.getCompanyId(), user.getScreenName());
078
079 if (binding == null) {
080 Properties userMappings = LDAPSettingsUtil.getUserMappings(
081 ldapServerId, companyId);
082
083 binding = addUser(
084 ldapServerId, ldapContext, user, userMappings);
085 }
086
087 Name name = new CompositeName();
088
089 name.add(
090 PortalLDAPUtil.getNameInNamespace(
091 ldapServerId, companyId, binding));
092
093 Modifications modifications =
094 _portalToLDAPConverter.getLDAPContactModifications(
095 contact, contactExpandoAttributes,
096 contactMappings, contactExpandoMappings);
097
098 if (modifications == null) {
099 return;
100 }
101
102 ModificationItem[] modificationItems = modifications.getItems();
103
104 ldapContext.modifyAttributes(name, modificationItems);
105 }
106 catch (Exception e) {
107 throw e;
108 }
109 finally {
110 if (ldapContext != null) {
111 ldapContext.close();
112 }
113 }
114 }
115
116 public void exportToLDAP(
117 User user, Map<String, Serializable> userExpandoAttributes)
118 throws Exception {
119
120 long companyId = user.getCompanyId();
121
122 if (!LDAPSettingsUtil.isAuthEnabled(companyId) ||
123 !LDAPSettingsUtil.isExportEnabled(companyId)) {
124
125 return;
126 }
127
128 long ldapServerId = PortalLDAPUtil.getLdapServerId(
129 companyId, user.getScreenName());
130
131 LdapContext ldapContext = PortalLDAPUtil.getContext(
132 ldapServerId, companyId);
133
134 try {
135 if (ldapContext == null) {
136 return;
137 }
138
139 Properties userMappings = LDAPSettingsUtil.getUserMappings(
140 ldapServerId, companyId);
141 Properties userExpandoMappings =
142 LDAPSettingsUtil.getUserExpandoMappings(
143 ldapServerId, companyId);
144
145 Binding binding = PortalLDAPUtil.getUser(
146 ldapServerId, user.getCompanyId(), user.getScreenName());
147
148 if (binding == null) {
149 binding = addUser(
150 ldapServerId, ldapContext, user, userMappings);
151 }
152
153 Name name = new CompositeName();
154
155 name.add(
156 PortalLDAPUtil.getNameInNamespace(
157 ldapServerId, companyId, binding));
158
159 Modifications modifications =
160 _portalToLDAPConverter.getLDAPUserModifications(
161 user, userExpandoAttributes, userMappings,
162 userExpandoMappings);
163
164 if (modifications == null) {
165 return;
166 }
167
168 ModificationItem[] modificationItems = modifications.getItems();
169
170 ldapContext.modifyAttributes(name, modificationItems);
171 }
172 catch (Exception e) {
173 _log.error(e, e);
174 }
175 finally {
176 if (ldapContext != null) {
177 ldapContext.close();
178 }
179 }
180 }
181
182 public void exportToLDAP(long userId, long userGroupId) throws Exception {
183 User user = UserLocalServiceUtil.getUser(userId);
184
185 long companyId = user.getCompanyId();
186
187 if (!LDAPSettingsUtil.isAuthEnabled(companyId) ||
188 !LDAPSettingsUtil.isExportEnabled(companyId)) {
189
190 return;
191 }
192
193 long ldapServerId = PortalLDAPUtil.getLdapServerId(
194 companyId, user.getScreenName());
195
196 LdapContext ldapContext = PortalLDAPUtil.getContext(
197 ldapServerId, companyId);
198
199 try {
200 if (ldapContext == null) {
201 return;
202 }
203
204 UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
205 userGroupId);
206
207 Properties groupMappings = LDAPSettingsUtil.getGroupMappings(
208 ldapServerId, companyId);
209
210 Properties userMappings = LDAPSettingsUtil.getUserMappings(
211 ldapServerId, companyId);
212
213 Binding binding = PortalLDAPUtil.getGroup(
214 ldapServerId, companyId, userGroup.getName());
215
216 if (binding == null) {
217 addGroup(
218 ldapServerId, ldapContext, userGroup, user, groupMappings,
219 userMappings);
220
221 return;
222 }
223
224 Name name = new CompositeName();
225
226 name.add(
227 PortalLDAPUtil.getNameInNamespace(
228 ldapServerId, companyId, binding));
229
230 Modifications modifications =
231 _portalToLDAPConverter.getLDAPGroupModifications(
232 ldapServerId, userGroup, user, groupMappings, userMappings);
233
234 ModificationItem[] modificationItems = modifications.getItems();
235
236 ldapContext.modifyAttributes(name, modificationItems);
237 }
238 catch (Exception e) {
239 _log.error(e, e);
240 }
241 finally {
242 if (ldapContext != null) {
243 ldapContext.close();
244 }
245 }
246 }
247
248 public void setPortalToLDAPConverter(
249 PortalToLDAPConverter portalToLDAPConverter) {
250
251 _portalToLDAPConverter = portalToLDAPConverter;
252 }
253
254 protected Binding addGroup(
255 long ldapServerId, LdapContext ldapContext, UserGroup userGroup,
256 User user, Properties groupMappings, Properties userMappings)
257 throws Exception {
258
259 Name name = new CompositeName();
260
261 name.add(
262 _portalToLDAPConverter.getGroupDNName(
263 ldapServerId, userGroup, groupMappings));
264
265 Attributes attributes = _portalToLDAPConverter.getLDAPGroupAttributes(
266 ldapServerId, userGroup, user, groupMappings, userMappings);
267
268 ldapContext.bind(name, new PortalLDAPContext(attributes));
269
270 Binding binding = PortalLDAPUtil.getGroup(
271 ldapServerId, userGroup.getCompanyId(), userGroup.getName());
272
273 return binding;
274 }
275
276 protected Binding addUser(
277 long ldapServerId, LdapContext ldapContext, User user,
278 Properties userMappings)
279 throws Exception {
280
281 Name name = new CompositeName();
282
283 name.add(
284 _portalToLDAPConverter.getUserDNName(
285 ldapServerId, user, userMappings));
286
287 Attributes attributes = _portalToLDAPConverter.getLDAPUserAttributes(
288 ldapServerId, user, userMappings);
289
290 ldapContext.bind(name, new PortalLDAPContext(attributes));
291
292 Binding binding = PortalLDAPUtil.getUser(
293 ldapServerId, user.getCompanyId(), user.getScreenName());
294
295 return binding;
296 }
297
298 private static Log _log = LogFactoryUtil.getLog(
299 PortalLDAPExporterImpl.class);
300
301 private PortalToLDAPConverter _portalToLDAPConverter;
302
303 }