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.UserEmailAddressException;
18  import com.liferay.portal.UserScreenNameException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
22  import com.liferay.portal.kernel.util.LocaleUtil;
23  import com.liferay.portal.kernel.util.PropsKeys;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.model.Contact;
27  import com.liferay.portal.model.ContactConstants;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.model.UserGroupRole;
30  import com.liferay.portal.model.impl.ContactImpl;
31  import com.liferay.portal.model.impl.UserImpl;
32  import com.liferay.portal.security.auth.FullNameGenerator;
33  import com.liferay.portal.security.auth.FullNameGeneratorFactory;
34  import com.liferay.portal.service.ServiceContext;
35  import com.liferay.portal.util.PrefsPropsUtil;
36  import com.liferay.util.ldap.LDAPUtil;
37  
38  import java.util.Calendar;
39  import java.util.HashMap;
40  import java.util.List;
41  import java.util.Locale;
42  import java.util.Map;
43  import java.util.Properties;
44  
45  import javax.naming.NamingException;
46  import javax.naming.directory.Attributes;
47  
48  /**
49   * <a href="BaseLDAPToPortalConverter.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Edward Han
52   * @author Brian Wing Shun Chan
53   */
54  public class BaseLDAPToPortalConverter implements LDAPToPortalConverter {
55  
56      public LDAPGroup importLDAPGroup(
57              long companyId, Attributes attributes, Properties groupMappings)
58          throws Exception {
59  
60          String groupName = LDAPUtil.getAttributeValue(
61              attributes, groupMappings, GroupConverterKeys.GROUP_NAME).
62                  toLowerCase();
63          String description = LDAPUtil.getAttributeValue(
64              attributes, groupMappings, GroupConverterKeys.DESCRIPTION);
65  
66          LDAPGroup ldapGroup = new LDAPGroup();
67  
68          ldapGroup.setCompanyId(companyId);
69          ldapGroup.setDescription(description);
70          ldapGroup.setGroupName(groupName);
71  
72          return ldapGroup;
73      }
74  
75      public LDAPUser importLDAPUser(
76              long companyId, Attributes attributes, Properties userMappings,
77              Properties userExpandoMappings, Properties contactMappings,
78              Properties contactExpandoMappings, String password)
79          throws Exception {
80  
81          boolean autoPassword = false;
82          boolean updatePassword = true;
83  
84          if (password.equals(StringPool.BLANK)) {
85              autoPassword = true;
86              updatePassword = false;
87          }
88  
89          long creatorUserId = 0;
90          boolean passwordReset = false;
91          boolean autoScreenName = false;
92  
93          String screenName = LDAPUtil.getAttributeValue(
94              attributes, userMappings, UserConverterKeys.SCREEN_NAME).
95                  toLowerCase();
96          String emailAddress = LDAPUtil.getAttributeValue(
97              attributes, userMappings, UserConverterKeys.EMAIL_ADDRESS);
98          String openId = StringPool.BLANK;
99          Locale locale = LocaleUtil.getDefault();
100         String firstName = LDAPUtil.getAttributeValue(
101             attributes, userMappings, UserConverterKeys.FIRST_NAME);
102         String middleName = LDAPUtil.getAttributeValue(
103             attributes, userMappings, UserConverterKeys.MIDDLE_NAME);
104         String lastName = LDAPUtil.getAttributeValue(
105             attributes, userMappings, UserConverterKeys.LAST_NAME);
106 
107         if (Validator.isNull(firstName) || Validator.isNull(lastName)) {
108             String fullName = LDAPUtil.getAttributeValue(
109                 attributes, userMappings, UserConverterKeys.FULL_NAME);
110 
111             FullNameGenerator fullNameGenerator =
112                 FullNameGeneratorFactory.getInstance();
113 
114             String[] names = fullNameGenerator.splitFullName(fullName);
115 
116             firstName = names[0];
117             middleName = names[1];
118             lastName = names[2];
119         }
120 
121         int prefixId = 0;
122         int suffixId = 0;
123         boolean male = true;
124         int birthdayMonth = Calendar.JANUARY;
125         int birthdayDay = 1;
126         int birthdayYear = 1970;
127         String jobTitle = LDAPUtil.getAttributeValue(
128             attributes, userMappings, UserConverterKeys.JOB_TITLE);
129         long[] groupIds = null;
130         long[] organizationIds = null;
131         long[] roleIds = null;
132         List<UserGroupRole> userGroupRoles = null;
133         long[] userGroupIds = null;
134         boolean sendEmail = false;
135 
136         ServiceContext serviceContext = new ServiceContext();
137 
138         if (_log.isDebugEnabled()) {
139             _log.debug(
140                 "Screen name " + screenName + " and email address " +
141                     emailAddress);
142         }
143 
144         if (Validator.isNull(screenName) &&
145             !PrefsPropsUtil.getBoolean(
146                 companyId, PropsKeys.USERS_SCREEN_NAME_ALWAYS_AUTOGENERATE)) {
147 
148             throw new UserScreenNameException(
149                 "Screen name cannot be null for " +
150                     ContactConstants.getFullName(
151                         firstName, middleName, lastName));
152         }
153 
154         if (Validator.isNull(emailAddress) &&
155             PrefsPropsUtil.getBoolean(
156                 companyId, PropsKeys.USERS_EMAIL_ADDRESS_REQUIRED)) {
157 
158             throw new UserEmailAddressException(
159                 "Email address cannot be null for " +
160                     ContactConstants.getFullName(
161                         firstName, middleName, lastName));
162         }
163 
164         User user = new UserImpl();
165 
166         user.setCompanyId(companyId);
167         user.setEmailAddress(emailAddress);
168         user.setFirstName(firstName);
169         user.setJobTitle(jobTitle);
170         user.setLanguageId(locale.getLanguage());
171         user.setLastName(lastName);
172         user.setMiddleName(middleName);
173         user.setOpenId(openId);
174         user.setPasswordUnencrypted(password);
175         user.setScreenName(screenName);
176 
177         Map<String, String> userExpandoAttributes = getExpandoAttributes(
178             attributes, userExpandoMappings);
179 
180         Contact contact = new ContactImpl();
181 
182         contact.setBirthday(
183             CalendarFactoryUtil.getCalendar(
184                 birthdayYear, birthdayMonth, birthdayDay).getTime());
185         contact.setMale(male);
186         contact.setPrefixId(prefixId);
187         contact.setSuffixId(suffixId);
188 
189         Map<String, String> contactExpandoAttributes = getExpandoAttributes(
190             attributes, contactExpandoMappings);
191 
192         LDAPUser ldapUser = new LDAPUser();
193 
194         ldapUser.setAutoPassword(autoPassword);
195         ldapUser.setAutoScreenName(autoScreenName);
196         ldapUser.setContact(contact);
197         ldapUser.setContactExpandoAttributes(contactExpandoAttributes);
198         ldapUser.setCreatorUserId(creatorUserId);
199         ldapUser.setGroupIds(groupIds);
200         ldapUser.setOrganizationIds(organizationIds);
201         ldapUser.setPasswordReset(passwordReset);
202         ldapUser.setRoleIds(roleIds);
203         ldapUser.setSendEmail(sendEmail);
204         ldapUser.setServiceContext(serviceContext);
205         ldapUser.setUpdatePassword(updatePassword);
206         ldapUser.setUser(user);
207         ldapUser.setUserExpandoAttributes(userExpandoAttributes);
208         ldapUser.setUserGroupIds(userGroupIds);
209         ldapUser.setUserGroupRoles(userGroupRoles);
210 
211         return ldapUser;
212     }
213 
214     protected Map<String, String> getExpandoAttributes(
215             Attributes attributes, Properties expandoMappings)
216         throws NamingException {
217 
218         Map<String, String> expandoAttributes = new HashMap<String, String>();
219 
220         for (Object key : expandoMappings.keySet()) {
221             String name = (String)key;
222 
223             String value = LDAPUtil.getAttributeValue(
224                 attributes, expandoMappings, name);
225 
226             if (Validator.isNotNull(value)) {
227                 expandoAttributes.put(name, value);
228             }
229         }
230 
231         return expandoAttributes;
232     }
233 
234     private static Log _log = LogFactoryUtil.getLog(
235         BaseLDAPToPortalConverter.class);
236 
237 }