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