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 = PrefsPropsUtil.getBoolean(
92              companyId, PropsKeys.USERS_SCREEN_NAME_ALWAYS_AUTOGENERATE);
93  
94          String screenName = LDAPUtil.getAttributeValue(
95              attributes, userMappings, UserConverterKeys.SCREEN_NAME).
96                  toLowerCase();
97          String emailAddress = LDAPUtil.getAttributeValue(
98              attributes, userMappings, UserConverterKeys.EMAIL_ADDRESS);
99          String openId = StringPool.BLANK;
100         Locale locale = LocaleUtil.getDefault();
101         String firstName = LDAPUtil.getAttributeValue(
102             attributes, userMappings, UserConverterKeys.FIRST_NAME);
103         String middleName = LDAPUtil.getAttributeValue(
104             attributes, userMappings, UserConverterKeys.MIDDLE_NAME);
105         String lastName = LDAPUtil.getAttributeValue(
106             attributes, userMappings, UserConverterKeys.LAST_NAME);
107 
108         if (Validator.isNull(firstName) || Validator.isNull(lastName)) {
109             String fullName = LDAPUtil.getAttributeValue(
110                 attributes, userMappings, UserConverterKeys.FULL_NAME);
111 
112             FullNameGenerator fullNameGenerator =
113                 FullNameGeneratorFactory.getInstance();
114 
115             String[] names = fullNameGenerator.splitFullName(fullName);
116 
117             firstName = names[0];
118             middleName = names[1];
119             lastName = names[2];
120         }
121 
122         int prefixId = 0;
123         int suffixId = 0;
124         boolean male = true;
125         int birthdayMonth = Calendar.JANUARY;
126         int birthdayDay = 1;
127         int birthdayYear = 1970;
128         String jobTitle = LDAPUtil.getAttributeValue(
129             attributes, userMappings, UserConverterKeys.JOB_TITLE);
130         long[] groupIds = null;
131         long[] organizationIds = null;
132         long[] roleIds = null;
133         List<UserGroupRole> userGroupRoles = null;
134         long[] userGroupIds = null;
135         boolean sendEmail = false;
136 
137         ServiceContext serviceContext = new ServiceContext();
138 
139         if (_log.isDebugEnabled()) {
140             _log.debug(
141                 "Screen name " + screenName + " and email address " +
142                     emailAddress);
143         }
144 
145         if (Validator.isNull(screenName) && !autoScreenName) {
146             throw new UserScreenNameException(
147                 "Screen name cannot be null for " +
148                     ContactConstants.getFullName(
149                         firstName, middleName, lastName));
150         }
151 
152         if (Validator.isNull(emailAddress) &&
153             PrefsPropsUtil.getBoolean(
154                 companyId, PropsKeys.USERS_EMAIL_ADDRESS_REQUIRED)) {
155 
156             throw new UserEmailAddressException(
157                 "Email address cannot be null for " +
158                     ContactConstants.getFullName(
159                         firstName, middleName, lastName));
160         }
161 
162         User user = new UserImpl();
163 
164         user.setCompanyId(companyId);
165         user.setEmailAddress(emailAddress);
166         user.setFirstName(firstName);
167         user.setJobTitle(jobTitle);
168         user.setLanguageId(locale.getLanguage());
169         user.setLastName(lastName);
170         user.setMiddleName(middleName);
171         user.setOpenId(openId);
172         user.setPasswordUnencrypted(password);
173         user.setScreenName(screenName);
174 
175         Map<String, String> userExpandoAttributes = getExpandoAttributes(
176             attributes, userExpandoMappings);
177 
178         Contact contact = new ContactImpl();
179 
180         contact.setBirthday(
181             CalendarFactoryUtil.getCalendar(
182                 birthdayYear, birthdayMonth, birthdayDay).getTime());
183         contact.setMale(male);
184         contact.setPrefixId(prefixId);
185         contact.setSuffixId(suffixId);
186 
187         Map<String, String> contactExpandoAttributes = getExpandoAttributes(
188             attributes, contactExpandoMappings);
189 
190         LDAPUser ldapUser = new LDAPUser();
191 
192         ldapUser.setAutoPassword(autoPassword);
193         ldapUser.setAutoScreenName(autoScreenName);
194         ldapUser.setContact(contact);
195         ldapUser.setContactExpandoAttributes(contactExpandoAttributes);
196         ldapUser.setCreatorUserId(creatorUserId);
197         ldapUser.setGroupIds(groupIds);
198         ldapUser.setOrganizationIds(organizationIds);
199         ldapUser.setPasswordReset(passwordReset);
200         ldapUser.setRoleIds(roleIds);
201         ldapUser.setSendEmail(sendEmail);
202         ldapUser.setServiceContext(serviceContext);
203         ldapUser.setUpdatePassword(updatePassword);
204         ldapUser.setUser(user);
205         ldapUser.setUserExpandoAttributes(userExpandoAttributes);
206         ldapUser.setUserGroupIds(userGroupIds);
207         ldapUser.setUserGroupRoles(userGroupRoles);
208 
209         return ldapUser;
210     }
211 
212     protected Map<String, String> getExpandoAttributes(
213             Attributes attributes, Properties expandoMappings)
214         throws NamingException {
215 
216         Map<String, String> expandoAttributes = new HashMap<String, String>();
217 
218         for (Object key : expandoMappings.keySet()) {
219             String name = (String)key;
220 
221             String value = LDAPUtil.getAttributeValue(
222                 attributes, expandoMappings, name);
223 
224             if (Validator.isNotNull(value)) {
225                 expandoAttributes.put(name, value);
226             }
227         }
228 
229         return expandoAttributes;
230     }
231 
232     private static Log _log = LogFactoryUtil.getLog(
233         BaseLDAPToPortalConverter.class);
234 
235 }