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