1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
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.model.Contact;
20  import com.liferay.portal.model.User;
21  import com.liferay.portal.service.UserLocalServiceUtil;
22  
23  import java.io.Serializable;
24  
25  import java.util.Map;
26  import java.util.Properties;
27  
28  import javax.naming.Binding;
29  import javax.naming.CompositeName;
30  import javax.naming.Name;
31  import javax.naming.directory.Attributes;
32  import javax.naming.directory.ModificationItem;
33  import javax.naming.ldap.LdapContext;
34  
35  /**
36   * <a href="PortalLDAPExporterImpl.java.html}"><b><i>View Source</i></b></a>
37   *
38   * @author Michael C. Han
39   * @author Brian Wing Shun Chan
40   */
41  public class PortalLDAPExporterImpl implements PortalLDAPExporter {
42  
43      public void exportToLDAP(
44              Contact contact, Map<String, Serializable> contactExpandoAttributes)
45          throws Exception {
46  
47          long companyId = contact.getCompanyId();
48  
49          if (!LDAPSettingsUtil.isAuthEnabled(companyId) ||
50              !LDAPSettingsUtil.isExportEnabled(companyId)) {
51  
52              return;
53          }
54  
55          User user = UserLocalServiceUtil.getUserByContactId(
56              contact.getContactId());
57  
58          long ldapServerId = PortalLDAPUtil.getLdapServerId(
59              companyId, user.getScreenName());
60  
61          LdapContext ldapContext = PortalLDAPUtil.getContext(
62              ldapServerId, companyId);
63  
64          try {
65              if (ldapContext == null) {
66                  return;
67              }
68  
69              Properties contactMappings = LDAPSettingsUtil.getContactMappings(
70                  ldapServerId, companyId);
71              Properties contactExpandoMappings =
72                  LDAPSettingsUtil.getContactExpandoMappings(
73                      ldapServerId, companyId);
74  
75              Binding binding = PortalLDAPUtil.getUser(
76                  ldapServerId, contact.getCompanyId(), user.getScreenName());
77  
78              if (binding == null) {
79                  Properties userMappings = LDAPSettingsUtil.getUserMappings(
80                      ldapServerId, companyId);
81  
82                  binding = createLDAPUser(
83                      ldapServerId, ldapContext, user, userMappings);
84              }
85  
86              Name name = new CompositeName();
87  
88              name.add(
89                  PortalLDAPUtil.getNameInNamespace(
90                      ldapServerId, companyId, binding));
91  
92              Modifications modifications =
93                  _portalToLDAPConverter.getLDAPContactModifications(
94                      contact, contactExpandoAttributes,
95                      contactMappings, contactExpandoMappings);
96  
97              if (modifications == null) {
98                  return;
99              }
100 
101             ModificationItem[] modificationItems = modifications.getItems();
102 
103             ldapContext.modifyAttributes(name, modificationItems);
104         }
105         catch (Exception e) {
106             throw e;
107         }
108         finally {
109             if (ldapContext != null) {
110                 ldapContext.close();
111             }
112         }
113     }
114 
115     public void exportToLDAP(
116             User user, Map<String, Serializable> userExpandoAttributes)
117         throws Exception {
118 
119         long companyId = user.getCompanyId();
120 
121         if (!LDAPSettingsUtil.isAuthEnabled(companyId) ||
122             !LDAPSettingsUtil.isExportEnabled(companyId)) {
123 
124             return;
125         }
126 
127         long ldapServerId = PortalLDAPUtil.getLdapServerId(
128             companyId, user.getScreenName());
129 
130         LdapContext ldapContext = PortalLDAPUtil.getContext(
131             ldapServerId, companyId);
132 
133         try {
134             if (ldapContext == null) {
135                 return;
136             }
137 
138             Properties userMappings = LDAPSettingsUtil.getUserMappings(
139                 ldapServerId, companyId);
140             Properties userExpandoMappings =
141                 LDAPSettingsUtil.getUserExpandoMappings(
142                     ldapServerId, companyId);
143 
144             Binding binding = PortalLDAPUtil.getUser(
145                 ldapServerId, user.getCompanyId(), user.getScreenName());
146 
147             if (binding == null) {
148                 binding = createLDAPUser(
149                     ldapServerId, ldapContext, user, userMappings);
150             }
151 
152             Name name = new CompositeName();
153 
154             name.add(
155                 PortalLDAPUtil.getNameInNamespace(
156                     ldapServerId, companyId, binding));
157 
158             Modifications modifications =
159                 _portalToLDAPConverter.getLDAPUserModifications(
160                     user, userExpandoAttributes, userMappings,
161                     userExpandoMappings);
162 
163             if (modifications == null) {
164                 return;
165             }
166 
167             ModificationItem[] modificationItems = modifications.getItems();
168 
169             ldapContext.modifyAttributes(name, modificationItems);
170         }
171         catch (Exception e) {
172             _log.error(e, e);
173         }
174         finally {
175             if (ldapContext != null) {
176                 ldapContext.close();
177             }
178         }
179     }
180 
181     public void setPortalToLDAPConverter(
182         PortalToLDAPConverter portalToLDAPConverter) {
183 
184         _portalToLDAPConverter = portalToLDAPConverter;
185     }
186 
187     protected Binding createLDAPUser(
188             long ldapServerId, LdapContext ldapContext, User user,
189             Properties userMappings)
190         throws Exception {
191 
192         Name name = new CompositeName();
193 
194         name.add(
195             _portalToLDAPConverter.getUserDNName(
196                 ldapServerId, user, userMappings));
197 
198         Attributes attributes = _portalToLDAPConverter.getLDAPUserAttributes(
199             ldapServerId, user, userMappings);
200 
201         ldapContext.bind(name, new PortalLDAPContext(attributes));
202 
203         Binding binding = PortalLDAPUtil.getUser(
204             ldapServerId, user.getCompanyId(), user.getScreenName());
205 
206         return binding;
207     }
208 
209     private static Log _log = LogFactoryUtil.getLog(
210         PortalLDAPExporterImpl.class);
211 
212     private PortalToLDAPConverter _portalToLDAPConverter;
213 
214 }