001    /**
002     * Copyright (c) 2000-2012 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.GetterUtil;
024    import com.liferay.portal.kernel.util.LocaleUtil;
025    import com.liferay.portal.kernel.util.PropsKeys;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.model.Contact;
029    import com.liferay.portal.model.ContactConstants;
030    import com.liferay.portal.model.User;
031    import com.liferay.portal.security.auth.FullNameGenerator;
032    import com.liferay.portal.security.auth.FullNameGeneratorFactory;
033    import com.liferay.portal.service.ServiceContext;
034    import com.liferay.portal.service.persistence.ContactUtil;
035    import com.liferay.portal.service.persistence.UserUtil;
036    import com.liferay.portal.util.PrefsPropsUtil;
037    
038    import java.util.Calendar;
039    import java.util.HashMap;
040    import java.util.Locale;
041    import java.util.Map;
042    import java.util.Properties;
043    
044    import javax.naming.NamingException;
045    import javax.naming.directory.Attributes;
046    
047    /**
048     * @author Edward Han
049     * @author Brian Wing Shun Chan
050     */
051    public class DefaultLDAPToPortalConverter implements LDAPToPortalConverter {
052    
053            public LDAPGroup importLDAPGroup(
054                            long companyId, Attributes attributes, Properties groupMappings)
055                    throws Exception {
056    
057                    LDAPGroup ldapGroup = new LDAPGroup();
058    
059                    ldapGroup.setCompanyId(companyId);
060    
061                    String description = LDAPUtil.getAttributeString(
062                            attributes, groupMappings, GroupConverterKeys.DESCRIPTION);
063    
064                    ldapGroup.setDescription(description);
065    
066                    String groupName = LDAPUtil.getAttributeString(
067                            attributes, groupMappings, GroupConverterKeys.GROUP_NAME).
068                                    toLowerCase();
069    
070                    ldapGroup.setGroupName(groupName);
071    
072                    return ldapGroup;
073            }
074    
075            public LDAPUser importLDAPUser(
076                            long companyId, Attributes attributes, Properties userMappings,
077                            Properties userExpandoMappings, Properties contactMappings,
078                            Properties contactExpandoMappings, String password)
079                    throws Exception {
080    
081                    boolean autoScreenName = PrefsPropsUtil.getBoolean(
082                            companyId, PropsKeys.USERS_SCREEN_NAME_ALWAYS_AUTOGENERATE);
083    
084                    String screenName = LDAPUtil.getAttributeString(
085                            attributes, userMappings, UserConverterKeys.SCREEN_NAME).
086                                    toLowerCase();
087                    String emailAddress = LDAPUtil.getAttributeString(
088                            attributes, userMappings, UserConverterKeys.EMAIL_ADDRESS);
089    
090                    if (_log.isDebugEnabled()) {
091                            _log.debug(
092                                    "Screen name " + screenName + " and email address " +
093                                            emailAddress);
094                    }
095    
096                    String firstName = LDAPUtil.getAttributeString(
097                            attributes, userMappings, UserConverterKeys.FIRST_NAME);
098                    String middleName = LDAPUtil.getAttributeString(
099                            attributes, userMappings, UserConverterKeys.MIDDLE_NAME);
100                    String lastName = LDAPUtil.getAttributeString(
101                            attributes, userMappings, UserConverterKeys.LAST_NAME);
102    
103                    if (Validator.isNull(firstName) || Validator.isNull(lastName)) {
104                            String fullName = LDAPUtil.getAttributeString(
105                                    attributes, userMappings, UserConverterKeys.FULL_NAME);
106    
107                            FullNameGenerator fullNameGenerator =
108                                    FullNameGeneratorFactory.getInstance();
109    
110                            String[] names = fullNameGenerator.splitFullName(fullName);
111    
112                            firstName = names[0];
113                            middleName = names[1];
114                            lastName = names[2];
115                    }
116    
117                    if (!autoScreenName && Validator.isNull(screenName)) {
118                            throw new UserScreenNameException(
119                                    "Screen name cannot be null for " +
120                                            ContactConstants.getFullName(
121                                                    firstName, middleName, lastName));
122                    }
123    
124                    if (Validator.isNull(emailAddress) &&
125                            PrefsPropsUtil.getBoolean(
126                                    companyId, PropsKeys.USERS_EMAIL_ADDRESS_REQUIRED)) {
127    
128                            throw new UserEmailAddressException(
129                                    "Email address cannot be null for " +
130                                            ContactConstants.getFullName(
131                                                    firstName, middleName, lastName));
132                    }
133    
134                    LDAPUser ldapUser = new LDAPUser();
135    
136                    ldapUser.setAutoPassword(password.equals(StringPool.BLANK));
137                    ldapUser.setAutoScreenName(autoScreenName);
138    
139                    Contact contact = ContactUtil.create(0);
140    
141                    Calendar birthdayCalendar = CalendarFactoryUtil.getCalendar(
142                            1970, Calendar.JANUARY, 1);
143    
144                    contact.setBirthday(birthdayCalendar.getTime());
145    
146                    contact.setMale(true);
147                    contact.setPrefixId(0);
148                    contact.setSuffixId(0);
149    
150                    ldapUser.setContact(contact);
151    
152                    Map<String, String[]> contactExpandoAttributes = getExpandoAttributes(
153                            attributes, contactExpandoMappings);
154    
155                    ldapUser.setContactExpandoAttributes(contactExpandoAttributes);
156    
157                    ldapUser.setCreatorUserId(0);
158                    ldapUser.setGroupIds(null);
159                    ldapUser.setOrganizationIds(null);
160                    ldapUser.setPasswordReset(false);
161    
162                    Object portrait = LDAPUtil.getAttributeObject(
163                            attributes, userMappings.getProperty(UserConverterKeys.PORTRAIT));
164    
165                    if (portrait != null) {
166                            byte[] portraitBytes = (byte[])portrait;
167    
168                            if (portraitBytes.length > 0) {
169                                    ldapUser.setPortraitBytes((byte[])portrait);
170                            }
171    
172                            ldapUser.setUpdatePortrait(true);
173                    }
174    
175                    ldapUser.setRoleIds(null);
176                    ldapUser.setSendEmail(false);
177    
178                    ServiceContext serviceContext = new ServiceContext();
179    
180                    String uuid = LDAPUtil.getAttributeString(
181                            attributes, userMappings, UserConverterKeys.UUID);
182    
183                    serviceContext.setUuid(uuid);
184    
185                    ldapUser.setServiceContext(serviceContext);
186    
187                    ldapUser.setUpdatePassword(!password.equals(StringPool.BLANK));
188    
189                    User user = UserUtil.create(0);
190    
191                    user.setCompanyId(companyId);
192                    user.setEmailAddress(emailAddress);
193                    user.setFirstName(firstName);
194    
195                    String jobTitle = LDAPUtil.getAttributeString(
196                            attributes, userMappings, UserConverterKeys.JOB_TITLE);
197    
198                    user.setJobTitle(jobTitle);
199    
200                    Locale locale = LocaleUtil.getDefault();
201    
202                    user.setLanguageId(locale.toString());
203    
204                    user.setLastName(lastName);
205                    user.setMiddleName(middleName);
206                    user.setOpenId(StringPool.BLANK);
207                    user.setPasswordUnencrypted(password);
208                    user.setScreenName(screenName);
209    
210                    String status = LDAPUtil.getAttributeString(
211                            attributes, userMappings, UserConverterKeys.STATUS);
212    
213                    if (Validator.isNotNull(status)) {
214                            user.setStatus(GetterUtil.getInteger(status));
215                    }
216    
217                    ldapUser.setUser(user);
218    
219                    Map<String, String[]> userExpandoAttributes = getExpandoAttributes(
220                            attributes, userExpandoMappings);
221    
222                    ldapUser.setUserExpandoAttributes(userExpandoAttributes);
223    
224                    ldapUser.setUserGroupIds(null);
225                    ldapUser.setUserGroupRoles(null);
226    
227                    return ldapUser;
228            }
229    
230            protected Map<String, String[]> getExpandoAttributes(
231                            Attributes attributes, Properties expandoMappings)
232                    throws NamingException {
233    
234                    Map<String, String[]> expandoAttributes =
235                            new HashMap<String, String[]>();
236    
237                    for (Object key : expandoMappings.keySet()) {
238                            String name = (String)key;
239    
240                            String[] value = LDAPUtil.getAttributeStringArray(
241                                    attributes, expandoMappings, name);
242    
243                            if (Validator.isNotNull(value)) {
244                                    expandoAttributes.put(name, value);
245                            }
246                    }
247    
248                    return expandoAttributes;
249            }
250    
251            private static Log _log = LogFactoryUtil.getLog(
252                    DefaultLDAPToPortalConverter.class);
253    
254    }