1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.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.security.auth.AuthSettingsUtil;
25  import com.liferay.portal.service.UserLocalServiceUtil;
26  import com.liferay.portal.util.PropsValues;
27  import com.liferay.util.ldap.Modifications;
28  
29  import java.util.Properties;
30  
31  import javax.naming.Binding;
32  import javax.naming.CompositeName;
33  import javax.naming.Name;
34  import javax.naming.directory.ModificationItem;
35  import javax.naming.ldap.LdapContext;
36  
37  /**
38   * <a href="PortalLDAPExporter.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Edward Han
41   * @author Michael C. Han
42   * @author Brian Wing Shun Chan
43   */
44  public class PortalLDAPExporter {
45  
46      public static void exportToLDAP(Contact contact) throws Exception {
47          long companyId = contact.getCompanyId();
48  
49          if (!AuthSettingsUtil.isLDAPAuthEnabled(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 userMappings = LDAPSettingsUtil.getUserMappings(
70                  ldapServerId, companyId);
71              Binding binding = PortalLDAPUtil.getUser(
72                  ldapServerId, contact.getCompanyId(), user.getScreenName());
73              Name name = new CompositeName();
74  
75              if (binding == null) {
76  
77                  // Create new user in LDAP
78  
79                  _getDNName(
80                      ldapServerId, companyId, user, userMappings, name);
81  
82                  LDAPUser ldapUser = (LDAPUser)Class.forName(
83                      PropsValues.LDAP_USER_IMPL).newInstance();
84  
85                  ldapUser.setUser(user, ldapServerId);
86  
87                  ldapContext.bind(name, ldapUser);
88              }
89              else {
90  
91                  // Modify existing LDAP user record
92  
93                  name.add(
94                      PortalLDAPUtil.getNameInNamespace(
95                          ldapServerId, companyId, binding));
96  
97                  Modifications modifications = Modifications.getInstance();
98  
99                  modifications.addItem(
100                     userMappings.getProperty("firstName"),
101                     contact.getFirstName());
102 
103                 String middleNameMapping = userMappings.getProperty(
104                     "middleName");
105 
106                 if (Validator.isNotNull(middleNameMapping)) {
107                     modifications.addItem(
108                         middleNameMapping, contact.getMiddleName());
109                 }
110 
111                 modifications.addItem(
112                     userMappings.getProperty("lastName"),
113                     contact.getLastName());
114 
115                 String fullNameMapping = userMappings.getProperty("fullName");
116 
117                 if (Validator.isNotNull(fullNameMapping)) {
118                     modifications.addItem(
119                         fullNameMapping, contact.getFullName());
120                 }
121 
122                 String jobTitleMapping = userMappings.getProperty("jobTitle");
123 
124                 if (Validator.isNotNull(jobTitleMapping)) {
125                     modifications.addItem(
126                         jobTitleMapping, contact.getJobTitle());
127                 }
128 
129                 ModificationItem[] modificationItems = modifications.getItems();
130 
131                 ldapContext.modifyAttributes(name, modificationItems);
132             }
133         }
134         catch (Exception e) {
135             throw e;
136         }
137         finally {
138             if (ldapContext != null) {
139                 ldapContext.close();
140             }
141         }
142     }
143 
144     public static void exportToLDAP(User user) throws Exception {
145         long companyId = user.getCompanyId();
146 
147         if (!AuthSettingsUtil.isLDAPAuthEnabled(companyId) ||
148             !LDAPSettingsUtil.isExportEnabled(companyId)) {
149 
150             return;
151         }
152 
153         long ldapServerId = PortalLDAPUtil.getLdapServerId(
154             companyId, user.getScreenName());
155 
156         LdapContext ldapContext = PortalLDAPUtil.getContext(
157             ldapServerId, companyId);
158 
159         try {
160             if (ldapContext == null) {
161                 return;
162             }
163 
164             Properties userMappings = LDAPSettingsUtil.getUserMappings(
165                 ldapServerId, companyId);
166             Binding binding = PortalLDAPUtil.getUser(
167                 ldapServerId, user.getCompanyId(), user.getScreenName());
168             Name name = new CompositeName();
169 
170             if (binding == null) {
171 
172                 // Create new user in LDAP
173 
174                 _getDNName(
175                     ldapServerId, companyId, user, userMappings, name);
176 
177                 LDAPUser ldapUser = (LDAPUser) Class.forName(
178                     PropsValues.LDAP_USER_IMPL).newInstance();
179 
180                 ldapUser.setUser(user, ldapServerId);
181 
182                 ldapContext.bind(name, ldapUser);
183 
184                 binding = PortalLDAPUtil.getUser(
185                     ldapServerId, user.getCompanyId(), user.getScreenName());
186 
187                 name = new CompositeName();
188             }
189 
190             // Modify existing LDAP user record
191 
192             name.add(
193                 PortalLDAPUtil.getNameInNamespace(
194                     ldapServerId, companyId, binding));
195 
196             Modifications modifications = Modifications.getInstance();
197 
198             modifications.addItem(
199                 userMappings.getProperty("firstName"), user.getFirstName());
200 
201             String middleNameMapping = userMappings.getProperty(
202                 "middleName");
203 
204             if (Validator.isNotNull(middleNameMapping)) {
205                 modifications.addItem(middleNameMapping, user.getMiddleName());
206             }
207 
208             modifications.addItem(
209                 userMappings.getProperty("lastName"), user.getLastName());
210 
211             String fullNameMapping = userMappings.getProperty("fullName");
212 
213             if (Validator.isNotNull(fullNameMapping)) {
214                 modifications.addItem(fullNameMapping, user.getFullName());
215             }
216 
217             if (user.isPasswordModified() &&
218                 Validator.isNotNull(user.getPasswordUnencrypted())) {
219 
220                 modifications.addItem(
221                     userMappings.getProperty("password"),
222                     user.getPasswordUnencrypted());
223             }
224 
225             if (Validator.isNotNull(user.getEmailAddress())) {
226                 modifications.addItem(
227                     userMappings.getProperty("emailAddress"),
228                     user.getEmailAddress());
229             }
230 
231             String jobTitleMapping = userMappings.getProperty("jobTitle");
232 
233             if (Validator.isNotNull(jobTitleMapping)) {
234                 modifications.addItem(jobTitleMapping, user.getJobTitle());
235             }
236 
237             ModificationItem[] modificationItems = modifications.getItems();
238 
239             ldapContext.modifyAttributes(name, modificationItems);
240         }
241         catch (Exception e) {
242             _log.error(e, e);
243         }
244         finally {
245             if (ldapContext != null) {
246                 ldapContext.close();
247             }
248         }
249     }
250 
251     private static void _getDNName(
252             long ldapServerId, long companyId, User user,
253             Properties userMappings, Name name)
254         throws Exception {
255 
256         // Generate full DN based on user DN
257 
258         StringBundler sb = new StringBundler(5);
259 
260         sb.append(userMappings.getProperty("screenName"));
261         sb.append(StringPool.EQUAL);
262         sb.append(user.getScreenName());
263         sb.append(StringPool.COMMA);
264         sb.append(PortalLDAPUtil.getUsersDN(ldapServerId, companyId));
265 
266         name.add(sb.toString());
267     }
268 
269     private static Log _log = LogFactoryUtil.getLog(PortalLDAPExporter.class);
270 
271 }