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(
130 "Screen name cannot be null for " +
131 ContactConstants.getFullName(
132 firstName, middleName, lastName));
133 }
134
135 if (Validator.isNull(emailAddress) &&
136 PrefsPropsUtil.getBoolean(
137 companyId, PropsKeys.USERS_EMAIL_ADDRESS_REQUIRED)) {
138
139 throw new UserEmailAddressException(
140 "Email address cannot be null for " +
141 ContactConstants.getFullName(
142 firstName, middleName, lastName));
143 }
144
145 LDAPUser ldapUser = new LDAPUser();
146
147 ldapUser.setAutoPassword(password.equals(StringPool.BLANK));
148 ldapUser.setAutoScreenName(autoScreenName);
149
150 Contact contact = ContactUtil.create(0);
151
152 int prefixId = getListTypeId(
153 attributes, contactMappings, ContactConverterKeys.PREFIX,
154 ListTypeConstants.CONTACT_PREFIX);
155
156 contact.setPrefixId(prefixId);
157
158 int suffixId = getListTypeId(
159 attributes, contactMappings, ContactConverterKeys.SUFFIX,
160 ListTypeConstants.CONTACT_SUFFIX);
161
162 contact.setSuffixId(suffixId);
163
164 String gender = LDAPUtil.getAttributeString(
165 attributes, contactMappings, ContactConverterKeys.GENDER);
166
167 gender = StringUtil.toLowerCase(gender);
168
169 if (GetterUtil.getBoolean(gender) || gender.equals("female")) {
170 contact.setMale(false);
171 }
172 else {
173 contact.setMale(true);
174 }
175
176 try {
177 Date birthday = DateUtil.parseDate(
178 LDAPUtil.getAttributeString(
179 attributes, contactMappings, ContactConverterKeys.BIRTHDAY),
180 LocaleUtil.getDefault());
181
182 contact.setBirthday(birthday);
183 }
184 catch (ParseException pe) {
185 Calendar birthdayCalendar = CalendarFactoryUtil.getCalendar(
186 1970, Calendar.JANUARY, 1);
187
188 contact.setBirthday(birthdayCalendar.getTime());
189 }
190
191 contact.setSmsSn(
192 LDAPUtil.getAttributeString(
193 attributes, contactMappings, ContactConverterKeys.SMS_SN));
194 contact.setAimSn(
195 LDAPUtil.getAttributeString(
196 attributes, contactMappings, ContactConverterKeys.AIM_SN));
197 contact.setFacebookSn(
198 LDAPUtil.getAttributeString(
199 attributes, contactMappings, ContactConverterKeys.FACEBOOK_SN));
200 contact.setIcqSn(
201 LDAPUtil.getAttributeString(
202 attributes, contactMappings, ContactConverterKeys.ICQ_SN));
203 contact.setJabberSn(
204 LDAPUtil.getAttributeString(
205 attributes, contactMappings, ContactConverterKeys.JABBER_SN));
206 contact.setMsnSn(
207 LDAPUtil.getAttributeString(
208 attributes, contactMappings, ContactConverterKeys.MSN_SN));
209 contact.setMySpaceSn(
210 LDAPUtil.getAttributeString(
211 attributes, contactMappings, ContactConverterKeys.MYSPACE_SN));
212 contact.setSkypeSn(
213 LDAPUtil.getAttributeString(
214 attributes, contactMappings, ContactConverterKeys.SKYPE_SN));
215 contact.setTwitterSn(
216 LDAPUtil.getAttributeString(
217 attributes, contactMappings, ContactConverterKeys.TWITTER_SN));
218 contact.setYmSn(
219 LDAPUtil.getAttributeString(
220 attributes, contactMappings, ContactConverterKeys.YM_SN));
221 contact.setJobTitle(
222 LDAPUtil.getAttributeString(
223 attributes, contactMappings, ContactConverterKeys.JOB_TITLE));
224
225 ldapUser.setContact(contact);
226
227 Map<String, String[]> contactExpandoAttributes = getExpandoAttributes(
228 attributes, contactExpandoMappings);
229
230 ldapUser.setContactExpandoAttributes(contactExpandoAttributes);
231
232 ldapUser.setCreatorUserId(0);
233 ldapUser.setGroupIds(null);
234 ldapUser.setOrganizationIds(null);
235 ldapUser.setPasswordReset(false);
236
237 Object portrait = LDAPUtil.getAttributeObject(
238 attributes, userMappings.getProperty(UserConverterKeys.PORTRAIT));
239
240 if (portrait != null) {
241 byte[] portraitBytes = (byte[])portrait;
242
243 if (portraitBytes.length > 0) {
244 ldapUser.setPortraitBytes((byte[])portrait);
245 }
246
247 ldapUser.setUpdatePortrait(true);
248 }
249
250 ldapUser.setRoleIds(null);
251 ldapUser.setSendEmail(false);
252
253 ServiceContext serviceContext = new ServiceContext();
254
255 String uuid = LDAPUtil.getAttributeString(
256 attributes, userMappings, UserConverterKeys.UUID);
257
258 serviceContext.setUuid(uuid);
259
260 ldapUser.setServiceContext(serviceContext);
261
262 ldapUser.setUpdatePassword(!password.equals(StringPool.BLANK));
263
264 User user = UserUtil.create(0);
265
266 user.setCompanyId(companyId);
267 user.setEmailAddress(emailAddress);
268 user.setFirstName(firstName);
269
270 String jobTitle = LDAPUtil.getAttributeString(
271 attributes, userMappings, UserConverterKeys.JOB_TITLE);
272
273 user.setJobTitle(jobTitle);
274
275 Locale locale = LocaleUtil.getDefault();
276
277 user.setLanguageId(locale.toString());
278
279 user.setLastName(lastName);
280 user.setMiddleName(middleName);
281 user.setOpenId(StringPool.BLANK);
282 user.setPasswordUnencrypted(password);
283 user.setScreenName(screenName);
284
285 String status = LDAPUtil.getAttributeString(
286 attributes, userMappings, UserConverterKeys.STATUS);
287
288 if (Validator.isNotNull(status)) {
289 user.setStatus(GetterUtil.getInteger(status));
290 }
291
292 ldapUser.setUser(user);
293
294 Map<String, String[]> userExpandoAttributes = getExpandoAttributes(
295 attributes, userExpandoMappings);
296
297 ldapUser.setUserExpandoAttributes(userExpandoAttributes);
298
299 ldapUser.setUserGroupIds(null);
300 ldapUser.setUserGroupRoles(null);
301
302 return ldapUser;
303 }
304
305 protected Map<String, String[]> getExpandoAttributes(
306 Attributes attributes, Properties expandoMappings)
307 throws NamingException {
308
309 Map<String, String[]> expandoAttributes =
310 new HashMap<String, String[]>();
311
312 for (Object key : expandoMappings.keySet()) {
313 String name = (String)key;
314
315 String[] value = LDAPUtil.getAttributeStringArray(
316 attributes, expandoMappings, name);
317
318 if (value != null) {
319 expandoAttributes.put(name, value);
320 }
321 }
322
323 return expandoAttributes;
324 }
325
326 protected int getListTypeId(
327 Attributes attributes, Properties contactMappings,
328 String contactMappingsKey, String listTypeType)
329 throws Exception {
330
331 List<ListType> contactPrefixListTypes =
332 ListTypeServiceUtil.getListTypes(listTypeType);
333
334 String name = LDAPUtil.getAttributeString(
335 attributes, contactMappings, contactMappingsKey);
336
337 for (ListType listType : contactPrefixListTypes) {
338 if (name.equals(listType.getName())) {
339 return listType.getListTypeId();
340 }
341 }
342
343 return 0;
344 }
345
346 private static Log _log = LogFactoryUtil.getLog(
347 DefaultLDAPToPortalConverter.class);
348
349 }