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