1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.security.ldap;
16  
17  import com.liferay.portal.NoSuchRoleException;
18  import com.liferay.portal.NoSuchUserException;
19  import com.liferay.portal.NoSuchUserGroupException;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.ArrayUtil;
23  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
24  import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
25  import com.liferay.portal.kernel.util.PropsKeys;
26  import com.liferay.portal.kernel.util.StringBundler;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.Company;
31  import com.liferay.portal.model.CompanyConstants;
32  import com.liferay.portal.model.Contact;
33  import com.liferay.portal.model.Group;
34  import com.liferay.portal.model.Role;
35  import com.liferay.portal.model.RoleConstants;
36  import com.liferay.portal.model.User;
37  import com.liferay.portal.model.UserGroup;
38  import com.liferay.portal.security.auth.ScreenNameGenerator;
39  import com.liferay.portal.security.auth.ScreenNameGeneratorFactory;
40  import com.liferay.portal.service.CompanyLocalServiceUtil;
41  import com.liferay.portal.service.GroupLocalServiceUtil;
42  import com.liferay.portal.service.RoleLocalServiceUtil;
43  import com.liferay.portal.service.UserGroupLocalServiceUtil;
44  import com.liferay.portal.service.UserLocalServiceUtil;
45  import com.liferay.portal.util.PrefsPropsUtil;
46  import com.liferay.portal.util.PropsValues;
47  import com.liferay.portlet.expando.model.ExpandoBridge;
48  import com.liferay.portlet.expando.model.ExpandoTableConstants;
49  import com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil;
50  import com.liferay.portlet.expando.util.ExpandoConverterUtil;
51  import com.liferay.util.ldap.LDAPUtil;
52  
53  import java.io.Serializable;
54  
55  import java.text.DateFormat;
56  import java.text.ParseException;
57  
58  import java.util.ArrayList;
59  import java.util.Calendar;
60  import java.util.Date;
61  import java.util.List;
62  import java.util.Map;
63  import java.util.Properties;
64  
65  import javax.naming.Binding;
66  import javax.naming.NameNotFoundException;
67  import javax.naming.directory.Attribute;
68  import javax.naming.directory.Attributes;
69  import javax.naming.directory.SearchResult;
70  import javax.naming.ldap.LdapContext;
71  
72  /**
73   * <a href="PortalLDAPImporterImpl.java.html"><b><i>View Source</i></b></a>
74   *
75   * @author Michael C. Han
76   * @author Brian Wing Shun Chan
77   */
78  public class PortalLDAPImporterImpl implements PortalLDAPImporter {
79  
80      public void importFromLDAP() throws Exception {
81          List<Company> companies = CompanyLocalServiceUtil.getCompanies(false);
82  
83          for (Company company : companies) {
84              importFromLDAP(company.getCompanyId());
85          }
86      }
87  
88      public void importFromLDAP(long companyId) throws Exception {
89          if (!LDAPSettingsUtil.isImportEnabled(companyId)) {
90              return;
91          }
92  
93          long[] ldapServerIds = StringUtil.split(
94              PrefsPropsUtil.getString(companyId, "ldap.server.ids"), 0L);
95  
96          for (long ldapServerId : ldapServerIds) {
97              importFromLDAP(ldapServerId, companyId);
98          }
99      }
100 
101     public void importFromLDAP(long ldapServerId, long companyId)
102         throws Exception {
103 
104         if (!LDAPSettingsUtil.isImportEnabled(companyId)) {
105             return;
106         }
107 
108         LdapContext ldapContext = PortalLDAPUtil.getContext(
109             ldapServerId, companyId);
110 
111         if (ldapContext == null) {
112             return;
113         }
114 
115         try {
116             Properties userMappings = LDAPSettingsUtil.getUserMappings(
117                 ldapServerId, companyId);
118             Properties userExpandoMappings =
119                 LDAPSettingsUtil.getUserExpandoMappings(
120                     ldapServerId, companyId);
121             Properties contactMappings = LDAPSettingsUtil.getContactMappings(
122                 ldapServerId, companyId);
123             Properties contactExpandoMappings =
124                 LDAPSettingsUtil.getContactExpandoMappings(
125                     ldapServerId, companyId);
126             Properties groupMappings = LDAPSettingsUtil.getGroupMappings(
127                 ldapServerId, companyId);
128 
129             String importMethod = PrefsPropsUtil.getString(
130                 companyId, PropsKeys.LDAP_IMPORT_METHOD);
131 
132             if (importMethod.equals(_IMPORT_BY_GROUP)) {
133                 importFromLDAPByGroup(
134                     ldapServerId, companyId, ldapContext, userMappings,
135                     userExpandoMappings, contactMappings,
136                     contactExpandoMappings, groupMappings);
137             }
138             else if (importMethod.equals(_IMPORT_BY_USER)) {
139                 importFromLDAPByUser(
140                     ldapServerId, companyId, ldapContext, userMappings,
141                     userExpandoMappings, contactMappings,
142                     contactExpandoMappings, groupMappings);
143             }
144         }
145         catch (Exception e) {
146             _log.error("Error importing LDAP users and groups", e);
147         }
148         finally {
149             if (ldapContext != null) {
150                 ldapContext.close();
151             }
152         }
153     }
154 
155     public User importLDAPUser(
156             long ldapServerId, long companyId, LdapContext ldapContext,
157             Attributes attributes, String password)
158         throws Exception {
159 
160         Properties userMappings = LDAPSettingsUtil.getUserMappings(
161             ldapServerId, companyId);
162         Properties userExpandoMappings =
163             LDAPSettingsUtil.getUserExpandoMappings(
164                 ldapServerId, companyId);
165         Properties contactMappings = LDAPSettingsUtil.getContactMappings(
166             ldapServerId, companyId);
167         Properties contactExpandoMappings =
168             LDAPSettingsUtil.getContactExpandoMappings(ldapServerId, companyId);
169 
170         User user = importUser(
171             companyId, attributes, userMappings, userExpandoMappings,
172             contactMappings, contactExpandoMappings, password);
173 
174         Properties groupMappings = LDAPSettingsUtil.getGroupMappings(
175             ldapServerId, companyId);
176 
177         importGroups(
178             ldapServerId, companyId, ldapContext, attributes, user,
179             userMappings, groupMappings);
180 
181         return user;
182     }
183 
184     public void setLDAPToPortalConverter(
185         LDAPToPortalConverter ldapToPortalConverter) {
186 
187         _ldapToPortalConverter = ldapToPortalConverter;
188     }
189 
190     protected void addRole(
191             long companyId, LDAPGroup ldapGroup, UserGroup userGroup)
192         throws Exception {
193 
194         if (!PropsValues.LDAP_IMPORT_CREATE_ROLE_PER_GROUP) {
195             return;
196         }
197 
198         Role role = null;
199 
200         try {
201             role = RoleLocalServiceUtil.getRole(
202                 companyId, ldapGroup.getGroupName());
203         }
204         catch (NoSuchRoleException nsre) {
205             User defaultUser = UserLocalServiceUtil.getDefaultUser(
206                 companyId);
207 
208             role = RoleLocalServiceUtil.addRole(
209                 defaultUser.getUserId(), companyId, ldapGroup.getGroupName(),
210                 null, "Autogenerated role from LDAP import",
211                 RoleConstants.TYPE_REGULAR);
212         }
213 
214         Group group = userGroup.getGroup();
215 
216         if (GroupLocalServiceUtil.hasRoleGroup(
217                 role.getRoleId(), group.getGroupId())) {
218 
219             return;
220         }
221 
222         GroupLocalServiceUtil.addRoleGroups(
223             role.getRoleId(), new long[] {group.getGroupId()});
224     }
225 
226     protected User addUser(
227             long companyId, LDAPUser ldapUser, String password)
228         throws Exception {
229 
230         if (_log.isDebugEnabled()) {
231             _log.debug("Adding user " + ldapUser.getEmailAddress());
232         }
233 
234         Calendar birthdayCal = CalendarFactoryUtil.getCalendar();
235 
236         birthdayCal.setTime(ldapUser.getBirthday());
237 
238         int birthdayMonth = birthdayCal.get(Calendar.MONTH);
239         int birthdayDay = birthdayCal.get(Calendar.DAY_OF_MONTH);
240         int birthdayYear = birthdayCal.get(Calendar.YEAR);
241 
242         return UserLocalServiceUtil.addUser(
243             ldapUser.getCreatorUserId(), companyId, ldapUser.isAutoPassword(),
244             password, password, ldapUser.isAutoScreenName(),
245             ldapUser.getScreenName(), ldapUser.getEmailAddress(),
246             ldapUser.getOpenId(), ldapUser.getLocale(), ldapUser.getFirstName(),
247             ldapUser.getMiddleName(), ldapUser.getLastName(),
248             ldapUser.getPrefixId(), ldapUser.getSuffixId(), ldapUser.isMale(),
249             birthdayMonth, birthdayDay, birthdayYear, ldapUser.getJobTitle(),
250             ldapUser.getGroupIds(), ldapUser.getOrganizationIds(),
251             ldapUser.getRoleIds(), ldapUser.getUserGroupIds(),
252             ldapUser.isSendEmail(), ldapUser.getServiceContext());
253     }
254 
255     protected User getUser(long companyId, LDAPUser ldapUser)
256         throws Exception {
257 
258         User user = null;
259 
260         try {
261             String authType = PrefsPropsUtil.getString(
262                 companyId, PropsKeys.COMPANY_SECURITY_AUTH_TYPE,
263                 PropsValues.COMPANY_SECURITY_AUTH_TYPE);
264 
265             if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
266                 user = UserLocalServiceUtil.getUserByScreenName(
267                     companyId, ldapUser.getScreenName());
268             }
269             else {
270                 user = UserLocalServiceUtil.getUserByEmailAddress(
271                     companyId, ldapUser.getEmailAddress());
272             }
273         }
274         catch (NoSuchUserException nsue) {
275         }
276 
277         return user;
278     }
279 
280     protected Attribute getUsers(
281             long ldapServerId, long companyId, LdapContext ldapContext,
282             Attributes attributes, UserGroup userGroup,
283             Properties groupMappings)
284         throws Exception {
285 
286         Attribute attribute = attributes.get(groupMappings.getProperty("user"));
287 
288         if (attribute == null) {
289             return null;
290         }
291 
292         String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
293 
294         String baseDN = PrefsPropsUtil.getString(
295             companyId, PropsKeys.LDAP_BASE_DN + postfix);
296 
297         StringBundler sb = new StringBundler(7);
298 
299         sb.append("(&");
300         sb.append(
301             PrefsPropsUtil.getString(
302                 companyId,
303                 PropsKeys.LDAP_IMPORT_GROUP_SEARCH_FILTER + postfix));
304         sb.append("(");
305         sb.append(groupMappings.getProperty("groupName"));
306         sb.append("=");
307         sb.append(userGroup.getName());
308         sb.append("))");
309 
310         return PortalLDAPUtil.getMultivaluedAttribute(
311             companyId, ldapContext, baseDN, sb.toString(), attribute);
312     }
313 
314     protected void importFromLDAPByGroup(
315             long ldapServerId, long companyId, LdapContext ldapContext,
316             Properties userMappings, Properties userExpandoMappings,
317             Properties contactMappings, Properties contactExpandoMappings,
318             Properties groupMappings)
319         throws Exception {
320 
321         List<SearchResult> searchResults = PortalLDAPUtil.getGroups(
322             ldapServerId, companyId, ldapContext, 0);
323 
324         for (SearchResult searchResult : searchResults) {
325             try {
326                 Attributes attributes = PortalLDAPUtil.getGroupAttributes(
327                     ldapServerId, companyId, ldapContext,
328                     PortalLDAPUtil.getNameInNamespace(
329                         ldapServerId, companyId, searchResult),
330                     true);
331 
332                 UserGroup userGroup = importUserGroup(
333                     companyId, attributes, groupMappings);
334 
335                 Attribute usersAttribute = getUsers(
336                     ldapServerId, companyId, ldapContext, attributes, userGroup,
337                     groupMappings);
338 
339                 if (usersAttribute == null) {
340                     if (_log.isInfoEnabled()) {
341                         _log.info("No users found in " + userGroup.getName());
342                     }
343 
344                     continue;
345                 }
346 
347                 importUsers(
348                     ldapServerId, companyId, ldapContext, userMappings,
349                     userExpandoMappings, contactMappings,
350                     contactExpandoMappings, userGroup.getUserGroupId(),
351                     usersAttribute);
352             }
353             catch (Exception e) {
354                 _log.error("Unable to import group " + searchResult, e);
355             }
356         }
357     }
358 
359     protected void importFromLDAPByUser(
360             long ldapServerId, long companyId, LdapContext ldapContext,
361             Properties userMappings, Properties userExpandoMappings,
362             Properties contactMappings, Properties contactExpandoMappings,
363             Properties groupMappings)
364         throws Exception {
365 
366         List<SearchResult> searchResults = PortalLDAPUtil.getUsers(
367             ldapServerId, companyId, ldapContext, 0);
368 
369         for (SearchResult searchResult : searchResults) {
370             try {
371                 Attributes userAttributes = PortalLDAPUtil.getUserAttributes(
372                     ldapServerId, companyId, ldapContext,
373                     PortalLDAPUtil.getNameInNamespace(
374                         ldapServerId, companyId, searchResult));
375 
376                 User user = importUser(
377                     companyId, userAttributes, userMappings,
378                     userExpandoMappings, contactMappings,
379                     contactExpandoMappings, StringPool.BLANK);
380 
381                 importGroups(
382                     ldapServerId, companyId, ldapContext, userAttributes, user,
383                     userMappings, groupMappings);
384             }
385             catch (Exception e) {
386                 _log.error("Unable to import user " + searchResult, e);
387             }
388         }
389     }
390 
391     protected void importGroups(
392             long ldapServerId, long companyId, LdapContext ldapContext,
393             Attributes attributes, User user, Properties userMappings,
394             Properties groupMappings)
395         throws Exception {
396 
397         String userMappingsGroup = userMappings.getProperty("group");
398 
399         if (Validator.isNull(userMappingsGroup)) {
400             return;
401         }
402 
403         Attribute attribute = attributes.get(userMappingsGroup);
404 
405         if (attribute == null) {
406             return;
407         }
408 
409         attribute.clear();
410 
411         String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
412 
413         String baseDN = PrefsPropsUtil.getString(
414             companyId, PropsKeys.LDAP_BASE_DN + postfix);
415 
416         Binding binding = PortalLDAPUtil.getUser(
417             ldapServerId, companyId, user.getScreenName());
418 
419         String fullUserDN = PortalLDAPUtil.getNameInNamespace(
420             ldapServerId, companyId, binding);
421 
422         StringBundler sb = new StringBundler(9);
423 
424         sb.append(StringPool.OPEN_PARENTHESIS);
425         sb.append(StringPool.AMPERSAND);
426         sb.append(
427             PrefsPropsUtil.getString(
428                 companyId,
429                 PropsKeys.LDAP_IMPORT_GROUP_SEARCH_FILTER + postfix));
430         sb.append(StringPool.OPEN_PARENTHESIS);
431         sb.append(groupMappings.getProperty("user"));
432         sb.append(StringPool.EQUAL);
433         sb.append(fullUserDN);
434         sb.append(StringPool.CLOSE_PARENTHESIS);
435         sb.append(StringPool.CLOSE_PARENTHESIS);
436 
437         List<SearchResult> searchResults = PortalLDAPUtil.searchLDAP(
438             companyId, ldapContext, 0, baseDN, sb.toString(), null);
439 
440         for (SearchResult searchResult : searchResults) {
441             String fullGroupDN = PortalLDAPUtil.getNameInNamespace(
442                 ldapServerId, companyId, searchResult);
443 
444             attribute.add(fullGroupDN);
445         }
446 
447         List<Long> newUserGroupIds = new ArrayList<Long>(attribute.size());
448 
449         for (int i = 0; i < attribute.size(); i++) {
450             String fullGroupDN = (String) attribute.get(i);
451 
452             Attributes groupAttributes = null;
453 
454             try {
455                 groupAttributes = PortalLDAPUtil.getGroupAttributes(
456                     ldapServerId, companyId, ldapContext, fullGroupDN);
457             }
458             catch (NameNotFoundException nnfe) {
459                 _log.error(
460                     "LDAP group not found with fullGroupDN " + fullGroupDN,
461                     nnfe);
462 
463                 continue;
464             }
465 
466             UserGroup userGroup = importUserGroup(
467                 companyId, groupAttributes, groupMappings);
468 
469             if (userGroup != null) {
470                 if (_log.isDebugEnabled()) {
471                     _log.debug(
472                         "Adding " + user.getUserId() + " to group " +
473                             userGroup.getUserGroupId());
474                 }
475 
476                 newUserGroupIds.add(userGroup.getUserGroupId());
477             }
478         }
479 
480         UserGroupLocalServiceUtil.setUserUserGroups(
481             user.getUserId(),
482             ArrayUtil.toArray(
483                 newUserGroupIds.toArray(new Long[newUserGroupIds.size()])));
484     }
485 
486     protected User importUser(
487             long companyId, Attributes attributes, Properties userMappings,
488             Properties userExpandoMappings, Properties contactMappings,
489             Properties contactExpandoMappings,
490             String password)
491         throws Exception {
492 
493         LDAPUserTransactionThreadLocal.setOriginatesFromLDAP(true);
494 
495         try {
496             AttributesTransformer attributesTransformer =
497                 AttributesTransformerFactory.getInstance();
498 
499             attributes = attributesTransformer.transformUser(attributes);
500 
501             LDAPUser ldapUser = _ldapToPortalConverter.importLDAPUser(
502                 companyId, attributes, userMappings, userExpandoMappings,
503                 contactMappings, contactExpandoMappings, password);
504 
505             User user = getUser(companyId, ldapUser);
506 
507             if ((user != null) && user.isDefaultUser()) {
508                 return user;
509             }
510 
511             if (user != null) {
512 
513                 // User already exists in the Liferay database. Skip import if
514                 // user fields have been already synced, if import is part of a
515                 // scheduled import, or if the LDAP entry has never been
516                 // modified.
517 
518                 String modifiedDate = LDAPUtil.getAttributeValue(
519                     attributes, "modifyTimestamp");
520 
521                 user = updateUser(
522                     companyId, ldapUser, user, password, modifiedDate);
523             }
524             else {
525                 user = addUser(companyId, ldapUser, password);
526             }
527 
528             updateExpandoAttributes(user, ldapUser);
529 
530             return user;
531         }
532         finally {
533             LDAPUserTransactionThreadLocal.setOriginatesFromLDAP(false);
534         }
535     }
536 
537     protected UserGroup importUserGroup(
538             long companyId, Attributes attributes, Properties groupMappings)
539         throws Exception {
540 
541         AttributesTransformer attributesTransformer =
542             AttributesTransformerFactory.getInstance();
543 
544         attributes = attributesTransformer.transformGroup(attributes);
545 
546         LDAPGroup ldapGroup = _ldapToPortalConverter.importLDAPGroup(
547             companyId, attributes, groupMappings);
548 
549         UserGroup userGroup = null;
550 
551         try {
552             userGroup = UserGroupLocalServiceUtil.getUserGroup(
553                 companyId, ldapGroup.getGroupName());
554 
555             UserGroupLocalServiceUtil.updateUserGroup(
556                 companyId, userGroup.getUserGroupId(), ldapGroup.getGroupName(),
557                 ldapGroup.getDescription());
558         }
559         catch (NoSuchUserGroupException nsuge) {
560             if (_log.isDebugEnabled()) {
561                 _log.debug(
562                     "Adding user group to portal " + ldapGroup.getGroupName());
563             }
564 
565             long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
566                 companyId);
567 
568             try {
569                 userGroup = UserGroupLocalServiceUtil.addUserGroup(
570                     defaultUserId, companyId, ldapGroup.getGroupName(),
571                     ldapGroup.getDescription());
572             }
573             catch (Exception e) {
574                 if (_log.isWarnEnabled()) {
575                     _log.warn(
576                         "Unable to create user group " +
577                             ldapGroup.getGroupName());
578                 }
579 
580                 if (_log.isDebugEnabled()) {
581                     _log.debug(e, e);
582                 }
583             }
584         }
585 
586         addRole(companyId, ldapGroup, userGroup);
587 
588         return userGroup;
589     }
590 
591     protected void importUsers(
592             long ldapServerId, long companyId, LdapContext ldapContext,
593             Properties userMappings, Properties userExpandoMappings,
594             Properties contactMappings, Properties contactExpandoMappings,
595             long userGroupId, Attribute attribute)
596         throws Exception {
597 
598         List<Long> newUserIds = new ArrayList<Long>(attribute.size());
599 
600         for (int i = 0; i < attribute.size(); i++) {
601             String fullUserDN = (String)attribute.get(i);
602 
603             Attributes userAttributes = null;
604 
605             try {
606                 userAttributes = PortalLDAPUtil.getUserAttributes(
607                     ldapServerId, companyId, ldapContext, fullUserDN);
608             }
609             catch (NameNotFoundException nnfe) {
610                 _log.error(
611                     "LDAP user not found with fullUserDN " + fullUserDN, nnfe);
612 
613                 continue;
614             }
615 
616             try {
617                 User user = importUser(
618                     companyId, userAttributes, userMappings,
619                     userExpandoMappings, contactMappings,
620                     contactExpandoMappings, StringPool.BLANK);
621 
622                 if (user != null) {
623                     if (_log.isDebugEnabled()) {
624                         _log.debug(
625                             "Adding " + user.getUserId() + " to group " +
626                                 userGroupId);
627                     }
628 
629                     newUserIds.add(user.getUserId());
630                 }
631             }
632             catch (Exception e) {
633                 _log.error("Unable to load user " + userAttributes, e);
634             }
635         }
636 
637         UserLocalServiceUtil.setUserGroupUsers(
638             userGroupId,
639             ArrayUtil.toArray(newUserIds.toArray(new Long[newUserIds.size()])));
640     }
641 
642     protected void populateExpandoAttributes(
643         ExpandoBridge expandoBridge, Map<String, String> expandoAttributes) {
644 
645         for (Map.Entry<String, String> expandoAttribute :
646                 expandoAttributes.entrySet()) {
647 
648             String name = expandoAttribute.getKey();
649 
650             if (!expandoBridge.hasAttribute(name)) {
651                 continue;
652             }
653 
654             int type = expandoBridge.getAttributeType(name);
655 
656             Serializable value = ExpandoConverterUtil.getAttributeFromString(
657                 type, expandoAttribute.getValue());
658 
659             try {
660                 ExpandoValueLocalServiceUtil.addValue(
661                     expandoBridge.getCompanyId(), expandoBridge.getClassName(),
662                     ExpandoTableConstants.DEFAULT_TABLE_NAME, name,
663                     expandoBridge.getClassPK(), value);
664             }
665             catch (Exception e) {
666                 _log.error(e, e);
667             }
668         }
669     }
670 
671     protected void updateExpandoAttributes(User user, LDAPUser ldapUser)
672         throws Exception {
673 
674         ExpandoBridge userExpandoBridge = user.getExpandoBridge();
675 
676         populateExpandoAttributes(
677             userExpandoBridge, ldapUser.getUserExpandoAttributes());
678 
679         Contact contact = user.getContact();
680 
681         ExpandoBridge contactExpandoBridge = contact.getExpandoBridge();
682 
683         populateExpandoAttributes(
684             contactExpandoBridge , ldapUser.getContactExpandoAttributes());
685     }
686 
687     protected User updateUser(
688             long companyId, LDAPUser ldapUser, User user, String password,
689             String modifiedDate)
690         throws Exception {
691 
692         Date ldapUserModifiedDate = null;
693 
694         try {
695             if (Validator.isNull(modifiedDate)) {
696                 if (_log.isInfoEnabled()) {
697                     _log.info(
698                         "LDAP entry never modified, skipping user " +
699                             user.getEmailAddress());
700                 }
701 
702                 return user;
703             }
704             else {
705                 DateFormat dateFormat =
706                     DateFormatFactoryUtil.getSimpleDateFormat(
707                         "yyyyMMddHHmmss");
708 
709                 ldapUserModifiedDate = dateFormat.parse(modifiedDate);
710             }
711 
712             if (ldapUserModifiedDate.equals(user.getModifiedDate()) &&
713                 ldapUser.isAutoPassword()) {
714 
715                 if (_log.isDebugEnabled()) {
716                     _log.debug(
717                         "User is already synchronized, skipping user " +
718                             user.getEmailAddress());
719                 }
720 
721                 return user;
722             }
723         }
724         catch (ParseException pe) {
725             if (_log.isDebugEnabled()) {
726                 _log.debug(
727                     "Unable to parse LDAP modify timestamp " + modifiedDate,
728                     pe);
729             }
730         }
731 
732         if (Validator.isNull(ldapUser.getScreenName())) {
733             ldapUser.setAutoScreenName(true);
734         }
735 
736         if (ldapUser.isAutoScreenName()) {
737             ScreenNameGenerator screenNameGenerator =
738                 ScreenNameGeneratorFactory.getInstance();
739 
740             ldapUser.setScreenName(
741                 screenNameGenerator.generate(
742                     companyId, user.getUserId(), ldapUser.getEmailAddress()));
743         }
744 
745         Calendar birthdayCal = CalendarFactoryUtil.getCalendar();
746 
747         birthdayCal.setTime(user.getContact().getBirthday());
748 
749         int birthdayMonth = birthdayCal.get(Calendar.MONTH);
750         int birthdayDay = birthdayCal.get(Calendar.DAY_OF_MONTH);
751         int birthdayYear = birthdayCal.get(Calendar.YEAR);
752 
753         if (ldapUser.isUpdatePassword()) {
754             UserLocalServiceUtil.updatePassword(
755                 user.getUserId(), password, password,
756                 ldapUser.isPasswordReset(), true);
757         }
758 
759         user = UserLocalServiceUtil.updateUser(
760             user.getUserId(), password, StringPool.BLANK, StringPool.BLANK,
761             ldapUser.isPasswordReset(), ldapUser.getReminderQueryQuestion(),
762             ldapUser.getReminderQueryAnswer(), ldapUser.getScreenName(),
763             ldapUser.getEmailAddress(), ldapUser.getOpenId(),
764             ldapUser.getLanguageId(), ldapUser.getTimeZoneId(),
765             ldapUser.getGreeting(), ldapUser.getComments(),
766             ldapUser.getFirstName(), ldapUser.getMiddleName(),
767             ldapUser.getLastName(), ldapUser.getPrefixId(),
768             ldapUser.getSuffixId(), ldapUser.isMale(), birthdayMonth,
769             birthdayDay, birthdayYear, ldapUser.getSmsSn(), ldapUser.getAimSn(),
770             ldapUser.getFacebookSn(), ldapUser.getIcqSn(),
771             ldapUser.getJabberSn(), ldapUser.getMsnSn(),
772             ldapUser.getMySpaceSn(), ldapUser.getSkypeSn(),
773             ldapUser.getTwitterSn(), ldapUser.getYmSn(), ldapUser.getJobTitle(),
774             ldapUser.getGroupIds(), ldapUser.getOrganizationIds(),
775             ldapUser.getRoleIds(), ldapUser.getUserGroupRoles(),
776             ldapUser.getUserGroupIds(), ldapUser.getServiceContext());
777 
778         if (ldapUserModifiedDate != null) {
779             user = UserLocalServiceUtil.updateModifiedDate(
780                 user.getUserId(), ldapUserModifiedDate);
781         }
782 
783         return user;
784     }
785 
786     private static final String _IMPORT_BY_GROUP = "group";
787 
788     private static final String _IMPORT_BY_USER = "user";
789 
790     private static Log _log = LogFactoryUtil.getLog(
791         PortalLDAPImporterImpl.class);
792 
793     private LDAPToPortalConverter _ldapToPortalConverter;
794 
795 }