001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.ldap;
016    
017    import com.liferay.portal.UserEmailAddressException;
018    import com.liferay.portal.UserScreenNameException;
019    import com.liferay.portal.kernel.ldap.LDAPUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
023    import com.liferay.portal.kernel.util.DateUtil;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.LocaleUtil;
026    import com.liferay.portal.kernel.util.PropsKeys;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.model.Contact;
030    import com.liferay.portal.model.ContactConstants;
031    import com.liferay.portal.model.ListType;
032    import com.liferay.portal.model.ListTypeConstants;
033    import com.liferay.portal.model.User;
034    import com.liferay.portal.security.auth.FullNameGenerator;
035    import com.liferay.portal.security.auth.FullNameGeneratorFactory;
036    import com.liferay.portal.service.ListTypeServiceUtil;
037    import com.liferay.portal.service.ServiceContext;
038    import com.liferay.portal.service.persistence.ContactUtil;
039    import com.liferay.portal.service.persistence.UserUtil;
040    import com.liferay.portal.util.PrefsPropsUtil;
041    
042    import java.text.ParseException;
043    
044    import java.util.Calendar;
045    import java.util.Date;
046    import java.util.HashMap;
047    import java.util.List;
048    import java.util.Locale;
049    import java.util.Map;
050    import java.util.Properties;
051    
052    import javax.naming.NamingException;
053    import javax.naming.directory.Attributes;
054    
055    /**
056     * @author Edward Han
057     * @author Brian Wing Shun Chan
058     */
059    public class DefaultLDAPToPortalConverter implements LDAPToPortalConverter {
060    
061            public LDAPGroup importLDAPGroup(
062                            long companyId, Attributes attributes, Properties groupMappings)
063                    throws Exception {
064    
065                    LDAPGroup ldapGroup = new LDAPGroup();
066    
067                    ldapGroup.setCompanyId(companyId);
068    
069                    String description = LDAPUtil.getAttributeString(
070                            attributes, groupMappings, GroupConverterKeys.DESCRIPTION);
071    
072                    ldapGroup.setDescription(description);
073    
074                    String groupName = LDAPUtil.getAttributeString(
075                            attributes, groupMappings, GroupConverterKeys.GROUP_NAME).
076                                    toLowerCase();
077    
078                    ldapGroup.setGroupName(groupName);
079    
080                    return ldapGroup;
081            }
082    
083            public LDAPUser importLDAPUser(
084                            long companyId, Attributes attributes, Properties userMappings,
085                            Properties userExpandoMappings, Properties contactMappings,
086                            Properties contactExpandoMappings, String password)
087                    throws Exception {
088    
089                    boolean autoScreenName = PrefsPropsUtil.getBoolean(
090                            companyId, PropsKeys.USERS_SCREEN_NAME_ALWAYS_AUTOGENERATE);
091    
092                    String screenName = LDAPUtil.getAttributeString(
093                            attributes, userMappings, UserConverterKeys.SCREEN_NAME).
094                                    toLowerCase();
095                    String emailAddress = LDAPUtil.getAttributeString(
096                            attributes, userMappings, UserConverterKeys.EMAIL_ADDRESS);
097    
098                    if (_log.isDebugEnabled()) {
099                            _log.debug(
100                                    "Screen name " + screenName + " and email address " +
101                                            emailAddress);
102                    }
103    
104                    String firstName = LDAPUtil.getAttributeString(
105                            attributes, userMappings, UserConverterKeys.FIRST_NAME);
106                    String middleName = LDAPUtil.getAttributeString(
107                            attributes, userMappings, UserConverterKeys.MIDDLE_NAME);
108                    String lastName = LDAPUtil.getAttributeString(
109                            attributes, userMappings, UserConverterKeys.LAST_NAME);
110    
111                    if (Validator.isNull(firstName) || Validator.isNull(lastName)) {
112                            String fullName = LDAPUtil.getAttributeString(
113                                    attributes, userMappings, UserConverterKeys.FULL_NAME);
114    
115                            FullNameGenerator fullNameGenerator =
116                                    FullNameGeneratorFactory.getInstance();
117    
118                            String[] names = fullNameGenerator.splitFullName(fullName);
119    
120                            firstName = names[0];
121                            middleName = names[1];
122                            lastName = names[2];
123                    }
124    
125                    if (!autoScreenName && Validator.isNull(screenName)) {
126                            throw new UserScreenNameException(
127                                    "Screen name cannot be null for " +
128                                            ContactConstants.getFullName(
129                                                    firstName, middleName, lastName));
130                    }
131    
132                    if (Validator.isNull(emailAddress) &&
133                            PrefsPropsUtil.getBoolean(
134                                    companyId, PropsKeys.USERS_EMAIL_ADDRESS_REQUIRED)) {
135    
136                            throw new UserEmailAddressException(
137                                    "Email address cannot be null for " +
138                                            ContactConstants.getFullName(
139                                                    firstName, middleName, lastName));
140                    }
141    
142                    LDAPUser ldapUser = new LDAPUser();
143    
144                    ldapUser.setAutoPassword(password.equals(StringPool.BLANK));
145                    ldapUser.setAutoScreenName(autoScreenName);
146    
147                    Contact contact = ContactUtil.create(0);
148    
149                    int prefixId = getListTypeId(
150                            attributes, contactMappings, ContactConverterKeys.PREFIX,
151                            ListTypeConstants.CONTACT_PREFIX);
152    
153                    contact.setPrefixId(prefixId);
154    
155                    int suffixId = getListTypeId(
156                            attributes, contactMappings, ContactConverterKeys.SUFFIX,
157                            ListTypeConstants.CONTACT_SUFFIX);
158    
159                    contact.setSuffixId(suffixId);
160    
161                    String gender = LDAPUtil.getAttributeString(
162                            attributes, contactMappings, ContactConverterKeys.GENDER);
163    
164                    gender = gender.toLowerCase();
165    
166                    if (GetterUtil.getBoolean(gender) || gender.equals("female")) {
167                            contact.setMale(false);
168                    }
169                    else {
170                            contact.setMale(true);
171                    }
172    
173                    try {
174                            Date birthday = DateUtil.parseDate(
175                                    LDAPUtil.getAttributeString(
176                                            attributes, contactMappings, ContactConverterKeys.BIRTHDAY),
177                                    LocaleUtil.getDefault());
178    
179                            contact.setBirthday(birthday);
180                    }
181                    catch (ParseException pe) {
182                            Calendar birthdayCalendar = CalendarFactoryUtil.getCalendar(
183                                    1970, Calendar.JANUARY, 1);
184    
185                            contact.setBirthday(birthdayCalendar.getTime());
186                    }
187    
188                    contact.setSmsSn(
189                            LDAPUtil.getAttributeString(
190                                    attributes, contactMappings, ContactConverterKeys.SMS_SN));
191                    contact.setAimSn(
192                            LDAPUtil.getAttributeString(
193                                    attributes, contactMappings, ContactConverterKeys.AIM_SN));
194                    contact.setFacebookSn(
195                            LDAPUtil.getAttributeString(
196                                    attributes, contactMappings, ContactConverterKeys.FACEBOOK_SN));
197                    contact.setIcqSn(
198                            LDAPUtil.getAttributeString(
199                                    attributes, contactMappings, ContactConverterKeys.ICQ_SN));
200                    contact.setJabberSn(
201                            LDAPUtil.getAttributeString(
202                                    attributes, contactMappings, ContactConverterKeys.JABBER_SN));
203                    contact.setMsnSn(
204                            LDAPUtil.getAttributeString(
205                                    attributes, contactMappings, ContactConverterKeys.MSN_SN));
206                    contact.setMySpaceSn(
207                            LDAPUtil.getAttributeString(
208                                    attributes, contactMappings, ContactConverterKeys.MYSPACE_SN));
209                    contact.setSkypeSn(
210                            LDAPUtil.getAttributeString(
211                                    attributes, contactMappings, ContactConverterKeys.SKYPE_SN));
212                    contact.setTwitterSn(
213                            LDAPUtil.getAttributeString(
214                                    attributes, contactMappings, ContactConverterKeys.TWITTER_SN));
215                    contact.setYmSn(
216                            LDAPUtil.getAttributeString(
217                                    attributes, contactMappings, ContactConverterKeys.YM_SN));
218                    contact.setJobTitle(
219                            LDAPUtil.getAttributeString(
220                                    attributes, contactMappings, ContactConverterKeys.JOB_TITLE));
221    
222                    ldapUser.setContact(contact);
223    
224                    Map<String, String[]> contactExpandoAttributes = getExpandoAttributes(
225                            attributes, contactExpandoMappings);
226    
227                    ldapUser.setContactExpandoAttributes(contactExpandoAttributes);
228    
229                    ldapUser.setCreatorUserId(0);
230                    ldapUser.setGroupIds(null);
231                    ldapUser.setOrganizationIds(null);
232                    ldapUser.setPasswordReset(false);
233    
234                    Object portrait = LDAPUtil.getAttributeObject(
235                            attributes, userMappings.getProperty(UserConverterKeys.PORTRAIT));
236    
237                    if (portrait != null) {
238                            byte[] portraitBytes = (byte[])portrait;
239    
240                            if (portraitBytes.length > 0) {
241                                    ldapUser.setPortraitBytes((byte[])portrait);
242                            }
243    
244                            ldapUser.setUpdatePortrait(true);
245                    }
246    
247                    ldapUser.setRoleIds(null);
248                    ldapUser.setSendEmail(false);
249    
250                    ServiceContext serviceContext = new ServiceContext();
251    
252                    String uuid = LDAPUtil.getAttributeString(
253                            attributes, userMappings, UserConverterKeys.UUID);
254    
255                    serviceContext.setUuid(uuid);
256    
257                    ldapUser.setServiceContext(serviceContext);
258    
259                    ldapUser.setUpdatePassword(!password.equals(StringPool.BLANK));
260    
261                    User user = UserUtil.create(0);
262    
263                    user.setCompanyId(companyId);
264                    user.setEmailAddress(emailAddress);
265                    user.setFirstName(firstName);
266    
267                    String jobTitle = LDAPUtil.getAttributeString(
268                            attributes, userMappings, UserConverterKeys.JOB_TITLE);
269    
270                    user.setJobTitle(jobTitle);
271    
272                    Locale locale = LocaleUtil.getDefault();
273    
274                    user.setLanguageId(locale.toString());
275    
276                    user.setLastName(lastName);
277                    user.setMiddleName(middleName);
278                    user.setOpenId(StringPool.BLANK);
279                    user.setPasswordUnencrypted(password);
280                    user.setScreenName(screenName);
281    
282                    String status = LDAPUtil.getAttributeString(
283                            attributes, userMappings, UserConverterKeys.STATUS);
284    
285                    if (Validator.isNotNull(status)) {
286                            user.setStatus(GetterUtil.getInteger(status));
287                    }
288    
289                    ldapUser.setUser(user);
290    
291                    Map<String, String[]> userExpandoAttributes = getExpandoAttributes(
292                            attributes, userExpandoMappings);
293    
294                    ldapUser.setUserExpandoAttributes(userExpandoAttributes);
295    
296                    ldapUser.setUserGroupIds(null);
297                    ldapUser.setUserGroupRoles(null);
298    
299                    return ldapUser;
300            }
301    
302            protected Map<String, String[]> getExpandoAttributes(
303                            Attributes attributes, Properties expandoMappings)
304                    throws NamingException {
305    
306                    Map<String, String[]> expandoAttributes =
307                            new HashMap<String, String[]>();
308    
309                    for (Object key : expandoMappings.keySet()) {
310                            String name = (String)key;
311    
312                            String[] value = LDAPUtil.getAttributeStringArray(
313                                    attributes, expandoMappings, name);
314    
315                            if (value != null) {
316                                    expandoAttributes.put(name, value);
317                            }
318                    }
319    
320                    return expandoAttributes;
321            }
322    
323            protected int getListTypeId(
324                            Attributes attributes, Properties contactMappings,
325                            String contactMappingsKey, String listTypeType)
326                    throws Exception {
327    
328                    List<ListType> contactPrefixListTypes =
329                            ListTypeServiceUtil.getListTypes(listTypeType);
330    
331                    String name = LDAPUtil.getAttributeString(
332                            attributes, contactMappings, contactMappingsKey);
333    
334                    for (ListType listType : contactPrefixListTypes) {
335                            if (name.equals(listType.getName())) {
336                                    return listType.getListTypeId();
337                            }
338                    }
339    
340                    return 0;
341            }
342    
343            private static Log _log = LogFactoryUtil.getLog(
344                    DefaultLDAPToPortalConverter.class);
345    
346    }