001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.kernel.util.PropsKeys;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.model.Contact;
027    import com.liferay.portal.model.ContactConstants;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.model.UserGroupRole;
030    import com.liferay.portal.model.impl.ContactImpl;
031    import com.liferay.portal.model.impl.UserImpl;
032    import com.liferay.portal.security.auth.FullNameGenerator;
033    import com.liferay.portal.security.auth.FullNameGeneratorFactory;
034    import com.liferay.portal.service.ServiceContext;
035    import com.liferay.portal.util.PrefsPropsUtil;
036    import com.liferay.util.ldap.LDAPUtil;
037    
038    import java.util.Calendar;
039    import java.util.HashMap;
040    import java.util.List;
041    import java.util.Locale;
042    import java.util.Map;
043    import java.util.Properties;
044    
045    import javax.naming.NamingException;
046    import javax.naming.directory.Attributes;
047    
048    /**
049     * @author Edward Han
050     * @author Brian Wing Shun Chan
051     */
052    public class BaseLDAPToPortalConverter implements LDAPToPortalConverter {
053    
054            public LDAPGroup importLDAPGroup(
055                            long companyId, Attributes attributes, Properties groupMappings)
056                    throws Exception {
057    
058                    String groupName = LDAPUtil.getAttributeValue(
059                            attributes, groupMappings, GroupConverterKeys.GROUP_NAME).
060                                    toLowerCase();
061                    String description = LDAPUtil.getAttributeValue(
062                            attributes, groupMappings, GroupConverterKeys.DESCRIPTION);
063    
064                    LDAPGroup ldapGroup = new LDAPGroup();
065    
066                    ldapGroup.setCompanyId(companyId);
067                    ldapGroup.setDescription(description);
068                    ldapGroup.setGroupName(groupName);
069    
070                    return ldapGroup;
071            }
072    
073            public LDAPUser importLDAPUser(
074                            long companyId, Attributes attributes, Properties userMappings,
075                            Properties userExpandoMappings, Properties contactMappings,
076                            Properties contactExpandoMappings, String password)
077                    throws Exception {
078    
079                    boolean autoPassword = false;
080                    boolean updatePassword = true;
081    
082                    if (password.equals(StringPool.BLANK)) {
083                            autoPassword = true;
084                            updatePassword = false;
085                    }
086    
087                    long creatorUserId = 0;
088                    boolean passwordReset = false;
089                    boolean autoScreenName = PrefsPropsUtil.getBoolean(
090                            companyId, PropsKeys.USERS_SCREEN_NAME_ALWAYS_AUTOGENERATE);
091    
092                    String screenName = LDAPUtil.getAttributeValue(
093                            attributes, userMappings, UserConverterKeys.SCREEN_NAME).
094                                    toLowerCase();
095                    String emailAddress = LDAPUtil.getAttributeValue(
096                            attributes, userMappings, UserConverterKeys.EMAIL_ADDRESS);
097                    String openId = StringPool.BLANK;
098                    Locale locale = LocaleUtil.getDefault();
099                    String firstName = LDAPUtil.getAttributeValue(
100                            attributes, userMappings, UserConverterKeys.FIRST_NAME);
101                    String middleName = LDAPUtil.getAttributeValue(
102                            attributes, userMappings, UserConverterKeys.MIDDLE_NAME);
103                    String lastName = LDAPUtil.getAttributeValue(
104                            attributes, userMappings, UserConverterKeys.LAST_NAME);
105    
106                    if (Validator.isNull(firstName) || Validator.isNull(lastName)) {
107                            String fullName = LDAPUtil.getAttributeValue(
108                                    attributes, userMappings, UserConverterKeys.FULL_NAME);
109    
110                            FullNameGenerator fullNameGenerator =
111                                    FullNameGeneratorFactory.getInstance();
112    
113                            String[] names = fullNameGenerator.splitFullName(fullName);
114    
115                            firstName = names[0];
116                            middleName = names[1];
117                            lastName = names[2];
118                    }
119    
120                    int prefixId = 0;
121                    int suffixId = 0;
122                    boolean male = true;
123                    int birthdayMonth = Calendar.JANUARY;
124                    int birthdayDay = 1;
125                    int birthdayYear = 1970;
126                    String jobTitle = LDAPUtil.getAttributeValue(
127                            attributes, userMappings, UserConverterKeys.JOB_TITLE);
128                    long[] groupIds = null;
129                    long[] organizationIds = null;
130                    long[] roleIds = null;
131                    List<UserGroupRole> userGroupRoles = null;
132                    long[] userGroupIds = null;
133                    boolean sendEmail = false;
134    
135                    ServiceContext serviceContext = new ServiceContext();
136    
137                    if (_log.isDebugEnabled()) {
138                            _log.debug(
139                                    "Screen name " + screenName + " and email address " +
140                                            emailAddress);
141                    }
142    
143                    if (Validator.isNull(screenName) && !autoScreenName) {
144                            throw new UserScreenNameException(
145                                    "Screen name cannot be null for " +
146                                            ContactConstants.getFullName(
147                                                    firstName, middleName, lastName));
148                    }
149    
150                    if (Validator.isNull(emailAddress) &&
151                            PrefsPropsUtil.getBoolean(
152                                    companyId, PropsKeys.USERS_EMAIL_ADDRESS_REQUIRED)) {
153    
154                            throw new UserEmailAddressException(
155                                    "Email address cannot be null for " +
156                                            ContactConstants.getFullName(
157                                                    firstName, middleName, lastName));
158                    }
159    
160                    User user = new UserImpl();
161    
162                    user.setCompanyId(companyId);
163                    user.setEmailAddress(emailAddress);
164                    user.setFirstName(firstName);
165                    user.setJobTitle(jobTitle);
166                    user.setLanguageId(locale.toString());
167                    user.setLastName(lastName);
168                    user.setMiddleName(middleName);
169                    user.setOpenId(openId);
170                    user.setPasswordUnencrypted(password);
171                    user.setScreenName(screenName);
172    
173                    Map<String, String> userExpandoAttributes = getExpandoAttributes(
174                            attributes, userExpandoMappings);
175    
176                    Contact contact = new ContactImpl();
177    
178                    contact.setBirthday(
179                            CalendarFactoryUtil.getCalendar(
180                                    birthdayYear, birthdayMonth, birthdayDay).getTime());
181                    contact.setMale(male);
182                    contact.setPrefixId(prefixId);
183                    contact.setSuffixId(suffixId);
184    
185                    Map<String, String> contactExpandoAttributes = getExpandoAttributes(
186                            attributes, contactExpandoMappings);
187    
188                    LDAPUser ldapUser = new LDAPUser();
189    
190                    ldapUser.setAutoPassword(autoPassword);
191                    ldapUser.setAutoScreenName(autoScreenName);
192                    ldapUser.setContact(contact);
193                    ldapUser.setContactExpandoAttributes(contactExpandoAttributes);
194                    ldapUser.setCreatorUserId(creatorUserId);
195                    ldapUser.setGroupIds(groupIds);
196                    ldapUser.setOrganizationIds(organizationIds);
197                    ldapUser.setPasswordReset(passwordReset);
198                    ldapUser.setRoleIds(roleIds);
199                    ldapUser.setSendEmail(sendEmail);
200                    ldapUser.setServiceContext(serviceContext);
201                    ldapUser.setUpdatePassword(updatePassword);
202                    ldapUser.setUser(user);
203                    ldapUser.setUserExpandoAttributes(userExpandoAttributes);
204                    ldapUser.setUserGroupIds(userGroupIds);
205                    ldapUser.setUserGroupRoles(userGroupRoles);
206    
207                    return ldapUser;
208            }
209    
210            protected Map<String, String> getExpandoAttributes(
211                            Attributes attributes, Properties expandoMappings)
212                    throws NamingException {
213    
214                    Map<String, String> expandoAttributes = new HashMap<String, String>();
215    
216                    for (Object key : expandoMappings.keySet()) {
217                            String name = (String)key;
218    
219                            String value = LDAPUtil.getAttributeValue(
220                                    attributes,     expandoMappings, name);
221    
222                            if (Validator.isNotNull(value)) {
223                                    expandoAttributes.put(name, value);
224                            }
225                    }
226    
227                    return expandoAttributes;
228            }
229    
230            private static Log _log = LogFactoryUtil.getLog(
231                    BaseLDAPToPortalConverter.class);
232    
233    }