001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.CompanyMaxUsersException;
018 import com.liferay.portal.ContactBirthdayException;
019 import com.liferay.portal.ContactFirstNameException;
020 import com.liferay.portal.ContactFullNameException;
021 import com.liferay.portal.ContactLastNameException;
022 import com.liferay.portal.DuplicateOpenIdException;
023 import com.liferay.portal.GroupFriendlyURLException;
024 import com.liferay.portal.ModelListenerException;
025 import com.liferay.portal.NoSuchImageException;
026 import com.liferay.portal.NoSuchOrganizationException;
027 import com.liferay.portal.NoSuchRoleException;
028 import com.liferay.portal.NoSuchTicketException;
029 import com.liferay.portal.NoSuchUserException;
030 import com.liferay.portal.NoSuchUserGroupException;
031 import com.liferay.portal.PasswordExpiredException;
032 import com.liferay.portal.RequiredUserException;
033 import com.liferay.portal.ReservedUserEmailAddressException;
034 import com.liferay.portal.ReservedUserScreenNameException;
035 import com.liferay.portal.SendPasswordException;
036 import com.liferay.portal.UserEmailAddressException;
037 import com.liferay.portal.UserIdException;
038 import com.liferay.portal.UserLockoutException;
039 import com.liferay.portal.UserPasswordException;
040 import com.liferay.portal.UserReminderQueryException;
041 import com.liferay.portal.UserScreenNameException;
042 import com.liferay.portal.UserSmsException;
043 import com.liferay.portal.kernel.cache.PortalCache;
044 import com.liferay.portal.kernel.cache.PortalCacheMapSynchronizeUtil;
045 import com.liferay.portal.kernel.cache.PortalCacheMapSynchronizeUtil.Synchronizer;
046 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
047 import com.liferay.portal.kernel.dao.orm.QueryUtil;
048 import com.liferay.portal.kernel.dao.orm.WildcardMode;
049 import com.liferay.portal.kernel.dao.shard.ShardCallable;
050 import com.liferay.portal.kernel.exception.PortalException;
051 import com.liferay.portal.kernel.exception.SystemException;
052 import com.liferay.portal.kernel.language.LanguageUtil;
053 import com.liferay.portal.kernel.log.Log;
054 import com.liferay.portal.kernel.log.LogFactoryUtil;
055 import com.liferay.portal.kernel.messaging.DestinationNames;
056 import com.liferay.portal.kernel.messaging.Message;
057 import com.liferay.portal.kernel.messaging.MessageBusUtil;
058 import com.liferay.portal.kernel.search.BaseModelSearchResult;
059 import com.liferay.portal.kernel.search.Hits;
060 import com.liferay.portal.kernel.search.Indexer;
061 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
062 import com.liferay.portal.kernel.search.QueryConfig;
063 import com.liferay.portal.kernel.search.SearchContext;
064 import com.liferay.portal.kernel.search.SearchException;
065 import com.liferay.portal.kernel.search.Sort;
066 import com.liferay.portal.kernel.spring.aop.Skip;
067 import com.liferay.portal.kernel.transaction.Propagation;
068 import com.liferay.portal.kernel.transaction.TransactionCommitCallbackRegistryUtil;
069 import com.liferay.portal.kernel.transaction.Transactional;
070 import com.liferay.portal.kernel.util.ArrayUtil;
071 import com.liferay.portal.kernel.util.CharPool;
072 import com.liferay.portal.kernel.util.Digester;
073 import com.liferay.portal.kernel.util.DigesterUtil;
074 import com.liferay.portal.kernel.util.GetterUtil;
075 import com.liferay.portal.kernel.util.KeyValuePair;
076 import com.liferay.portal.kernel.util.ListUtil;
077 import com.liferay.portal.kernel.util.LocaleUtil;
078 import com.liferay.portal.kernel.util.LocalizationUtil;
079 import com.liferay.portal.kernel.util.MapUtil;
080 import com.liferay.portal.kernel.util.OrderByComparator;
081 import com.liferay.portal.kernel.util.ParamUtil;
082 import com.liferay.portal.kernel.util.PropsKeys;
083 import com.liferay.portal.kernel.util.StringBundler;
084 import com.liferay.portal.kernel.util.StringPool;
085 import com.liferay.portal.kernel.util.StringUtil;
086 import com.liferay.portal.kernel.util.UnicodeProperties;
087 import com.liferay.portal.kernel.util.Validator;
088 import com.liferay.portal.kernel.workflow.WorkflowConstants;
089 import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
090 import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
091 import com.liferay.portal.model.Account;
092 import com.liferay.portal.model.Company;
093 import com.liferay.portal.model.CompanyConstants;
094 import com.liferay.portal.model.Contact;
095 import com.liferay.portal.model.ContactConstants;
096 import com.liferay.portal.model.Group;
097 import com.liferay.portal.model.GroupConstants;
098 import com.liferay.portal.model.Layout;
099 import com.liferay.portal.model.Organization;
100 import com.liferay.portal.model.PasswordPolicy;
101 import com.liferay.portal.model.ResourceConstants;
102 import com.liferay.portal.model.Role;
103 import com.liferay.portal.model.RoleConstants;
104 import com.liferay.portal.model.Team;
105 import com.liferay.portal.model.Ticket;
106 import com.liferay.portal.model.TicketConstants;
107 import com.liferay.portal.model.User;
108 import com.liferay.portal.model.UserGroup;
109 import com.liferay.portal.model.UserGroupRole;
110 import com.liferay.portal.model.impl.LayoutImpl;
111 import com.liferay.portal.model.impl.UserCacheModel;
112 import com.liferay.portal.model.impl.UserImpl;
113 import com.liferay.portal.security.auth.AuthPipeline;
114 import com.liferay.portal.security.auth.Authenticator;
115 import com.liferay.portal.security.auth.EmailAddressGenerator;
116 import com.liferay.portal.security.auth.EmailAddressGeneratorFactory;
117 import com.liferay.portal.security.auth.EmailAddressValidator;
118 import com.liferay.portal.security.auth.EmailAddressValidatorFactory;
119 import com.liferay.portal.security.auth.FullNameGenerator;
120 import com.liferay.portal.security.auth.FullNameGeneratorFactory;
121 import com.liferay.portal.security.auth.FullNameValidator;
122 import com.liferay.portal.security.auth.FullNameValidatorFactory;
123 import com.liferay.portal.security.auth.PrincipalException;
124 import com.liferay.portal.security.auth.ScreenNameGenerator;
125 import com.liferay.portal.security.auth.ScreenNameGeneratorFactory;
126 import com.liferay.portal.security.auth.ScreenNameValidator;
127 import com.liferay.portal.security.auth.ScreenNameValidatorFactory;
128 import com.liferay.portal.security.ldap.LDAPSettingsUtil;
129 import com.liferay.portal.security.permission.PermissionCacheUtil;
130 import com.liferay.portal.security.pwd.PasswordEncryptorUtil;
131 import com.liferay.portal.security.pwd.PwdAuthenticator;
132 import com.liferay.portal.security.pwd.PwdToolkitUtil;
133 import com.liferay.portal.security.pwd.RegExpToolkit;
134 import com.liferay.portal.service.BaseServiceImpl;
135 import com.liferay.portal.service.ServiceContext;
136 import com.liferay.portal.service.ServiceContextThreadLocal;
137 import com.liferay.portal.service.base.UserLocalServiceBaseImpl;
138 import com.liferay.portal.util.PortalUtil;
139 import com.liferay.portal.util.PrefsPropsUtil;
140 import com.liferay.portal.util.PropsValues;
141 import com.liferay.portal.util.SubscriptionSender;
142 import com.liferay.portlet.messageboards.model.MBMessage;
143 import com.liferay.portlet.social.model.SocialRelation;
144 import com.liferay.portlet.social.model.SocialRelationConstants;
145 import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
146 import com.liferay.util.Encryptor;
147 import com.liferay.util.EncryptorException;
148 import com.liferay.util.PwdGenerator;
149
150 import java.io.Serializable;
151
152 import java.util.ArrayList;
153 import java.util.Arrays;
154 import java.util.Calendar;
155 import java.util.Date;
156 import java.util.HashMap;
157 import java.util.HashSet;
158 import java.util.LinkedHashMap;
159 import java.util.List;
160 import java.util.Locale;
161 import java.util.Map;
162 import java.util.Set;
163 import java.util.concurrent.Callable;
164 import java.util.concurrent.ConcurrentHashMap;
165
166 import javax.portlet.PortletPreferences;
167
168
180 public class UserLocalServiceImpl extends UserLocalServiceBaseImpl {
181
182
195 @Override
196 public User addDefaultAdminUser(
197 long companyId, String screenName, String emailAddress,
198 Locale locale, String firstName, String middleName, String lastName)
199 throws PortalException {
200
201 long creatorUserId = 0;
202 boolean autoPassword = false;
203 String password1 = PropsValues.DEFAULT_ADMIN_PASSWORD;
204 String password2 = password1;
205 boolean autoScreenName = false;
206
207 screenName = getLogin(screenName);
208
209 for (int i = 1;; i++) {
210 User screenNameUser = userPersistence.fetchByC_SN(
211 companyId, screenName);
212
213 if (screenNameUser == null) {
214 break;
215 }
216
217 screenName = screenName + i;
218 }
219
220 long facebookId = 0;
221 String openId = StringPool.BLANK;
222 int prefixId = 0;
223 int suffixId = 0;
224 boolean male = true;
225 int birthdayMonth = Calendar.JANUARY;
226 int birthdayDay = 1;
227 int birthdayYear = 1970;
228 String jobTitle = StringPool.BLANK;
229
230 Group guestGroup = groupLocalService.getGroup(
231 companyId, GroupConstants.GUEST);
232
233 long[] groupIds = {guestGroup.getGroupId()};
234
235 long[] organizationIds = null;
236
237 Role adminRole = roleLocalService.getRole(
238 companyId, RoleConstants.ADMINISTRATOR);
239
240 Role powerUserRole = roleLocalService.getRole(
241 companyId, RoleConstants.POWER_USER);
242
243 long[] roleIds = {adminRole.getRoleId(), powerUserRole.getRoleId()};
244
245 long[] userGroupIds = null;
246 boolean sendEmail = false;
247 ServiceContext serviceContext = new ServiceContext();
248
249 User defaultAdminUser = addUser(
250 creatorUserId, companyId, autoPassword, password1, password2,
251 autoScreenName, screenName, emailAddress, facebookId, openId,
252 locale, firstName, middleName, lastName, prefixId, suffixId, male,
253 birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
254 organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
255
256 updateEmailAddressVerified(defaultAdminUser.getUserId(), true);
257
258 updateLastLogin(
259 defaultAdminUser.getUserId(), defaultAdminUser.getLoginIP());
260
261 updatePasswordReset(defaultAdminUser.getUserId(), false);
262
263 return defaultAdminUser;
264 }
265
266
275 @Override
276 public void addDefaultGroups(long userId) throws PortalException {
277 User user = userPersistence.findByPrimaryKey(userId);
278
279 Set<Long> groupIdsSet = new HashSet<Long>();
280
281 String[] defaultGroupNames = PrefsPropsUtil.getStringArray(
282 user.getCompanyId(), PropsKeys.ADMIN_DEFAULT_GROUP_NAMES,
283 StringPool.NEW_LINE, PropsValues.ADMIN_DEFAULT_GROUP_NAMES);
284
285 for (String defaultGroupName : defaultGroupNames) {
286 Company company = companyPersistence.findByPrimaryKey(
287 user.getCompanyId());
288
289 Account account = company.getAccount();
290
291 if (StringUtil.equalsIgnoreCase(
292 defaultGroupName, account.getName())) {
293
294 defaultGroupName = GroupConstants.GUEST;
295 }
296
297 Group group = groupPersistence.fetchByC_N(
298 user.getCompanyId(), defaultGroupName);
299
300 if ((group != null) &&
301 !userPersistence.containsGroup(
302 userId, group.getGroupId())) {
303
304 groupIdsSet.add(group.getGroupId());
305 }
306 }
307
308 String[] defaultOrganizationGroupNames = PrefsPropsUtil.getStringArray(
309 user.getCompanyId(),
310 PropsKeys.ADMIN_DEFAULT_ORGANIZATION_GROUP_NAMES,
311 StringPool.NEW_LINE,
312 PropsValues.ADMIN_DEFAULT_ORGANIZATION_GROUP_NAMES);
313
314 for (String defaultOrganizationGroupName :
315 defaultOrganizationGroupNames) {
316
317 defaultOrganizationGroupName +=
318 GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX;
319
320 Group group = groupPersistence.fetchByC_N(
321 user.getCompanyId(), defaultOrganizationGroupName);
322
323 if ((group != null) &&
324 !userPersistence.containsGroup(
325 userId, group.getGroupId())) {
326
327 groupIdsSet.add(group.getGroupId());
328 }
329 }
330
331 long[] groupIds = ArrayUtil.toArray(
332 groupIdsSet.toArray(new Long[groupIdsSet.size()]));
333
334 groupLocalService.addUserGroups(userId, groupIds);
335 }
336
337
346 @Override
347 public void addDefaultRoles(long userId) throws PortalException {
348 User user = userPersistence.findByPrimaryKey(userId);
349
350 Set<Long> roleIdSet = new HashSet<Long>();
351
352 String[] defaultRoleNames = PrefsPropsUtil.getStringArray(
353 user.getCompanyId(), PropsKeys.ADMIN_DEFAULT_ROLE_NAMES,
354 StringPool.NEW_LINE, PropsValues.ADMIN_DEFAULT_ROLE_NAMES);
355
356 for (String defaultRoleName : defaultRoleNames) {
357 try {
358 Role role = rolePersistence.findByC_N(
359 user.getCompanyId(), defaultRoleName);
360
361 if (!userPersistence.containsRole(userId, role.getRoleId())) {
362 roleIdSet.add(role.getRoleId());
363 }
364 }
365 catch (NoSuchRoleException nsre) {
366 }
367 }
368
369 long[] roleIds = ArrayUtil.toArray(
370 roleIdSet.toArray(new Long[roleIdSet.size()]));
371
372 roleIds = UsersAdminUtil.addRequiredRoles(user, roleIds);
373
374 userPersistence.addRoles(userId, roleIds);
375 }
376
377
386 @Override
387 @SuppressWarnings("deprecation")
388 public void addDefaultUserGroups(long userId) throws PortalException {
389 User user = userPersistence.findByPrimaryKey(userId);
390
391 Set<Long> userGroupIdSet = new HashSet<Long>();
392
393 String[] defaultUserGroupNames = PrefsPropsUtil.getStringArray(
394 user.getCompanyId(), PropsKeys.ADMIN_DEFAULT_USER_GROUP_NAMES,
395 StringPool.NEW_LINE, PropsValues.ADMIN_DEFAULT_USER_GROUP_NAMES);
396
397 for (String defaultUserGroupName : defaultUserGroupNames) {
398 try {
399 UserGroup userGroup = userGroupPersistence.findByC_N(
400 user.getCompanyId(), defaultUserGroupName);
401
402 if (!userPersistence.containsUserGroup(
403 userId, userGroup.getUserGroupId())) {
404
405 userGroupIdSet.add(userGroup.getUserGroupId());
406 }
407 }
408 catch (NoSuchUserGroupException nsuge) {
409 }
410 }
411
412 long[] userGroupIds = ArrayUtil.toArray(
413 userGroupIdSet.toArray(new Long[userGroupIdSet.size()]));
414
415 if (PropsValues.USER_GROUPS_COPY_LAYOUTS_TO_USER_PERSONAL_SITE) {
416 for (long userGroupId : userGroupIds) {
417 userGroupLocalService.copyUserGroupLayouts(userGroupId, userId);
418 }
419 }
420
421 userPersistence.addUserGroups(userId, userGroupIds);
422 }
423
424
432 @Override
433 public void addGroupUsers(long groupId, long[] userIds)
434 throws PortalException {
435
436 groupPersistence.addUsers(groupId, userIds);
437
438 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
439
440 indexer.reindex(userIds);
441
442 PermissionCacheUtil.clearCache();
443
444 addDefaultRolesAndTeams(groupId, userIds);
445 }
446
447
455 @Override
456 public void addOrganizationUsers(long organizationId, long[] userIds)
457 throws PortalException {
458
459 organizationPersistence.addUsers(organizationId, userIds);
460
461 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
462
463 indexer.reindex(userIds);
464
465 PermissionCacheUtil.clearCache();
466 }
467
468
475 @Override
476 public void addPasswordPolicyUsers(long passwordPolicyId, long[] userIds) {
477 passwordPolicyRelLocalService.addPasswordPolicyRels(
478 passwordPolicyId, User.class.getName(), userIds);
479 }
480
481
489 @Override
490 public void addRoleUsers(long roleId, long[] userIds)
491 throws PortalException {
492
493 rolePersistence.addUsers(roleId, userIds);
494
495 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
496
497 indexer.reindex(userIds);
498
499 PermissionCacheUtil.clearCache();
500 }
501
502
510 @Override
511 public void addTeamUsers(long teamId, long[] userIds)
512 throws PortalException {
513
514 teamPersistence.addUsers(teamId, userIds);
515
516 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
517
518 indexer.reindex(userIds);
519
520 PermissionCacheUtil.clearCache();
521 }
522
523
570 @Override
571 public User addUser(
572 long creatorUserId, long companyId, boolean autoPassword,
573 String password1, String password2, boolean autoScreenName,
574 String screenName, String emailAddress, long facebookId,
575 String openId, Locale locale, String firstName, String middleName,
576 String lastName, int prefixId, int suffixId, boolean male,
577 int birthdayMonth, int birthdayDay, int birthdayYear,
578 String jobTitle, long[] groupIds, long[] organizationIds,
579 long[] roleIds, long[] userGroupIds, boolean sendEmail,
580 ServiceContext serviceContext)
581 throws PortalException {
582
583 boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
584
585 try {
586 WorkflowThreadLocal.setEnabled(false);
587
588 if (serviceContext == null) {
589 serviceContext = new ServiceContext();
590 }
591
592 if (serviceContext.getWorkflowAction() !=
593 WorkflowConstants.ACTION_PUBLISH) {
594
595 serviceContext.setWorkflowAction(
596 WorkflowConstants.ACTION_PUBLISH);
597 }
598
599 return addUserWithWorkflow(
600 creatorUserId, companyId, autoPassword, password1, password2,
601 autoScreenName, screenName, emailAddress, facebookId, openId,
602 locale, firstName, middleName, lastName, prefixId, suffixId,
603 male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
604 groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
605 serviceContext);
606 }
607 finally {
608 WorkflowThreadLocal.setEnabled(workflowEnabled);
609 }
610 }
611
612
620 @Override
621 @SuppressWarnings("deprecation")
622 public void addUserGroupUsers(long userGroupId, long[] userIds)
623 throws PortalException {
624
625 if (PropsValues.USER_GROUPS_COPY_LAYOUTS_TO_USER_PERSONAL_SITE) {
626 userGroupLocalService.copyUserGroupLayouts(userGroupId, userIds);
627 }
628
629 userGroupPersistence.addUsers(userGroupId, userIds);
630
631 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
632
633 indexer.reindex(userIds);
634
635 PermissionCacheUtil.clearCache();
636 }
637
638
685 @Override
686 @SuppressWarnings("deprecation")
687 public User addUserWithWorkflow(
688 long creatorUserId, long companyId, boolean autoPassword,
689 String password1, String password2, boolean autoScreenName,
690 String screenName, String emailAddress, long facebookId,
691 String openId, Locale locale, String firstName, String middleName,
692 String lastName, int prefixId, int suffixId, boolean male,
693 int birthdayMonth, int birthdayDay, int birthdayYear,
694 String jobTitle, long[] groupIds, long[] organizationIds,
695 long[] roleIds, long[] userGroupIds, boolean sendEmail,
696 ServiceContext serviceContext)
697 throws PortalException {
698
699
700
701 Company company = companyPersistence.findByPrimaryKey(companyId);
702 screenName = getLogin(screenName);
703 openId = StringUtil.trim(openId);
704 Date now = new Date();
705
706 if (PrefsPropsUtil.getBoolean(
707 companyId, PropsKeys.USERS_SCREEN_NAME_ALWAYS_AUTOGENERATE)) {
708
709 autoScreenName = true;
710 }
711
712
713
714 long userId = counterLocalService.increment();
715
716 EmailAddressGenerator emailAddressGenerator =
717 EmailAddressGeneratorFactory.getInstance();
718
719 if ((emailAddress == null) ||
720 emailAddressGenerator.isGenerated(emailAddress)) {
721
722 emailAddress = StringPool.BLANK;
723 }
724 else {
725 emailAddress = StringUtil.toLowerCase(emailAddress.trim());
726 }
727
728 if (!PrefsPropsUtil.getBoolean(
729 companyId, PropsKeys.USERS_EMAIL_ADDRESS_REQUIRED) &&
730 Validator.isNull(emailAddress)) {
731
732 emailAddress = emailAddressGenerator.generate(companyId, userId);
733 }
734
735 validate(
736 companyId, userId, autoPassword, password1, password2,
737 autoScreenName, screenName, emailAddress, openId, firstName,
738 middleName, lastName, organizationIds);
739
740 if (!autoPassword) {
741 if (Validator.isNull(password1) || Validator.isNull(password2)) {
742 throw new UserPasswordException(
743 UserPasswordException.PASSWORD_INVALID);
744 }
745 }
746
747 if (autoScreenName) {
748 ScreenNameGenerator screenNameGenerator =
749 ScreenNameGeneratorFactory.getInstance();
750
751 try {
752 screenName = screenNameGenerator.generate(
753 companyId, userId, emailAddress);
754 }
755 catch (Exception e) {
756 throw new SystemException(e);
757 }
758 }
759
760 User defaultUser = getDefaultUser(companyId);
761
762 FullNameGenerator fullNameGenerator =
763 FullNameGeneratorFactory.getInstance();
764
765 String fullName = fullNameGenerator.getFullName(
766 firstName, middleName, lastName);
767
768 String greeting = LanguageUtil.format(
769 locale, "welcome-x", fullName, false);
770
771 User user = userPersistence.create(userId);
772
773 if (serviceContext != null) {
774 String uuid = serviceContext.getUuid();
775
776 if (Validator.isNotNull(uuid)) {
777 user.setUuid(uuid);
778 }
779 }
780
781 user.setCompanyId(companyId);
782 user.setCreateDate(now);
783 user.setModifiedDate(now);
784 user.setDefaultUser(false);
785 user.setContactId(counterLocalService.increment());
786
787 if (Validator.isNotNull(password1)) {
788 user.setPassword(PasswordEncryptorUtil.encrypt(password1));
789 user.setPasswordUnencrypted(password1);
790 }
791
792 user.setPasswordEncrypted(true);
793
794 PasswordPolicy passwordPolicy = defaultUser.getPasswordPolicy();
795
796 if ((passwordPolicy != null) && passwordPolicy.isChangeable() &&
797 passwordPolicy.isChangeRequired()) {
798
799 user.setPasswordReset(true);
800 }
801 else {
802 user.setPasswordReset(false);
803 }
804
805 user.setDigest(StringPool.BLANK);
806 user.setScreenName(screenName);
807 user.setEmailAddress(emailAddress);
808 user.setFacebookId(facebookId);
809
810 Long ldapServerId = null;
811
812 if (serviceContext != null) {
813 ldapServerId = (Long)serviceContext.getAttribute("ldapServerId");
814 }
815
816 if (ldapServerId != null) {
817 user.setLdapServerId(ldapServerId);
818 }
819 else {
820 user.setLdapServerId(-1);
821 }
822
823 user.setOpenId(openId);
824 user.setLanguageId(LocaleUtil.toLanguageId(locale));
825 user.setTimeZoneId(defaultUser.getTimeZoneId());
826 user.setGreeting(greeting);
827 user.setFirstName(firstName);
828 user.setMiddleName(middleName);
829 user.setLastName(lastName);
830 user.setJobTitle(jobTitle);
831 user.setStatus(WorkflowConstants.STATUS_DRAFT);
832 user.setExpandoBridgeAttributes(serviceContext);
833
834 userPersistence.update(user, serviceContext);
835
836
837
838 String creatorUserName = StringPool.BLANK;
839
840 if (creatorUserId <= 0) {
841 creatorUserId = user.getUserId();
842
843
844
845
846
847 }
848 else {
849 User creatorUser = userPersistence.findByPrimaryKey(creatorUserId);
850
851 creatorUserName = creatorUser.getFullName();
852 }
853
854 Date birthday = getBirthday(birthdayMonth, birthdayDay, birthdayYear);
855
856 Contact contact = contactPersistence.create(user.getContactId());
857
858 contact.setCompanyId(user.getCompanyId());
859 contact.setUserId(creatorUserId);
860 contact.setUserName(creatorUserName);
861 contact.setCreateDate(now);
862 contact.setModifiedDate(now);
863 contact.setClassName(User.class.getName());
864 contact.setClassPK(user.getUserId());
865 contact.setAccountId(company.getAccountId());
866 contact.setParentContactId(ContactConstants.DEFAULT_PARENT_CONTACT_ID);
867 contact.setEmailAddress(user.getEmailAddress());
868 contact.setFirstName(firstName);
869 contact.setMiddleName(middleName);
870 contact.setLastName(lastName);
871 contact.setPrefixId(prefixId);
872 contact.setSuffixId(suffixId);
873 contact.setMale(male);
874 contact.setBirthday(birthday);
875 contact.setJobTitle(jobTitle);
876
877 contactPersistence.update(contact, serviceContext);
878
879
880
881 groupLocalService.addGroup(
882 user.getUserId(), GroupConstants.DEFAULT_PARENT_GROUP_ID,
883 User.class.getName(), user.getUserId(), null, null, 0,
884 StringPool.SLASH + screenName, false, true, null);
885
886
887
888 if (groupIds != null) {
889 List<Group> groups = new ArrayList<Group>();
890
891 for (long groupId : groupIds) {
892 Group group = groupLocalService.fetchGroup(groupId);
893
894 if (group != null) {
895 groups.add(group);
896 }
897 else {
898 if (_log.isWarnEnabled()) {
899 _log.warn("Group " + groupId + " does not exist");
900 }
901 }
902 }
903
904 groupLocalService.addUserGroups(userId, groups);
905 }
906
907 addDefaultGroups(userId);
908
909
910
911 updateOrganizations(userId, organizationIds, false);
912
913
914
915 if (roleIds != null) {
916 roleIds = UsersAdminUtil.addRequiredRoles(user, roleIds);
917
918 userPersistence.setRoles(userId, roleIds);
919 }
920
921 addDefaultRoles(userId);
922
923
924
925 if (userGroupIds != null) {
926 if (PropsValues.USER_GROUPS_COPY_LAYOUTS_TO_USER_PERSONAL_SITE) {
927 for (long userGroupId : userGroupIds) {
928 userGroupLocalService.copyUserGroupLayouts(
929 userGroupId, new long[] {userId});
930 }
931 }
932
933 userPersistence.setUserGroups(userId, userGroupIds);
934 }
935
936 addDefaultUserGroups(userId);
937
938
939
940 resourceLocalService.addResources(
941 companyId, 0, creatorUserId, User.class.getName(), user.getUserId(),
942 false, false, false);
943
944
945
946 if (serviceContext != null) {
947 updateAsset(
948 creatorUserId, user, serviceContext.getAssetCategoryIds(),
949 serviceContext.getAssetTagNames());
950 }
951
952
953
954 if ((serviceContext == null) || serviceContext.isIndexingEnabled()) {
955 reindex(user);
956 }
957
958
959
960 long workflowUserId = creatorUserId;
961
962 if (workflowUserId == userId) {
963 workflowUserId = defaultUser.getUserId();
964 }
965
966 ServiceContext workflowServiceContext = serviceContext;
967
968 if (workflowServiceContext == null) {
969 workflowServiceContext = new ServiceContext();
970 }
971
972 workflowServiceContext.setAttribute("autoPassword", autoPassword);
973 workflowServiceContext.setAttribute("passwordUnencrypted", password1);
974 workflowServiceContext.setAttribute("sendEmail", sendEmail);
975
976 WorkflowHandlerRegistryUtil.startWorkflowInstance(
977 companyId, workflowUserId, User.class.getName(), userId, user,
978 workflowServiceContext);
979
980 if (serviceContext != null) {
981 String passwordUnencrypted = (String)serviceContext.getAttribute(
982 "passwordUnencrypted");
983
984 if (Validator.isNotNull(passwordUnencrypted)) {
985 user.setPasswordUnencrypted(passwordUnencrypted);
986 }
987 }
988
989 return user;
990 }
991
992 @Override
993 public void afterPropertiesSet() {
994 super.afterPropertiesSet();
995
996 PortalCache<Serializable, Serializable> portalCache =
997 EntityCacheUtil.getPortalCache(UserImpl.class);
998
999 PortalCacheMapSynchronizeUtil.synchronize(
1000 portalCache, _defaultUsers,
1001 new Synchronizer<Serializable, Serializable>() {
1002
1003 @Override
1004 public void onSynchronize(
1005 Map<? extends Serializable, ? extends Serializable> map,
1006 Serializable key, Serializable value, int timeToLive) {
1007
1008 if (!(value instanceof UserCacheModel)) {
1009 return;
1010 }
1011
1012 UserCacheModel userCacheModel = (UserCacheModel)value;
1013
1014 if (userCacheModel.defaultUser) {
1015 _defaultUsers.remove(userCacheModel.companyId);
1016 }
1017 }
1018
1019 });
1020 }
1021
1022
1045 @Override
1046 public int authenticateByEmailAddress(
1047 long companyId, String emailAddress, String password,
1048 Map<String, String[]> headerMap, Map<String, String[]> parameterMap,
1049 Map<String, Object> resultsMap)
1050 throws PortalException {
1051
1052 return authenticate(
1053 companyId, emailAddress, password, CompanyConstants.AUTH_TYPE_EA,
1054 headerMap, parameterMap, resultsMap);
1055 }
1056
1057
1080 @Override
1081 public int authenticateByScreenName(
1082 long companyId, String screenName, String password,
1083 Map<String, String[]> headerMap, Map<String, String[]> parameterMap,
1084 Map<String, Object> resultsMap)
1085 throws PortalException {
1086
1087 return authenticate(
1088 companyId, screenName, password, CompanyConstants.AUTH_TYPE_SN,
1089 headerMap, parameterMap, resultsMap);
1090 }
1091
1092
1115 @Override
1116 public int authenticateByUserId(
1117 long companyId, long userId, String password,
1118 Map<String, String[]> headerMap, Map<String, String[]> parameterMap,
1119 Map<String, Object> resultsMap)
1120 throws PortalException {
1121
1122 return authenticate(
1123 companyId, String.valueOf(userId), password,
1124 CompanyConstants.AUTH_TYPE_ID, headerMap, parameterMap, resultsMap);
1125 }
1126
1127
1161 @Override
1162 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
1163 public long authenticateForBasic(
1164 long companyId, String authType, String login, String password)
1165 throws PortalException {
1166
1167 if (PropsValues.AUTH_LOGIN_DISABLED) {
1168 return 0;
1169 }
1170
1171 User user = null;
1172
1173 if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
1174 user = fetchUserByEmailAddress(companyId, login);
1175 }
1176 else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
1177 user = fetchUserByScreenName(companyId, login);
1178 }
1179 else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
1180 user = userPersistence.fetchByPrimaryKey(GetterUtil.getLong(login));
1181 }
1182
1183 if (user == null) {
1184 return 0;
1185 }
1186
1187 if (user.isDefaultUser()) {
1188 if (_log.isInfoEnabled()) {
1189 _log.info(
1190 "Basic authentication is disabled for the default " +
1191 "user");
1192 }
1193
1194 return 0;
1195 }
1196 else if (!user.isActive()) {
1197 if (_log.isInfoEnabled()) {
1198 _log.info(
1199 "Basic authentication is disabled for inactive user " +
1200 user.getUserId());
1201 }
1202
1203 return 0;
1204 }
1205
1206 if (!PropsValues.BASIC_AUTH_PASSWORD_REQUIRED) {
1207 return user.getUserId();
1208 }
1209
1210 String userPassword = user.getPassword();
1211
1212 if (!user.isPasswordEncrypted()) {
1213 userPassword = PasswordEncryptorUtil.encrypt(userPassword);
1214 }
1215
1216 String encPassword = PasswordEncryptorUtil.encrypt(
1217 password, userPassword);
1218
1219 if (userPassword.equals(password) || userPassword.equals(encPassword)) {
1220 return user.getUserId();
1221 }
1222
1223 return 0;
1224 }
1225
1226
1243 @Override
1244 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
1245 public long authenticateForDigest(
1246 long companyId, String username, String realm, String nonce,
1247 String method, String uri, String response)
1248 throws PortalException {
1249
1250 if (PropsValues.AUTH_LOGIN_DISABLED) {
1251 return 0;
1252 }
1253
1254
1255
1256 User user = fetchUserByEmailAddress(companyId, username);
1257
1258 if (user == null) {
1259 user = fetchUserByScreenName(companyId, username);
1260 }
1261
1262 if (user == null) {
1263 user = userPersistence.fetchByPrimaryKey(
1264 GetterUtil.getLong(username));
1265 }
1266
1267 if (user == null) {
1268 return 0;
1269 }
1270
1271 if (user.isDefaultUser()) {
1272 if (_log.isInfoEnabled()) {
1273 _log.info(
1274 "Digest authentication is disabled for the default user");
1275 }
1276
1277 return 0;
1278 }
1279 else if (!user.isActive()) {
1280 if (_log.isInfoEnabled()) {
1281 _log.info(
1282 "Digest authentication is disabled for inactive user " +
1283 user.getUserId());
1284 }
1285
1286 return 0;
1287 }
1288
1289
1290
1291 String digest = user.getDigest();
1292
1293 if (Validator.isNull(digest)) {
1294 _log.error(
1295 "User must first login through the portal " + user.getUserId());
1296
1297 return 0;
1298 }
1299
1300 String[] digestArray = StringUtil.split(user.getDigest());
1301
1302 for (String ha1 : digestArray) {
1303 String ha2 = DigesterUtil.digestHex(Digester.MD5, method, uri);
1304
1305 String curResponse = DigesterUtil.digestHex(
1306 Digester.MD5, ha1, nonce, ha2);
1307
1308 if (response.equals(curResponse)) {
1309 return user.getUserId();
1310 }
1311 }
1312
1313 return 0;
1314 }
1315
1316
1325 @Override
1326 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
1327 public boolean authenticateForJAAS(long userId, String encPassword) {
1328 if (PropsValues.AUTH_LOGIN_DISABLED) {
1329 return false;
1330 }
1331
1332 try {
1333 User user = userPersistence.findByPrimaryKey(userId);
1334
1335 if (user.isDefaultUser()) {
1336 if (_log.isInfoEnabled()) {
1337 _log.info(
1338 "JAAS authentication is disabled for the default user");
1339 }
1340
1341 return false;
1342 }
1343 else if (!user.isActive()) {
1344 if (_log.isInfoEnabled()) {
1345 _log.info(
1346 "JAAS authentication is disabled for inactive user " +
1347 userId);
1348 }
1349
1350 return false;
1351 }
1352
1353 String userPassword = user.getPassword();
1354
1355 if (user.isPasswordEncrypted()) {
1356 if (userPassword.equals(encPassword)) {
1357 return true;
1358 }
1359
1360 if (!PropsValues.PORTAL_JAAS_STRICT_PASSWORD) {
1361 encPassword = PasswordEncryptorUtil.encrypt(
1362 encPassword, userPassword);
1363
1364 if (userPassword.equals(encPassword)) {
1365 return true;
1366 }
1367 }
1368 }
1369 else {
1370 if (!PropsValues.PORTAL_JAAS_STRICT_PASSWORD) {
1371 if (userPassword.equals(encPassword)) {
1372 return true;
1373 }
1374 }
1375
1376 userPassword = PasswordEncryptorUtil.encrypt(
1377 userPassword, encPassword);
1378
1379 if (userPassword.equals(encPassword)) {
1380 return true;
1381 }
1382 }
1383 }
1384 catch (Exception e) {
1385 _log.error(e);
1386 }
1387
1388 return false;
1389 }
1390
1391
1398 @Override
1399 public void checkLockout(User user) throws PortalException {
1400 if (LDAPSettingsUtil.isPasswordPolicyEnabled(user.getCompanyId())) {
1401 return;
1402 }
1403
1404 PasswordPolicy passwordPolicy = user.getPasswordPolicy();
1405
1406 if (!passwordPolicy.isLockout()) {
1407 return;
1408 }
1409
1410
1411
1412 Date now = new Date();
1413 int failedLoginAttempts = user.getFailedLoginAttempts();
1414
1415 if (failedLoginAttempts > 0) {
1416 long failedLoginTime = user.getLastFailedLoginDate().getTime();
1417 long elapsedTime = now.getTime() - failedLoginTime;
1418 long requiredElapsedTime =
1419 passwordPolicy.getResetFailureCount() * 1000;
1420
1421 if ((requiredElapsedTime != 0) &&
1422 (elapsedTime > requiredElapsedTime)) {
1423
1424 user.setFailedLoginAttempts(0);
1425
1426 userPersistence.update(user);
1427 }
1428 }
1429
1430
1431
1432 if (user.isLockout()) {
1433 long lockoutTime = user.getLockoutDate().getTime();
1434 long elapsedTime = now.getTime() - lockoutTime;
1435 long requiredElapsedTime =
1436 passwordPolicy.getLockoutDuration() * 1000;
1437
1438 if ((requiredElapsedTime != 0) &&
1439 (elapsedTime > requiredElapsedTime)) {
1440
1441 user.setLockout(false);
1442 user.setLockoutDate(null);
1443
1444 userPersistence.update(user);
1445 }
1446 }
1447
1448 if (user.isLockout()) {
1449 throw new UserLockoutException.PasswordPolicyLockout(
1450 user, passwordPolicy);
1451 }
1452 }
1453
1454
1460 @Override
1461 public void checkLoginFailure(User user) {
1462 Date now = new Date();
1463
1464 int failedLoginAttempts = user.getFailedLoginAttempts();
1465
1466 user.setLastFailedLoginDate(now);
1467 user.setFailedLoginAttempts(++failedLoginAttempts);
1468
1469 userPersistence.update(user);
1470 }
1471
1472
1481 @Override
1482 public void checkLoginFailureByEmailAddress(
1483 long companyId, String emailAddress)
1484 throws PortalException {
1485
1486 User user = getUserByEmailAddress(companyId, emailAddress);
1487
1488 checkLoginFailure(user);
1489 }
1490
1491
1498 @Override
1499 public void checkLoginFailureById(long userId) throws PortalException {
1500 User user = userPersistence.findByPrimaryKey(userId);
1501
1502 checkLoginFailure(user);
1503 }
1504
1505
1513 @Override
1514 public void checkLoginFailureByScreenName(long companyId, String screenName)
1515 throws PortalException {
1516
1517 User user = getUserByScreenName(companyId, screenName);
1518
1519 checkLoginFailure(user);
1520 }
1521
1522
1531 @Override
1532 public void checkPasswordExpired(User user) throws PortalException {
1533 if (LDAPSettingsUtil.isPasswordPolicyEnabled(user.getCompanyId())) {
1534 return;
1535 }
1536
1537 PasswordPolicy passwordPolicy = user.getPasswordPolicy();
1538
1539
1540
1541 if (isPasswordExpired(user)) {
1542 int graceLoginCount = user.getGraceLoginCount();
1543
1544 if (graceLoginCount < passwordPolicy.getGraceLimit()) {
1545 user.setGraceLoginCount(++graceLoginCount);
1546
1547 userPersistence.update(user);
1548 }
1549 else {
1550 user.setDigest(StringPool.BLANK);
1551
1552 userPersistence.update(user);
1553
1554 throw new PasswordExpiredException();
1555 }
1556 }
1557
1558
1559
1560 if (passwordPolicy.isChangeable() &&
1561 passwordPolicy.isChangeRequired()) {
1562
1563 if (user.getLastLoginDate() == null) {
1564 user.setPasswordReset(true);
1565
1566 userPersistence.update(user);
1567 }
1568 }
1569 }
1570
1571
1576 @Override
1577 public void clearOrganizationUsers(long organizationId) {
1578 organizationPersistence.clearUsers(organizationId);
1579
1580 PermissionCacheUtil.clearCache();
1581 }
1582
1583
1588 @Override
1589 public void clearUserGroupUsers(long userGroupId) {
1590 userGroupPersistence.clearUsers(userGroupId);
1591
1592 PermissionCacheUtil.clearCache();
1593 }
1594
1595
1609 @Override
1610 public void completeUserRegistration(
1611 User user, ServiceContext serviceContext)
1612 throws PortalException {
1613
1614 boolean autoPassword = ParamUtil.getBoolean(
1615 serviceContext, "autoPassword");
1616
1617 String password = (String)serviceContext.getAttribute(
1618 "passwordUnencrypted");
1619
1620 if (autoPassword) {
1621 if (LDAPSettingsUtil.isPasswordPolicyEnabled(user.getCompanyId())) {
1622 if (_log.isWarnEnabled()) {
1623 StringBundler sb = new StringBundler(4);
1624
1625 sb.append("When LDAP password policy is enabled, it is ");
1626 sb.append("possible that portal generated passwords will ");
1627 sb.append("not match the LDAP policy. Using ");
1628 sb.append("RegExpToolkit to generate new password.");
1629
1630 _log.warn(sb.toString());
1631 }
1632
1633 RegExpToolkit regExpToolkit = new RegExpToolkit();
1634
1635 password = regExpToolkit.generate(null);
1636 }
1637 else {
1638 PasswordPolicy passwordPolicy =
1639 passwordPolicyLocalService.getPasswordPolicy(
1640 user.getCompanyId(), user.getOrganizationIds());
1641
1642 password = PwdToolkitUtil.generate(passwordPolicy);
1643 }
1644
1645 serviceContext.setAttribute("passwordUnencrypted", password);
1646
1647 user.setPassword(PasswordEncryptorUtil.encrypt(password));
1648 user.setPasswordUnencrypted(password);
1649 user.setPasswordEncrypted(true);
1650 user.setPasswordModified(true);
1651 user.setPasswordModifiedDate(new Date());
1652
1653 userPersistence.update(user);
1654
1655 user.setPasswordModified(false);
1656 }
1657
1658 if (user.hasCompanyMx()) {
1659 mailService.addUser(
1660 user.getCompanyId(), user.getUserId(), password,
1661 user.getFirstName(), user.getMiddleName(), user.getLastName(),
1662 user.getEmailAddress());
1663 }
1664
1665 boolean sendEmail = ParamUtil.getBoolean(serviceContext, "sendEmail");
1666
1667 if (sendEmail) {
1668 notifyUser(user, password, serviceContext);
1669 }
1670
1671 Company company = companyPersistence.findByPrimaryKey(
1672 user.getCompanyId());
1673
1674 if (company.isStrangersVerify()) {
1675 sendEmailAddressVerification(
1676 user, user.getEmailAddress(), serviceContext);
1677 }
1678 }
1679
1680
1692 @Override
1693 public KeyValuePair decryptUserId(
1694 long companyId, String name, String password)
1695 throws PortalException {
1696
1697 Company company = companyPersistence.findByPrimaryKey(companyId);
1698
1699 try {
1700 name = Encryptor.decrypt(company.getKeyObj(), name);
1701 }
1702 catch (EncryptorException ee) {
1703 throw new SystemException(ee);
1704 }
1705
1706 long userId = GetterUtil.getLong(name);
1707
1708 User user = userPersistence.findByPrimaryKey(userId);
1709
1710 try {
1711 password = Encryptor.decrypt(company.getKeyObj(), password);
1712 }
1713 catch (EncryptorException ee) {
1714 throw new SystemException(ee);
1715 }
1716
1717 String userPassword = user.getPassword();
1718 String encPassword = PasswordEncryptorUtil.encrypt(
1719 password, userPassword);
1720
1721 if (userPassword.equals(encPassword)) {
1722 if (isPasswordExpired(user)) {
1723 user.setPasswordReset(true);
1724
1725 userPersistence.update(user);
1726 }
1727
1728 return new KeyValuePair(name, password);
1729 }
1730 else {
1731 throw new PrincipalException();
1732 }
1733 }
1734
1735
1742 @Override
1743 public void deletePortrait(long userId) throws PortalException {
1744 User user = userPersistence.findByPrimaryKey(userId);
1745
1746 PortalUtil.updateImageId(user, false, null, "portraitId", 0, 0, 0);
1747 }
1748
1749
1757 @Override
1758 public void deleteRoleUser(long roleId, long userId)
1759 throws PortalException {
1760
1761 rolePersistence.removeUser(roleId, userId);
1762
1763 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
1764
1765 indexer.reindex(userId);
1766
1767 PermissionCacheUtil.clearCache();
1768 }
1769
1770
1777 @Override
1778 public User deleteUser(long userId) throws PortalException {
1779 User user = userPersistence.findByPrimaryKey(userId);
1780
1781 return deleteUser(user);
1782 }
1783
1784
1791 @Override
1792 public User deleteUser(User user) throws PortalException {
1793 if (!PropsValues.USERS_DELETE) {
1794 throw new RequiredUserException();
1795 }
1796
1797
1798
1799 browserTrackerLocalService.deleteUserBrowserTracker(user.getUserId());
1800
1801
1802
1803 Group group = null;
1804
1805 if (!user.isDefaultUser()) {
1806 group = user.getGroup();
1807 }
1808
1809 if (group != null) {
1810 groupLocalService.deleteGroup(group);
1811 }
1812
1813
1814
1815 try {
1816 imageLocalService.deleteImage(user.getPortraitId());
1817 }
1818 catch (NoSuchImageException nsie) {
1819 if (_log.isWarnEnabled()) {
1820 _log.warn("Unable to delete image " + user.getPortraitId());
1821 }
1822 }
1823
1824
1825
1826 passwordPolicyRelLocalService.deletePasswordPolicyRel(
1827 User.class.getName(), user.getUserId());
1828
1829
1830
1831 passwordTrackerLocalService.deletePasswordTrackers(user.getUserId());
1832
1833
1834
1835 subscriptionLocalService.deleteSubscriptions(user.getUserId());
1836
1837
1838
1839 userIdMapperLocalService.deleteUserIdMappers(user.getUserId());
1840
1841
1842
1843 announcementsDeliveryLocalService.deleteDeliveries(user.getUserId());
1844
1845
1846
1847 assetEntryLocalService.deleteEntry(
1848 User.class.getName(), user.getUserId());
1849
1850
1851
1852 blogsStatsUserLocalService.deleteStatsUserByUserId(user.getUserId());
1853
1854
1855
1856 dlFileRankLocalService.deleteFileRanksByUserId(user.getUserId());
1857
1858
1859
1860 expandoRowLocalService.deleteRows(user.getUserId());
1861
1862
1863
1864 mbBanLocalService.deleteBansByBanUserId(user.getUserId());
1865 mbStatsUserLocalService.deleteStatsUsersByUserId(user.getUserId());
1866 mbThreadFlagLocalService.deleteThreadFlagsByUserId(user.getUserId());
1867
1868
1869
1870 membershipRequestLocalService.deleteMembershipRequestsByUserId(
1871 user.getUserId());
1872
1873
1874
1875 shoppingCartLocalService.deleteUserCarts(user.getUserId());
1876
1877
1878
1879 socialActivityLocalService.deleteUserActivities(user.getUserId());
1880 socialRequestLocalService.deleteReceiverUserRequests(user.getUserId());
1881 socialRequestLocalService.deleteUserRequests(user.getUserId());
1882
1883
1884
1885 mailService.deleteUser(user.getCompanyId(), user.getUserId());
1886
1887
1888
1889 Contact contact = contactLocalService.fetchContact(user.getContactId());
1890
1891 if (contact != null) {
1892 contactLocalService.deleteContact(contact);
1893 }
1894
1895
1896
1897 userGroupRoleLocalService.deleteUserGroupRolesByUserId(
1898 user.getUserId());
1899
1900
1901
1902 resourceLocalService.deleteResource(
1903 user.getCompanyId(), User.class.getName(),
1904 ResourceConstants.SCOPE_INDIVIDUAL, user.getUserId());
1905
1906
1907
1908 userPersistence.remove(user);
1909
1910
1911
1912 PermissionCacheUtil.clearCache();
1913
1914
1915
1916 workflowInstanceLinkLocalService.deleteWorkflowInstanceLinks(
1917 user.getCompanyId(), 0, User.class.getName(), user.getUserId());
1918
1919 return user;
1920 }
1921
1922
1929 @Override
1930 public void deleteUserGroupUser(long userGroupId, long userId)
1931 throws PortalException {
1932
1933 userGroupPersistence.removeUser(userGroupId, userId);
1934
1935 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
1936
1937 indexer.reindex(userId);
1938
1939 PermissionCacheUtil.clearCache();
1940 }
1941
1942
1950 @Override
1951 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
1952 public String encryptUserId(String name) throws PortalException {
1953 long userId = GetterUtil.getLong(name);
1954
1955 User user = userPersistence.findByPrimaryKey(userId);
1956
1957 Company company = companyPersistence.findByPrimaryKey(
1958 user.getCompanyId());
1959
1960 try {
1961 return Encryptor.encrypt(company.getKeyObj(), name);
1962 }
1963 catch (EncryptorException ee) {
1964 throw new SystemException(ee);
1965 }
1966 }
1967
1968
1975 @Override
1976 public User fetchUserByContactId(long contactId) {
1977 return userPersistence.fetchByContactId(contactId);
1978 }
1979
1980
1988 @Override
1989 public User fetchUserByEmailAddress(long companyId, String emailAddress) {
1990 emailAddress = getLogin(emailAddress);
1991
1992 return userPersistence.fetchByC_EA(companyId, emailAddress);
1993 }
1994
1995
2003 @Override
2004 public User fetchUserByFacebookId(long companyId, long facebookId) {
2005 return userPersistence.fetchByC_FID(companyId, facebookId);
2006 }
2007
2008
2015 @Override
2016 public User fetchUserById(long userId) {
2017 return userPersistence.fetchByPrimaryKey(userId);
2018 }
2019
2020
2028 @Override
2029 public User fetchUserByOpenId(long companyId, String openId) {
2030 return userPersistence.fetchByC_O(companyId, openId);
2031 }
2032
2033
2040 @Override
2041 public User fetchUserByPortraitId(long portraitId) {
2042 return userPersistence.fetchByPortraitId(portraitId);
2043 }
2044
2045
2053 @Override
2054 public User fetchUserByScreenName(long companyId, String screenName) {
2055 screenName = getLogin(screenName);
2056
2057 return userPersistence.fetchByC_SN(companyId, screenName);
2058 }
2059
2060
2078 @Override
2079 public List<User> getCompanyUsers(long companyId, int start, int end) {
2080 return userPersistence.findByCompanyId(companyId, start, end);
2081 }
2082
2083
2089 @Override
2090 public int getCompanyUsersCount(long companyId) {
2091 return userPersistence.countByCompanyId(companyId);
2092 }
2093
2094
2102 @Override
2103 @Skip
2104 public User getDefaultUser(long companyId) throws PortalException {
2105 User userModel = _defaultUsers.get(companyId);
2106
2107 if (userModel == null) {
2108 userModel = userLocalService.loadGetDefaultUser(companyId);
2109
2110 _defaultUsers.put(companyId, userModel);
2111 }
2112
2113 return userModel;
2114 }
2115
2116
2124 @Override
2125 @Skip
2126 public long getDefaultUserId(long companyId) throws PortalException {
2127 User user = getDefaultUser(companyId);
2128
2129 return user.getUserId();
2130 }
2131
2132
2138 @Override
2139 public long[] getGroupUserIds(long groupId) {
2140 return groupPersistence.getUserPrimaryKeys(groupId);
2141 }
2142
2143
2152 @Override
2153 public int getGroupUsersCount(long groupId, int status)
2154 throws PortalException {
2155
2156 Group group = groupPersistence.findByPrimaryKey(groupId);
2157
2158 LinkedHashMap<String, Object> params =
2159 new LinkedHashMap<String, Object>();
2160
2161 params.put("usersGroups", new Long(groupId));
2162
2163 return searchCount(group.getCompanyId(), null, status, params);
2164 }
2165
2166 @Override
2167 public List<User> getInheritedRoleUsers(
2168 long roleId, int start, int end, OrderByComparator<User> obc)
2169 throws PortalException {
2170
2171 Role role = rolePersistence.findByPrimaryKey(roleId);
2172
2173 LinkedHashMap<String, Object> params =
2174 new LinkedHashMap<String, Object>();
2175
2176 params.put("inherit", Boolean.TRUE);
2177 params.put("usersRoles", roleId);
2178
2179 return search(
2180 role.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,
2181 params, start, end, obc);
2182 }
2183
2184
2191 @Override
2192 public List<User> getNoAnnouncementsDeliveries(String type) {
2193 return userFinder.findByNoAnnouncementsDeliveries(type);
2194 }
2195
2196
2201 @Override
2202 public List<User> getNoContacts() {
2203 return userFinder.findByNoContacts();
2204 }
2205
2206
2212 @Override
2213 public List<User> getNoGroups() {
2214 return userFinder.findByNoGroups();
2215 }
2216
2217
2223 @Override
2224 public long[] getOrganizationUserIds(long organizationId) {
2225 return organizationPersistence.getUserPrimaryKeys(organizationId);
2226 }
2227
2228
2238 @Override
2239 public int getOrganizationUsersCount(long organizationId, int status)
2240 throws PortalException {
2241
2242 Organization organization = organizationPersistence.findByPrimaryKey(
2243 organizationId);
2244
2245 LinkedHashMap<String, Object> params =
2246 new LinkedHashMap<String, Object>();
2247
2248 params.put("usersOrgs", new Long(organizationId));
2249
2250 return searchCount(organization.getCompanyId(), null, status, params);
2251 }
2252
2253
2259 @Override
2260 public long[] getRoleUserIds(long roleId) {
2261 return rolePersistence.getUserPrimaryKeys(roleId);
2262 }
2263
2264
2273 @Override
2274 public int getRoleUsersCount(long roleId, int status)
2275 throws PortalException {
2276
2277 Role role = rolePersistence.findByPrimaryKey(roleId);
2278
2279 LinkedHashMap<String, Object> params =
2280 new LinkedHashMap<String, Object>();
2281
2282 params.put("usersRoles", new Long(roleId));
2283
2284 return searchCount(role.getCompanyId(), null, status, params);
2285 }
2286
2287
2316 @Deprecated
2317 @Override
2318 public List<User> getSocialUsers(
2319 long userId, int socialRelationType, int start, int end,
2320 OrderByComparator<User> obc)
2321 throws PortalException {
2322
2323 return getSocialUsers(
2324 userId, socialRelationType, StringPool.EQUAL, start, end, obc);
2325 }
2326
2327
2353 @Deprecated
2354 @Override
2355 public List<User> getSocialUsers(
2356 long userId, int start, int end, OrderByComparator<User> obc)
2357 throws PortalException {
2358
2359 return getSocialUsers(
2360 userId, SocialRelationConstants.TYPE_UNI_ENEMY,
2361 StringPool.NOT_EQUAL, start, end, obc);
2362 }
2363
2364 @Override
2365 public List<User> getSocialUsers(
2366 long userId, int socialRelationType,
2367 String socialRelationTypeComparator, int start, int end,
2368 OrderByComparator<User> obc)
2369 throws PortalException {
2370
2371 if (!socialRelationTypeComparator.equals(StringPool.EQUAL) &&
2372 !socialRelationTypeComparator.equals(StringPool.NOT_EQUAL)) {
2373
2374 throw new IllegalArgumentException(
2375 "Invalid social relation type comparator " +
2376 socialRelationTypeComparator);
2377 }
2378
2379 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS)) {
2380 List<SocialRelation> socialRelations =
2381 socialRelationPersistence.findByU1_T(
2382 userId, socialRelationType);
2383
2384 if (socialRelationTypeComparator.equals(StringPool.NOT_EQUAL)) {
2385 socialRelations = ListUtil.remove(
2386 socialRelationPersistence.findByUserId1(userId),
2387 socialRelations);
2388 }
2389
2390 List<User> users = new ArrayList<User>();
2391
2392 for (SocialRelation socialRelation : socialRelations) {
2393 User user = userPersistence.findByPrimaryKey(
2394 socialRelation.getUserId2());
2395
2396 if (user.isDefaultUser() ||
2397 (user.getStatus() != WorkflowConstants.STATUS_APPROVED)) {
2398
2399 continue;
2400 }
2401
2402 if (!users.contains(user)) {
2403 users.add(user);
2404 }
2405 }
2406
2407 if (obc != null) {
2408 users = ListUtil.sort(users, obc);
2409 }
2410
2411 return users;
2412 }
2413
2414 User user = userPersistence.findByPrimaryKey(userId);
2415
2416 return userFinder.findBySocialUsers(
2417 user.getCompanyId(), userId, socialRelationType,
2418 socialRelationTypeComparator, WorkflowConstants.STATUS_APPROVED,
2419 start, end, obc);
2420 }
2421
2422
2449 @Override
2450 public List<User> getSocialUsers(
2451 long userId1, long userId2, int socialRelationType, int start,
2452 int end, OrderByComparator<User> obc)
2453 throws PortalException {
2454
2455 User user1 = userPersistence.findByPrimaryKey(userId1);
2456
2457 LinkedHashMap<String, Object> params =
2458 new LinkedHashMap<String, Object>();
2459
2460 params.put(
2461 "socialMutualRelationType",
2462 new Long[] {userId1, new Long(socialRelationType), userId2,
2463 new Long(socialRelationType)});
2464
2465 return search(
2466 user1.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,
2467 params, start, end, obc);
2468 }
2469
2470
2494 @Override
2495 public List<User> getSocialUsers(
2496 long userId1, long userId2, int start, int end,
2497 OrderByComparator<User> obc)
2498 throws PortalException {
2499
2500 User user1 = userPersistence.findByPrimaryKey(userId1);
2501
2502 LinkedHashMap<String, Object> params =
2503 new LinkedHashMap<String, Object>();
2504
2505 params.put("socialMutualRelation", new Long[] {userId1, userId2});
2506
2507 return search(
2508 user1.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,
2509 params, start, end, obc);
2510 }
2511
2512
2522 @Deprecated
2523 @Override
2524 public int getSocialUsersCount(long userId) throws PortalException {
2525 return getSocialUsersCount(
2526 userId, SocialRelationConstants.TYPE_UNI_ENEMY,
2527 StringPool.NOT_EQUAL);
2528 }
2529
2530
2545 @Deprecated
2546 @Override
2547 public int getSocialUsersCount(long userId, int socialRelationType)
2548 throws PortalException {
2549
2550 return getSocialUsersCount(
2551 userId, socialRelationType, StringPool.EQUAL);
2552 }
2553
2554
2564 @Override
2565 public int getSocialUsersCount(
2566 long userId, int socialRelationType,
2567 String socialRelationTypeComparator)
2568 throws PortalException {
2569
2570 User user = userPersistence.findByPrimaryKey(userId);
2571
2572 if (!socialRelationTypeComparator.equals(StringPool.EQUAL) &&
2573 !socialRelationTypeComparator.equals(StringPool.NOT_EQUAL)) {
2574
2575 throw new IllegalArgumentException(
2576 "Invalid social relation type comparator " +
2577 socialRelationTypeComparator);
2578 }
2579
2580 return userFinder.countBySocialUsers(
2581 user.getCompanyId(), user.getUserId(), socialRelationType,
2582 socialRelationTypeComparator, WorkflowConstants.STATUS_APPROVED);
2583 }
2584
2585
2594 @Override
2595 public int getSocialUsersCount(long userId1, long userId2)
2596 throws PortalException {
2597
2598 User user1 = userPersistence.findByPrimaryKey(userId1);
2599
2600 LinkedHashMap<String, Object> params =
2601 new LinkedHashMap<String, Object>();
2602
2603 params.put("socialMutualRelation", new Long[] {userId1, userId2});
2604
2605 return searchCount(
2606 user1.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,
2607 params);
2608 }
2609
2610
2623 @Override
2624 public int getSocialUsersCount(
2625 long userId1, long userId2, int socialRelationType)
2626 throws PortalException {
2627
2628 User user1 = userPersistence.findByPrimaryKey(userId1);
2629
2630 LinkedHashMap<String, Object> params =
2631 new LinkedHashMap<String, Object>();
2632
2633 params.put(
2634 "socialMutualRelationType",
2635 new Long[] {userId1, new Long(socialRelationType), userId2,
2636 new Long(socialRelationType)});
2637
2638 return searchCount(
2639 user1.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,
2640 params);
2641 }
2642
2643
2650 @Override
2651 public User getUserByContactId(long contactId) throws PortalException {
2652 return userPersistence.findByContactId(contactId);
2653 }
2654
2655
2664 @Override
2665 public User getUserByEmailAddress(long companyId, String emailAddress)
2666 throws PortalException {
2667
2668 emailAddress = getLogin(emailAddress);
2669
2670 return userPersistence.findByC_EA(companyId, emailAddress);
2671 }
2672
2673
2681 @Override
2682 public User getUserByFacebookId(long companyId, long facebookId)
2683 throws PortalException {
2684
2685 return userPersistence.findByC_FID(companyId, facebookId);
2686 }
2687
2688
2695 @Override
2696 public User getUserById(long userId) throws PortalException {
2697 return userPersistence.findByPrimaryKey(userId);
2698 }
2699
2700
2709 @Override
2710 public User getUserById(long companyId, long userId)
2711 throws PortalException {
2712
2713 return userPersistence.findByC_U(companyId, userId);
2714 }
2715
2716
2724 @Override
2725 public User getUserByOpenId(long companyId, String openId)
2726 throws PortalException {
2727
2728 return userPersistence.findByC_O(companyId, openId);
2729 }
2730
2731
2738 @Override
2739 public User getUserByPortraitId(long portraitId) throws PortalException {
2740 return userPersistence.findByPortraitId(portraitId);
2741 }
2742
2743
2751 @Override
2752 public User getUserByScreenName(long companyId, String screenName)
2753 throws PortalException {
2754
2755 screenName = getLogin(screenName);
2756
2757 return userPersistence.findByC_SN(companyId, screenName);
2758 }
2759
2760
2769 @Deprecated
2770 @Override
2771 public User getUserByUuid(String uuid) throws PortalException {
2772 List<User> users = userPersistence.findByUuid(uuid);
2773
2774 if (users.isEmpty()) {
2775 throw new NoSuchUserException("{uuid=" + uuid + "}");
2776 }
2777 else {
2778 return users.get(0);
2779 }
2780 }
2781
2782
2790 @Override
2791 public User getUserByUuidAndCompanyId(String uuid, long companyId)
2792 throws PortalException {
2793
2794 List<User> users = userPersistence.findByUuid_C(uuid, companyId);
2795
2796 if (users.isEmpty()) {
2797 StringBundler sb = new StringBundler(5);
2798
2799 sb.append("{uuid=");
2800 sb.append(uuid);
2801 sb.append(", companyId=");
2802 sb.append(companyId);
2803 sb.append("}");
2804
2805 throw new NoSuchUserException(sb.toString());
2806 }
2807 else {
2808 return users.get(0);
2809 }
2810 }
2811
2812
2821 @Override
2822 public int getUserGroupUsersCount(long userGroupId, int status)
2823 throws PortalException {
2824
2825 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
2826 userGroupId);
2827
2828 LinkedHashMap<String, Object> params =
2829 new LinkedHashMap<String, Object>();
2830
2831 params.put("usersUserGroups", new Long(userGroupId));
2832
2833 return searchCount(userGroup.getCompanyId(), null, status, params);
2834 }
2835
2836
2845 @Override
2846 public long getUserIdByEmailAddress(long companyId, String emailAddress)
2847 throws PortalException {
2848
2849 emailAddress = StringUtil.toLowerCase(emailAddress.trim());
2850
2851 User user = userPersistence.findByC_EA(companyId, emailAddress);
2852
2853 return user.getUserId();
2854 }
2855
2856
2864 @Override
2865 public long getUserIdByScreenName(long companyId, String screenName)
2866 throws PortalException {
2867
2868 screenName = getLogin(screenName);
2869
2870 User user = userPersistence.findByC_SN(companyId, screenName);
2871
2872 return user.getUserId();
2873 }
2874
2875
2884 @Override
2885 public boolean hasPasswordPolicyUser(long passwordPolicyId, long userId) {
2886 return passwordPolicyRelLocalService.hasPasswordPolicyRel(
2887 passwordPolicyId, User.class.getName(), userId);
2888 }
2889
2890
2904 @Override
2905 public boolean hasRoleUser(
2906 long companyId, String name, long userId, boolean inherited)
2907 throws PortalException {
2908
2909 return roleLocalService.hasUserRole(userId, companyId, name, inherited);
2910 }
2911
2912
2921 @Override
2922 public boolean isPasswordExpired(User user) throws PortalException {
2923 PasswordPolicy passwordPolicy = user.getPasswordPolicy();
2924
2925 if ((passwordPolicy != null) && passwordPolicy.getExpireable()) {
2926 Date now = new Date();
2927
2928 if (user.getPasswordModifiedDate() == null) {
2929 user.setPasswordModifiedDate(now);
2930
2931 userLocalService.updateUser(user);
2932 }
2933
2934 long passwordStartTime = user.getPasswordModifiedDate().getTime();
2935 long elapsedTime = now.getTime() - passwordStartTime;
2936
2937 if (elapsedTime > (passwordPolicy.getMaxAge() * 1000)) {
2938 return true;
2939 }
2940 else {
2941 return false;
2942 }
2943 }
2944
2945 return false;
2946 }
2947
2948
2959 @Override
2960 public boolean isPasswordExpiringSoon(User user) throws PortalException {
2961 PasswordPolicy passwordPolicy = user.getPasswordPolicy();
2962
2963 if ((passwordPolicy != null) && passwordPolicy.isExpireable() &&
2964 (passwordPolicy.getWarningTime() > 0)) {
2965
2966 Date now = new Date();
2967
2968 if (user.getPasswordModifiedDate() == null) {
2969 user.setPasswordModifiedDate(now);
2970
2971 userLocalService.updateUser(user);
2972 }
2973
2974 long timeModified = user.getPasswordModifiedDate().getTime();
2975 long passwordExpiresOn =
2976 (passwordPolicy.getMaxAge() * 1000) + timeModified;
2977
2978 long timeStartWarning =
2979 passwordExpiresOn - (passwordPolicy.getWarningTime() * 1000);
2980
2981 if (now.getTime() > timeStartWarning) {
2982 return true;
2983 }
2984 else {
2985 return false;
2986 }
2987 }
2988
2989 return false;
2990 }
2991
2992
2999 @Override
3000 public User loadGetDefaultUser(long companyId) throws PortalException {
3001 return userPersistence.findByC_DU(companyId, true);
3002 }
3003
3004
3035 @Override
3036 public List<User> search(
3037 long companyId, String keywords, int status,
3038 LinkedHashMap<String, Object> params, int start, int end,
3039 OrderByComparator<User> obc) {
3040
3041 return userFinder.findByKeywords(
3042 companyId, keywords, status, params, start, end, obc);
3043 }
3044
3045
3075 @Override
3076 public Hits search(
3077 long companyId, String keywords, int status,
3078 LinkedHashMap<String, Object> params, int start, int end, Sort sort) {
3079
3080 String firstName = null;
3081 String middleName = null;
3082 String lastName = null;
3083 String fullName = null;
3084 String screenName = null;
3085 String emailAddress = null;
3086 String street = null;
3087 String city = null;
3088 String zip = null;
3089 String region = null;
3090 String country = null;
3091 boolean andOperator = false;
3092
3093 if (Validator.isNotNull(keywords)) {
3094 firstName = keywords;
3095 middleName = keywords;
3096 lastName = keywords;
3097 fullName = keywords;
3098 screenName = keywords;
3099 emailAddress = keywords;
3100 street = keywords;
3101 city = keywords;
3102 zip = keywords;
3103 region = keywords;
3104 country = keywords;
3105 }
3106 else {
3107 andOperator = true;
3108 }
3109
3110 if (params != null) {
3111 params.put("keywords", keywords);
3112 }
3113
3114 try {
3115 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
3116 User.class);
3117
3118 SearchContext searchContext = buildSearchContext(
3119 companyId, firstName, middleName, lastName, fullName,
3120 screenName, emailAddress, street, city, zip, region, country,
3121 status, params, andOperator, start, end, sort);
3122
3123 return indexer.search(searchContext);
3124 }
3125 catch (Exception e) {
3126 throw new SystemException(e);
3127 }
3128 }
3129
3130
3169 @Override
3170 public List<User> search(
3171 long companyId, String firstName, String middleName, String lastName,
3172 String screenName, String emailAddress, int status,
3173 LinkedHashMap<String, Object> params, boolean andSearch, int start,
3174 int end, OrderByComparator<User> obc) {
3175
3176 return userFinder.findByC_FN_MN_LN_SN_EA_S(
3177 companyId, firstName, middleName, lastName, screenName,
3178 emailAddress, status, params, andSearch, start, end, obc);
3179 }
3180
3181
3219 @Override
3220 public Hits search(
3221 long companyId, String firstName, String middleName, String lastName,
3222 String screenName, String emailAddress, int status,
3223 LinkedHashMap<String, Object> params, boolean andSearch, int start,
3224 int end, Sort sort) {
3225
3226 try {
3227 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
3228 User.class);
3229
3230 SearchContext searchContext = buildSearchContext(
3231 companyId, firstName, middleName, lastName, null, screenName,
3232 emailAddress, null, null, null, null, null, status, params,
3233 andSearch, start, end, sort);
3234
3235 return indexer.search(searchContext);
3236 }
3237 catch (Exception e) {
3238 throw new SystemException(e);
3239 }
3240 }
3241
3242
3255 @Override
3256 public int searchCount(
3257 long companyId, String keywords, int status,
3258 LinkedHashMap<String, Object> params) {
3259
3260 if (!PropsValues.USERS_INDEXER_ENABLED ||
3261 !PropsValues.USERS_SEARCH_WITH_INDEX || isUseCustomSQL(params)) {
3262
3263 return userFinder.countByKeywords(
3264 companyId, keywords, status, params);
3265 }
3266
3267 try {
3268 String firstName = null;
3269 String middleName = null;
3270 String lastName = null;
3271 String fullName = null;
3272 String screenName = null;
3273 String emailAddress = null;
3274 String street = null;
3275 String city = null;
3276 String zip = null;
3277 String region = null;
3278 String country = null;
3279 boolean andOperator = false;
3280
3281 if (Validator.isNotNull(keywords)) {
3282 firstName = keywords;
3283 middleName = keywords;
3284 lastName = keywords;
3285 fullName = keywords;
3286 screenName = keywords;
3287 emailAddress = keywords;
3288 street = keywords;
3289 city = keywords;
3290 zip = keywords;
3291 region = keywords;
3292 country = keywords;
3293 }
3294 else {
3295 andOperator = true;
3296 }
3297
3298 if (params != null) {
3299 params.put("keywords", keywords);
3300 }
3301
3302 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
3303 User.class);
3304
3305 SearchContext searchContext = buildSearchContext(
3306 companyId, firstName, middleName, lastName, fullName,
3307 screenName, emailAddress, street, city, zip, region, country,
3308 status, params, andOperator, QueryUtil.ALL_POS,
3309 QueryUtil.ALL_POS, null);
3310
3311 Hits hits = indexer.search(searchContext);
3312
3313 return hits.getLength();
3314 }
3315 catch (Exception e) {
3316 throw new SystemException(e);
3317 }
3318 }
3319
3320
3341 @Override
3342 public int searchCount(
3343 long companyId, String firstName, String middleName, String lastName,
3344 String screenName, String emailAddress, int status,
3345 LinkedHashMap<String, Object> params, boolean andSearch) {
3346
3347 if (!PropsValues.USERS_INDEXER_ENABLED ||
3348 !PropsValues.USERS_SEARCH_WITH_INDEX || isUseCustomSQL(params)) {
3349
3350 return userFinder.countByC_FN_MN_LN_SN_EA_S(
3351 companyId, firstName, middleName, lastName, screenName,
3352 emailAddress, status, params, andSearch);
3353 }
3354
3355 try {
3356 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
3357 User.class);
3358
3359 FullNameGenerator fullNameGenerator =
3360 FullNameGeneratorFactory.getInstance();
3361
3362 String fullName = fullNameGenerator.getFullName(
3363 firstName, middleName, lastName);
3364
3365 SearchContext searchContext = buildSearchContext(
3366 companyId, firstName, middleName, lastName, fullName,
3367 screenName, emailAddress, null, null, null, null, null, status,
3368 params, true, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
3369
3370 Hits hits = indexer.search(searchContext);
3371
3372 return hits.getLength();
3373 }
3374 catch (Exception e) {
3375 throw new SystemException(e);
3376 }
3377 }
3378
3379 @Override
3380 public List<User> searchSocial(
3381 long userId, int[] socialRelationTypes, String keywords, int start,
3382 int end)
3383 throws PortalException {
3384
3385 User user = userPersistence.findByPrimaryKey(userId);
3386
3387 LinkedHashMap<String, Object> params =
3388 new LinkedHashMap<String, Object>();
3389
3390 params.put(
3391 "socialRelationType",
3392 new Long[][] {
3393 new Long[] {userId}, ArrayUtil.toLongArray(socialRelationTypes)
3394 });
3395 params.put("wildcardMode", WildcardMode.TRAILING);
3396
3397 return userFinder.findByKeywords(
3398 user.getCompanyId(), keywords, WorkflowConstants.STATUS_APPROVED,
3399 params, start, end, null);
3400 }
3401
3402 @Override
3403 public List<User> searchSocial(
3404 long companyId, long[] groupIds, String keywords, int start, int end) {
3405
3406 LinkedHashMap<String, Object> params =
3407 new LinkedHashMap<String, Object>();
3408
3409 params.put("usersGroups", ArrayUtil.toLongArray(groupIds));
3410 params.put("wildcardMode", WildcardMode.TRAILING);
3411
3412 return userFinder.findByKeywords(
3413 companyId, keywords, WorkflowConstants.STATUS_APPROVED, params,
3414 start, end, null);
3415 }
3416
3417 @Override
3418 public List<User> searchSocial(
3419 long[] groupIds, long userId, int[] socialRelationTypes,
3420 String keywords, int start, int end)
3421 throws PortalException {
3422
3423 User user = userPersistence.findByPrimaryKey(userId);
3424
3425 LinkedHashMap<String, Object> params =
3426 new LinkedHashMap<String, Object>();
3427
3428 params.put(
3429 "socialRelationType",
3430 new Long[][] {
3431 new Long[] {userId}, ArrayUtil.toLongArray(socialRelationTypes)
3432 });
3433 params.put("socialRelationTypeUnionUserGroups", true);
3434 params.put("usersGroups", ArrayUtil.toLongArray(groupIds));
3435 params.put("wildcardMode", WildcardMode.TRAILING);
3436
3437 return userFinder.findByKeywords(
3438 user.getCompanyId(), keywords, WorkflowConstants.STATUS_APPROVED,
3439 params, start, end, null);
3440 }
3441
3442 @Override
3443 public BaseModelSearchResult<User> searchUsers(
3444 long companyId, String keywords, int status,
3445 LinkedHashMap<String, Object> params, int start, int end, Sort sort)
3446 throws PortalException {
3447
3448 String firstName = null;
3449 String middleName = null;
3450 String lastName = null;
3451 String fullName = null;
3452 String screenName = null;
3453 String emailAddress = null;
3454 String street = null;
3455 String city = null;
3456 String zip = null;
3457 String region = null;
3458 String country = null;
3459 boolean andOperator = false;
3460
3461 if (Validator.isNotNull(keywords)) {
3462 firstName = keywords;
3463 middleName = keywords;
3464 lastName = keywords;
3465 fullName = keywords;
3466 screenName = keywords;
3467 emailAddress = keywords;
3468 street = keywords;
3469 city = keywords;
3470 zip = keywords;
3471 region = keywords;
3472 country = keywords;
3473 }
3474 else {
3475 andOperator = true;
3476 }
3477
3478 if (params != null) {
3479 params.put("keywords", keywords);
3480 }
3481
3482 SearchContext searchContext = buildSearchContext(
3483 companyId, firstName, middleName, lastName, fullName, screenName,
3484 emailAddress, street, city, zip, region, country, status, params,
3485 andOperator, start, end, sort);
3486
3487 return searchUsers(searchContext);
3488 }
3489
3490 @Override
3491 public BaseModelSearchResult<User> searchUsers(
3492 long companyId, String firstName, String middleName,
3493 String lastName, String screenName, String emailAddress, int status,
3494 LinkedHashMap<String, Object> params, boolean andSearch, int start,
3495 int end, Sort sort)
3496 throws PortalException {
3497
3498 SearchContext searchContext = buildSearchContext(
3499 companyId, firstName, middleName, lastName, null, screenName,
3500 emailAddress, null, null, null, null, null, status, params,
3501 andSearch, start, end, sort);
3502
3503 return searchUsers(searchContext);
3504 }
3505
3506
3516 @Override
3517 public void sendEmailAddressVerification(
3518 User user, String emailAddress, ServiceContext serviceContext)
3519 throws PortalException {
3520
3521 if (user.isEmailAddressVerified() &&
3522 StringUtil.equalsIgnoreCase(emailAddress, user.getEmailAddress())) {
3523
3524 return;
3525 }
3526
3527 Ticket ticket = ticketLocalService.addTicket(
3528 user.getCompanyId(), User.class.getName(), user.getUserId(),
3529 TicketConstants.TYPE_EMAIL_ADDRESS, emailAddress, null,
3530 serviceContext);
3531
3532 String verifyEmailAddressURL =
3533 serviceContext.getPortalURL() + serviceContext.getPathMain() +
3534 "/portal/verify_email_address?ticketKey=" + ticket.getKey();
3535
3536 long plid = serviceContext.getPlid();
3537
3538 if (plid > 0) {
3539 Layout layout = layoutLocalService.fetchLayout(plid);
3540
3541 if (layout != null) {
3542 Group group = layout.getGroup();
3543
3544 if (!layout.isPrivateLayout() && !group.isUser()) {
3545 verifyEmailAddressURL +=
3546 "&p_l_id=" + serviceContext.getPlid();
3547 }
3548 }
3549 }
3550
3551 String fromName = PrefsPropsUtil.getString(
3552 user.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_NAME);
3553 String fromAddress = PrefsPropsUtil.getString(
3554 user.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
3555
3556 String toName = user.getFullName();
3557 String toAddress = emailAddress;
3558
3559 PortletPreferences companyPortletPreferences =
3560 PrefsPropsUtil.getPreferences(user.getCompanyId(), true);
3561
3562 Map<Locale, String> localizedSubjectMap =
3563 LocalizationUtil.getLocalizationMap(
3564 companyPortletPreferences, "adminEmailVerificationSubject",
3565 PropsKeys.ADMIN_EMAIL_VERIFICATION_SUBJECT);
3566 Map<Locale, String> localizedBodyMap =
3567 LocalizationUtil.getLocalizationMap(
3568 companyPortletPreferences, "adminEmailVerificationBody",
3569 PropsKeys.ADMIN_EMAIL_VERIFICATION_BODY);
3570
3571 SubscriptionSender subscriptionSender = new SubscriptionSender();
3572
3573 subscriptionSender.setCompanyId(user.getCompanyId());
3574 subscriptionSender.setContextAttributes(
3575 "[$EMAIL_VERIFICATION_CODE$]", ticket.getKey(),
3576 "[$EMAIL_VERIFICATION_URL$]", verifyEmailAddressURL,
3577 "[$REMOTE_ADDRESS$]", serviceContext.getRemoteAddr(),
3578 "[$REMOTE_HOST$]", serviceContext.getRemoteHost(), "[$USER_ID$]",
3579 user.getUserId(), "[$USER_SCREENNAME$]", user.getScreenName());
3580 subscriptionSender.setFrom(fromAddress, fromName);
3581 subscriptionSender.setHtmlFormat(true);
3582 subscriptionSender.setLocalizedBodyMap(localizedBodyMap);
3583 subscriptionSender.setLocalizedSubjectMap(localizedSubjectMap);
3584 subscriptionSender.setMailId("user", user.getUserId());
3585 subscriptionSender.setServiceContext(serviceContext);
3586 subscriptionSender.setUserId(user.getUserId());
3587
3588 subscriptionSender.addRuntimeSubscribers(toAddress, toName);
3589
3590 subscriptionSender.flushNotificationsAsync();
3591 }
3592
3593
3611 @Override
3612 public boolean sendPassword(
3613 long companyId, String emailAddress, String fromName,
3614 String fromAddress, String subject, String body,
3615 ServiceContext serviceContext)
3616 throws PortalException {
3617
3618 Company company = companyPersistence.findByPrimaryKey(companyId);
3619
3620 if (!company.isSendPassword() && !company.isSendPasswordResetLink()) {
3621 throw new SendPasswordException.MustBeEnabled(company);
3622 }
3623
3624 emailAddress = StringUtil.toLowerCase(emailAddress.trim());
3625
3626 if (Validator.isNull(emailAddress)) {
3627 throw new UserEmailAddressException.MustNotBeNull();
3628 }
3629
3630 User user = userPersistence.findByC_EA(companyId, emailAddress);
3631
3632 PasswordPolicy passwordPolicy = user.getPasswordPolicy();
3633
3634 String newPassword = StringPool.BLANK;
3635 String passwordResetURL = StringPool.BLANK;
3636
3637 if (company.isSendPasswordResetLink()) {
3638 Date expirationDate = null;
3639
3640 if ((passwordPolicy != null) &&
3641 (passwordPolicy.getResetTicketMaxAge() > 0)) {
3642
3643 expirationDate = new Date(
3644 System.currentTimeMillis() +
3645 (passwordPolicy.getResetTicketMaxAge() * 1000));
3646 }
3647
3648 Ticket ticket = ticketLocalService.addTicket(
3649 companyId, User.class.getName(), user.getUserId(),
3650 TicketConstants.TYPE_PASSWORD, null, expirationDate,
3651 serviceContext);
3652
3653 passwordResetURL =
3654 serviceContext.getPortalURL() + serviceContext.getPathMain() +
3655 "/portal/update_password?p_l_id="+
3656 serviceContext.getPlid() +
3657 "&ticketKey=" + ticket.getKey();
3658 }
3659 else {
3660 if (!PasswordEncryptorUtil.PASSWORDS_ENCRYPTION_ALGORITHM.equals(
3661 PasswordEncryptorUtil.TYPE_NONE)) {
3662
3663 if (LDAPSettingsUtil.isPasswordPolicyEnabled(
3664 user.getCompanyId())) {
3665
3666 if (_log.isWarnEnabled()) {
3667 StringBundler sb = new StringBundler(5);
3668
3669 sb.append("When LDAP password policy is enabled, ");
3670 sb.append("it is possible that portal generated ");
3671 sb.append("passwords will not match the LDAP policy.");
3672 sb.append("Using RegExpToolkit to generate new ");
3673 sb.append("password.");
3674
3675 _log.warn(sb.toString());
3676 }
3677
3678 RegExpToolkit regExpToolkit = new RegExpToolkit();
3679
3680 newPassword = regExpToolkit.generate(null);
3681 }
3682 else {
3683 newPassword = PwdToolkitUtil.generate(passwordPolicy);
3684 }
3685
3686 boolean passwordReset = false;
3687
3688 if (passwordPolicy.getChangeable() &&
3689 passwordPolicy.getChangeRequired()) {
3690
3691 passwordReset = true;
3692 }
3693
3694 user.setPassword(PasswordEncryptorUtil.encrypt(newPassword));
3695 user.setPasswordUnencrypted(newPassword);
3696 user.setPasswordEncrypted(true);
3697 user.setPasswordReset(passwordReset);
3698 user.setPasswordModified(true);
3699 user.setPasswordModifiedDate(new Date());
3700
3701 userPersistence.update(user);
3702
3703 user.setPasswordModified(false);
3704 }
3705 else {
3706 newPassword = user.getPassword();
3707 }
3708 }
3709
3710 if (Validator.isNull(fromName)) {
3711 fromName = PrefsPropsUtil.getString(
3712 companyId, PropsKeys.ADMIN_EMAIL_FROM_NAME);
3713 }
3714
3715 if (Validator.isNull(fromAddress)) {
3716 fromAddress = PrefsPropsUtil.getString(
3717 companyId, PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
3718 }
3719
3720 String toName = user.getFullName();
3721 String toAddress = user.getEmailAddress();
3722
3723 PortletPreferences companyPortletPreferences =
3724 PrefsPropsUtil.getPreferences(company.getCompanyId(), true);
3725
3726 Map<Locale, String> localizedSubjectMap = null;
3727 Map<Locale, String> localizedBodyMap = null;
3728
3729 String bodyProperty = null;
3730 String prefix = null;
3731 String subjectProperty = null;
3732
3733 if (company.isSendPasswordResetLink()) {
3734 bodyProperty = PropsKeys.ADMIN_EMAIL_PASSWORD_RESET_BODY;
3735 prefix = "adminEmailPasswordReset";
3736 subjectProperty = PropsKeys.ADMIN_EMAIL_PASSWORD_RESET_SUBJECT;
3737 }
3738 else {
3739 bodyProperty = PropsKeys.ADMIN_EMAIL_PASSWORD_SENT_BODY;
3740 prefix = "adminEmailPasswordSent";
3741 subjectProperty = PropsKeys.ADMIN_EMAIL_PASSWORD_SENT_SUBJECT;
3742 }
3743
3744 if (Validator.isNull(body)) {
3745 localizedBodyMap = LocalizationUtil.getLocalizationMap(
3746 companyPortletPreferences, prefix + "Body", bodyProperty);
3747 }
3748
3749 if (Validator.isNull(subject)) {
3750 localizedSubjectMap = LocalizationUtil.getLocalizationMap(
3751 companyPortletPreferences, prefix + "Subject", subjectProperty);
3752 }
3753
3754 SubscriptionSender subscriptionSender = new SubscriptionSender();
3755
3756 subscriptionSender.setCompanyId(companyId);
3757 subscriptionSender.setContextAttributes(
3758 "[$PASSWORD_RESET_URL$]", passwordResetURL, "[$REMOTE_ADDRESS$]",
3759 serviceContext.getRemoteAddr(), "[$REMOTE_HOST$]",
3760 serviceContext.getRemoteHost(), "[$USER_ID$]", user.getUserId(),
3761 "[$USER_PASSWORD$]", newPassword, "[$USER_SCREENNAME$]",
3762 user.getScreenName());
3763 subscriptionSender.setFrom(fromAddress, fromName);
3764 subscriptionSender.setHtmlFormat(true);
3765 subscriptionSender.setLocalizedBodyMap(localizedBodyMap);
3766 subscriptionSender.setLocalizedSubjectMap(localizedSubjectMap);
3767 subscriptionSender.setMailId("user", user.getUserId());
3768 subscriptionSender.setServiceContext(serviceContext);
3769 subscriptionSender.setUserId(user.getUserId());
3770
3771 subscriptionSender.addRuntimeSubscribers(toAddress, toName);
3772
3773 subscriptionSender.flushNotificationsAsync();
3774
3775 return company.isSendPassword();
3776 }
3777
3778 @Override
3779 public boolean sendPasswordByEmailAddress(
3780 long companyId, String emailAddress)
3781 throws PortalException {
3782
3783 User user = userPersistence.findByC_EA(companyId, emailAddress);
3784
3785 return sendPassword(
3786 user.getCompanyId(), user.getEmailAddress(), null, null, null, null,
3787 ServiceContextThreadLocal.getServiceContext());
3788 }
3789
3790 @Override
3791 public boolean sendPasswordByScreenName(long companyId, String screenName)
3792 throws PortalException {
3793
3794 User user = userPersistence.findByC_SN(companyId, screenName);
3795
3796 return sendPassword(
3797 user.getCompanyId(), user.getEmailAddress(), null, null, null, null,
3798 ServiceContextThreadLocal.getServiceContext());
3799 }
3800
3801 @Override
3802 public boolean sendPasswordByUserId(long userId) throws PortalException {
3803 User user = userPersistence.findByPrimaryKey(userId);
3804
3805 return sendPassword(
3806 user.getCompanyId(), user.getEmailAddress(), null, null, null, null,
3807 ServiceContextThreadLocal.getServiceContext());
3808 }
3809
3810
3818 @Override
3819 public void setRoleUsers(long roleId, long[] userIds)
3820 throws PortalException {
3821
3822 rolePersistence.setUsers(roleId, userIds);
3823
3824 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
3825
3826 indexer.reindex(userIds);
3827
3828 PermissionCacheUtil.clearCache();
3829 }
3830
3831
3839 @Override
3840 @SuppressWarnings("deprecation")
3841 public void setUserGroupUsers(long userGroupId, long[] userIds)
3842 throws PortalException {
3843
3844 if (PropsValues.USER_GROUPS_COPY_LAYOUTS_TO_USER_PERSONAL_SITE) {
3845 userGroupLocalService.copyUserGroupLayouts(userGroupId, userIds);
3846 }
3847
3848 userGroupPersistence.setUsers(userGroupId, userIds);
3849
3850 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
3851
3852 indexer.reindex(userIds);
3853
3854 PermissionCacheUtil.clearCache();
3855 }
3856
3857
3864 @Override
3865 public void unsetGroupTeamsUsers(long groupId, long[] userIds)
3866 throws PortalException {
3867
3868 List<Team> teams = teamPersistence.findByGroupId(groupId);
3869
3870 for (Team team : teams) {
3871 unsetTeamUsers(team.getTeamId(), userIds);
3872 }
3873
3874 PermissionCacheUtil.clearCache();
3875 }
3876
3877
3886 @Override
3887 public void unsetGroupUsers(
3888 final long groupId, final long[] userIds,
3889 ServiceContext serviceContext)
3890 throws PortalException {
3891
3892 userGroupRoleLocalService.deleteUserGroupRoles(
3893 userIds, groupId, RoleConstants.TYPE_SITE);
3894
3895 unsetGroupTeamsUsers(groupId, userIds);
3896
3897 groupPersistence.removeUsers(groupId, userIds);
3898
3899 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
3900
3901 indexer.reindex(userIds);
3902
3903 PermissionCacheUtil.clearCache();
3904
3905 Callable<Void> callable = new Callable<Void>() {
3906
3907 @Override
3908 public Void call() throws Exception {
3909 Message message = new Message();
3910
3911 message.put("groupId", groupId);
3912 message.put("userIds", userIds);
3913
3914 MessageBusUtil.sendMessage(
3915 DestinationNames.SUBSCRIPTION_CLEAN_UP, message);
3916
3917 return null;
3918 }
3919
3920 };
3921
3922 TransactionCommitCallbackRegistryUtil.registerCallback(callable);
3923 }
3924
3925
3932 @Override
3933 public void unsetOrganizationUsers(
3934 long organizationId, final long[] userIds)
3935 throws PortalException {
3936
3937 Organization organization = organizationPersistence.findByPrimaryKey(
3938 organizationId);
3939
3940 final Group group = organization.getGroup();
3941
3942 userGroupRoleLocalService.deleteUserGroupRoles(
3943 userIds, group.getGroupId());
3944
3945 organizationPersistence.removeUsers(organizationId, userIds);
3946
3947 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
3948
3949 indexer.reindex(userIds);
3950
3951 PermissionCacheUtil.clearCache();
3952
3953 Callable<Void> callable = new Callable<Void>() {
3954
3955 @Override
3956 public Void call() throws Exception {
3957 Message message = new Message();
3958
3959 message.put("groupId", group.getGroupId());
3960 message.put("userIds", userIds);
3961
3962 MessageBusUtil.sendMessage(
3963 DestinationNames.SUBSCRIPTION_CLEAN_UP, message);
3964
3965 return null;
3966 }
3967
3968 };
3969
3970 TransactionCommitCallbackRegistryUtil.registerCallback(callable);
3971 }
3972
3973
3979 @Override
3980 public void unsetPasswordPolicyUsers(
3981 long passwordPolicyId, long[] userIds) {
3982
3983 passwordPolicyRelLocalService.deletePasswordPolicyRels(
3984 passwordPolicyId, User.class.getName(), userIds);
3985 }
3986
3987
3994 @Override
3995 public void unsetRoleUsers(long roleId, List<User> users)
3996 throws PortalException {
3997
3998 Role role = rolePersistence.findByPrimaryKey(roleId);
3999
4000 String roleName = role.getName();
4001
4002 if ((roleName.equals(RoleConstants.ADMINISTRATOR) &&
4003 (getRoleUsersCount(role.getRoleId()) <= 1)) ||
4004 roleName.equals(RoleConstants.USER)) {
4005
4006 return;
4007 }
4008
4009 rolePersistence.removeUsers(roleId, users);
4010
4011 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
4012
4013 indexer.reindex(users);
4014
4015 PermissionCacheUtil.clearCache();
4016 }
4017
4018
4025 @Override
4026 public void unsetRoleUsers(long roleId, long[] userIds)
4027 throws PortalException {
4028
4029 Role role = rolePersistence.findByPrimaryKey(roleId);
4030
4031 String roleName = role.getName();
4032
4033 if (roleName.equals(RoleConstants.USER) ||
4034 (roleName.equals(RoleConstants.ADMINISTRATOR) &&
4035 getRoleUsersCount(role.getRoleId()) <= 1)) {
4036
4037 return;
4038 }
4039
4040 rolePersistence.removeUsers(roleId, userIds);
4041
4042 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
4043
4044 indexer.reindex(userIds);
4045
4046 PermissionCacheUtil.clearCache();
4047 }
4048
4049
4056 @Override
4057 public void unsetTeamUsers(long teamId, long[] userIds)
4058 throws PortalException {
4059
4060 teamPersistence.removeUsers(teamId, userIds);
4061
4062 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
4063
4064 indexer.reindex(userIds);
4065
4066 PermissionCacheUtil.clearCache();
4067 }
4068
4069
4076 @Override
4077 public void unsetUserGroupUsers(long userGroupId, long[] userIds)
4078 throws PortalException {
4079
4080 userGroupPersistence.removeUsers(userGroupId, userIds);
4081
4082 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
4083
4084 indexer.reindex(userIds);
4085
4086 PermissionCacheUtil.clearCache();
4087 }
4088
4089
4098 @Override
4099 public User updateAgreedToTermsOfUse(
4100 long userId, boolean agreedToTermsOfUse)
4101 throws PortalException {
4102
4103 User user = userPersistence.findByPrimaryKey(userId);
4104
4105 user.setAgreedToTermsOfUse(agreedToTermsOfUse);
4106
4107 userPersistence.update(user);
4108
4109 return user;
4110 }
4111
4112
4122 @Override
4123 public void updateAsset(
4124 long userId, User user, long[] assetCategoryIds,
4125 String[] assetTagNames)
4126 throws PortalException {
4127
4128 User owner = userPersistence.findByPrimaryKey(userId);
4129
4130 Company company = companyPersistence.findByPrimaryKey(
4131 owner.getCompanyId());
4132
4133 Group companyGroup = company.getGroup();
4134
4135 assetEntryLocalService.updateEntry(
4136 userId, companyGroup.getGroupId(), user.getCreateDate(),
4137 user.getModifiedDate(), User.class.getName(), user.getUserId(),
4138 user.getUuid(), 0, assetCategoryIds, assetTagNames, false, null,
4139 null, null, null, user.getFullName(), null, null, null, null, 0, 0,
4140 null, false);
4141 }
4142
4143
4151 @Override
4152 public User updateCreateDate(long userId, Date createDate)
4153 throws PortalException {
4154
4155 User user = userPersistence.findByPrimaryKey(userId);
4156
4157 user.setCreateDate(createDate);
4158
4159 userPersistence.update(user);
4160
4161 return user;
4162 }
4163
4164
4174 @Override
4175 public User updateEmailAddress(
4176 long userId, String password, String emailAddress1,
4177 String emailAddress2)
4178 throws PortalException {
4179
4180 emailAddress1 = StringUtil.toLowerCase(emailAddress1.trim());
4181 emailAddress2 = StringUtil.toLowerCase(emailAddress2.trim());
4182
4183 User user = userPersistence.findByPrimaryKey(userId);
4184
4185 validateEmailAddress(user, emailAddress1, emailAddress2);
4186
4187 setEmailAddress(
4188 user, password, user.getFirstName(), user.getMiddleName(),
4189 user.getLastName(), emailAddress1);
4190
4191 userPersistence.update(user);
4192
4193 Contact contact = user.getContact();
4194
4195 contact.setEmailAddress(user.getEmailAddress());
4196
4197 contactPersistence.update(contact);
4198
4199 return user;
4200 }
4201
4202
4215 @Override
4216 public User updateEmailAddress(
4217 long userId, String password, String emailAddress1,
4218 String emailAddress2, ServiceContext serviceContext)
4219 throws PortalException {
4220
4221 emailAddress1 = StringUtil.toLowerCase(emailAddress1.trim());
4222 emailAddress2 = StringUtil.toLowerCase(emailAddress2.trim());
4223
4224 User user = userPersistence.findByPrimaryKey(userId);
4225
4226 validateEmailAddress(user, emailAddress1, emailAddress2);
4227
4228 Company company = companyPersistence.findByPrimaryKey(
4229 user.getCompanyId());
4230
4231 if (company.isStrangersVerify() &&
4232 !StringUtil.equalsIgnoreCase(
4233 emailAddress1, user.getEmailAddress())) {
4234
4235 sendEmailAddressVerification(user, emailAddress1, serviceContext);
4236 }
4237 else {
4238 setEmailAddress(
4239 user, password, user.getFirstName(), user.getMiddleName(),
4240 user.getLastName(), emailAddress1);
4241
4242 userPersistence.update(user);
4243
4244 Contact contact = user.getContact();
4245
4246 contact.setEmailAddress(user.getEmailAddress());
4247
4248 contactPersistence.update(contact);
4249 }
4250
4251 return user;
4252 }
4253
4254
4262 @Override
4263 public User updateEmailAddressVerified(
4264 long userId, boolean emailAddressVerified)
4265 throws PortalException {
4266
4267 User user = userPersistence.findByPrimaryKey(userId);
4268
4269 user.setEmailAddressVerified(emailAddressVerified);
4270
4271 userPersistence.update(user);
4272
4273 return user;
4274 }
4275
4276
4284 @Override
4285 public User updateFacebookId(long userId, long facebookId)
4286 throws PortalException {
4287
4288 User user = userPersistence.findByPrimaryKey(userId);
4289
4290 user.setFacebookId(facebookId);
4291
4292 userPersistence.update(user);
4293
4294 return user;
4295 }
4296
4297
4306 @Override
4307 public void updateGroups(
4308 long userId, long[] newGroupIds, ServiceContext serviceContext)
4309 throws PortalException {
4310
4311 boolean indexingEnabled = true;
4312
4313 if (serviceContext != null) {
4314 indexingEnabled = serviceContext.isIndexingEnabled();
4315 }
4316
4317 updateGroups(userId, newGroupIds, serviceContext, indexingEnabled);
4318 }
4319
4320
4358 @Override
4359 public User updateIncompleteUser(
4360 long creatorUserId, long companyId, boolean autoPassword,
4361 String password1, String password2, boolean autoScreenName,
4362 String screenName, String emailAddress, long facebookId,
4363 String openId, Locale locale, String firstName, String middleName,
4364 String lastName, int prefixId, int suffixId, boolean male,
4365 int birthdayMonth, int birthdayDay, int birthdayYear,
4366 String jobTitle, boolean updateUserInformation, boolean sendEmail,
4367 ServiceContext serviceContext)
4368 throws PortalException {
4369
4370 User user = getUserByEmailAddress(companyId, emailAddress);
4371
4372 if (user.getStatus() != WorkflowConstants.STATUS_INCOMPLETE) {
4373 throw new PortalException("Invalid user status");
4374 }
4375
4376 User defaultUser = getDefaultUser(companyId);
4377
4378 if (facebookId > 0) {
4379 autoPassword = false;
4380
4381 if ((password1 == null) || (password2 == null)) {
4382 password1 = PwdGenerator.getPassword();
4383 password2 = password1;
4384 }
4385
4386 sendEmail = false;
4387 }
4388
4389 if (updateUserInformation) {
4390 autoScreenName = false;
4391
4392 if (PrefsPropsUtil.getBoolean(
4393 companyId,
4394 PropsKeys.USERS_SCREEN_NAME_ALWAYS_AUTOGENERATE)) {
4395
4396 autoScreenName = true;
4397 }
4398
4399 validate(
4400 companyId, user.getUserId(), autoPassword, password1, password2,
4401 autoScreenName, screenName, emailAddress, openId, firstName,
4402 middleName, lastName, null);
4403
4404 if (!autoPassword) {
4405 if (Validator.isNull(password1) ||
4406 Validator.isNull(password2)) {
4407 throw new UserPasswordException(
4408 UserPasswordException.PASSWORD_INVALID);
4409 }
4410 }
4411
4412 if (autoScreenName) {
4413 ScreenNameGenerator screenNameGenerator =
4414 ScreenNameGeneratorFactory.getInstance();
4415
4416 try {
4417 screenName = screenNameGenerator.generate(
4418 companyId, user.getUserId(), emailAddress);
4419 }
4420 catch (Exception e) {
4421 throw new SystemException(e);
4422 }
4423 }
4424
4425 FullNameGenerator fullNameGenerator =
4426 FullNameGeneratorFactory.getInstance();
4427
4428 String fullName = fullNameGenerator.getFullName(
4429 firstName, middleName, lastName);
4430
4431 String greeting = LanguageUtil.format(
4432 locale, "welcome-x", fullName, false);
4433
4434 if (Validator.isNotNull(password1)) {
4435 user.setPassword(PasswordEncryptorUtil.encrypt(password1));
4436 user.setPasswordUnencrypted(password1);
4437 }
4438
4439 user.setPasswordEncrypted(true);
4440
4441 PasswordPolicy passwordPolicy = defaultUser.getPasswordPolicy();
4442
4443 if ((passwordPolicy != null) && passwordPolicy.isChangeable() &&
4444 passwordPolicy.isChangeRequired()) {
4445
4446 user.setPasswordReset(true);
4447 }
4448 else {
4449 user.setPasswordReset(false);
4450 }
4451
4452 user.setScreenName(screenName);
4453 user.setFacebookId(facebookId);
4454 user.setOpenId(openId);
4455 user.setLanguageId(locale.toString());
4456 user.setTimeZoneId(defaultUser.getTimeZoneId());
4457 user.setGreeting(greeting);
4458 user.setFirstName(firstName);
4459 user.setMiddleName(middleName);
4460 user.setLastName(lastName);
4461 user.setJobTitle(jobTitle);
4462 user.setExpandoBridgeAttributes(serviceContext);
4463
4464 Date birthday = getBirthday(
4465 birthdayMonth, birthdayDay, birthdayYear);
4466
4467 Contact contact = user.getContact();
4468
4469 contact.setFirstName(firstName);
4470 contact.setMiddleName(middleName);
4471 contact.setLastName(lastName);
4472 contact.setPrefixId(prefixId);
4473 contact.setSuffixId(suffixId);
4474 contact.setMale(male);
4475 contact.setBirthday(birthday);
4476 contact.setJobTitle(jobTitle);
4477
4478 contactPersistence.update(contact, serviceContext);
4479
4480
4481
4482 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
4483 User.class);
4484
4485 indexer.reindex(user);
4486 }
4487
4488 user.setStatus(WorkflowConstants.STATUS_DRAFT);
4489
4490 userPersistence.update(user, serviceContext);
4491
4492
4493
4494 long workflowUserId = creatorUserId;
4495
4496 if (workflowUserId == user.getUserId()) {
4497 workflowUserId = defaultUser.getUserId();
4498 }
4499
4500 ServiceContext workflowServiceContext = serviceContext;
4501
4502 if (workflowServiceContext == null) {
4503 workflowServiceContext = new ServiceContext();
4504 }
4505
4506 workflowServiceContext.setAttribute("autoPassword", autoPassword);
4507 workflowServiceContext.setAttribute("passwordUnencrypted", password1);
4508 workflowServiceContext.setAttribute("sendEmail", sendEmail);
4509
4510 WorkflowHandlerRegistryUtil.startWorkflowInstance(
4511 companyId, workflowUserId, User.class.getName(), user.getUserId(),
4512 user, workflowServiceContext);
4513
4514 return getUserByEmailAddress(companyId, emailAddress);
4515 }
4516
4517
4526 @Override
4527 public User updateJobTitle(long userId, String jobTitle)
4528 throws PortalException {
4529
4530 User user = userPersistence.findByPrimaryKey(userId);
4531
4532 user.setJobTitle(jobTitle);
4533
4534 userPersistence.update(user);
4535
4536 Contact contact = contactPersistence.findByPrimaryKey(
4537 user.getContactId());
4538
4539 contact.setJobTitle(jobTitle);
4540
4541 contactPersistence.update(contact);
4542
4543 return user;
4544 }
4545
4546
4554 @Override
4555 public User updateLastLogin(long userId, String loginIP)
4556 throws PortalException {
4557
4558 User user = userPersistence.findByPrimaryKey(userId);
4559
4560 Date lastLoginDate = user.getLoginDate();
4561
4562 if (lastLoginDate == null) {
4563 lastLoginDate = new Date();
4564 }
4565
4566 user.setLoginDate(new Date());
4567 user.setLoginIP(loginIP);
4568 user.setLastLoginDate(lastLoginDate);
4569 user.setLastLoginIP(user.getLoginIP());
4570 user.setFailedLoginAttempts(0);
4571
4572 userPersistence.update(user);
4573
4574 return user;
4575 }
4576
4577
4585 @Override
4586 public User updateLockout(User user, boolean lockout)
4587 throws PortalException {
4588
4589 PasswordPolicy passwordPolicy = user.getPasswordPolicy();
4590
4591 if ((passwordPolicy == null) || !passwordPolicy.isLockout()) {
4592 return user;
4593 }
4594
4595 Date lockoutDate = null;
4596
4597 if (lockout) {
4598 lockoutDate = new Date();
4599 }
4600
4601 user.setLockout(lockout);
4602 user.setLockoutDate(lockoutDate);
4603
4604 if (!lockout) {
4605 user.setFailedLoginAttempts(0);
4606 }
4607
4608 userPersistence.update(user);
4609
4610 return user;
4611 }
4612
4613
4623 @Override
4624 public User updateLockoutByEmailAddress(
4625 long companyId, String emailAddress, boolean lockout)
4626 throws PortalException {
4627
4628 User user = getUserByEmailAddress(companyId, emailAddress);
4629
4630 return updateLockout(user, lockout);
4631 }
4632
4633
4641 @Override
4642 public User updateLockoutById(long userId, boolean lockout)
4643 throws PortalException {
4644
4645 User user = userPersistence.findByPrimaryKey(userId);
4646
4647 return updateLockout(user, lockout);
4648 }
4649
4650
4659 @Override
4660 public User updateLockoutByScreenName(
4661 long companyId, String screenName, boolean lockout)
4662 throws PortalException {
4663
4664 User user = getUserByScreenName(companyId, screenName);
4665
4666 return updateLockout(user, lockout);
4667 }
4668
4669
4677 @Override
4678 public User updateModifiedDate(long userId, Date modifiedDate)
4679 throws PortalException {
4680
4681 User user = userPersistence.findByPrimaryKey(userId);
4682
4683 user.setModifiedDate(modifiedDate);
4684
4685 userPersistence.update(user);
4686
4687 return user;
4688 }
4689
4690
4698 @Override
4699 public User updateOpenId(long userId, String openId)
4700 throws PortalException {
4701
4702 openId = openId.trim();
4703
4704 User user = userPersistence.findByPrimaryKey(userId);
4705
4706 user.setOpenId(openId);
4707
4708 userPersistence.update(user);
4709
4710 return user;
4711 }
4712
4713
4723 @Override
4724 public void updateOrganizations(
4725 long userId, long[] newOrganizationIds,
4726 ServiceContext serviceContext)
4727 throws PortalException {
4728
4729 updateOrganizations(
4730 userId, newOrganizationIds, serviceContext.isIndexingEnabled());
4731 }
4732
4733
4744 @Override
4745 public User updatePassword(
4746 long userId, String password1, String password2,
4747 boolean passwordReset)
4748 throws PortalException {
4749
4750 return updatePassword(
4751 userId, password1, password2, passwordReset, false);
4752 }
4753
4754
4768 @Override
4769 public User updatePassword(
4770 long userId, String password1, String password2,
4771 boolean passwordReset, boolean silentUpdate)
4772 throws PortalException {
4773
4774 User user = userPersistence.findByPrimaryKey(userId);
4775
4776 if (!silentUpdate) {
4777 validatePassword(user.getCompanyId(), userId, password1, password2);
4778 }
4779
4780 String oldEncPwd = user.getPassword();
4781
4782 if (!user.isPasswordEncrypted()) {
4783 oldEncPwd = PasswordEncryptorUtil.encrypt(user.getPassword());
4784 }
4785
4786 String newEncPwd = PasswordEncryptorUtil.encrypt(password1);
4787
4788 if (user.hasCompanyMx()) {
4789 mailService.updatePassword(user.getCompanyId(), userId, password1);
4790 }
4791
4792 user.setPassword(newEncPwd);
4793 user.setPasswordUnencrypted(password1);
4794 user.setPasswordEncrypted(true);
4795 user.setPasswordReset(passwordReset);
4796
4797 if (!silentUpdate || (user.getPasswordModifiedDate() == null)) {
4798 user.setPasswordModifiedDate(new Date());
4799 }
4800
4801 user.setDigest(StringPool.BLANK);
4802 user.setGraceLoginCount(0);
4803
4804 if (!silentUpdate) {
4805 user.setPasswordModified(true);
4806 }
4807
4808 try {
4809 userPersistence.update(user);
4810 }
4811 catch (ModelListenerException mle) {
4812 String msg = GetterUtil.getString(mle.getCause().getMessage());
4813
4814 if (LDAPSettingsUtil.isPasswordPolicyEnabled(user.getCompanyId())) {
4815 String passwordHistory = PrefsPropsUtil.getString(
4816 user.getCompanyId(), PropsKeys.LDAP_ERROR_PASSWORD_HISTORY);
4817
4818 if (msg.contains(passwordHistory)) {
4819 throw new UserPasswordException(
4820 UserPasswordException.PASSWORD_ALREADY_USED);
4821 }
4822 }
4823
4824 throw new UserPasswordException(
4825 UserPasswordException.PASSWORD_INVALID);
4826 }
4827
4828 if (!silentUpdate) {
4829 user.setPasswordModified(false);
4830
4831 passwordTrackerLocalService.trackPassword(userId, oldEncPwd);
4832 }
4833
4834 return user;
4835 }
4836
4837
4850 @Override
4851 public User updatePasswordManually(
4852 long userId, String password, boolean passwordEncrypted,
4853 boolean passwordReset, Date passwordModifiedDate)
4854 throws PortalException {
4855
4856
4857
4858 User user = userPersistence.findByPrimaryKey(userId);
4859
4860 user.setPassword(password);
4861 user.setPasswordEncrypted(passwordEncrypted);
4862 user.setPasswordReset(passwordReset);
4863 user.setPasswordModifiedDate(passwordModifiedDate);
4864 user.setDigest(StringPool.BLANK);
4865
4866 userPersistence.update(user);
4867
4868 return user;
4869 }
4870
4871
4881 @Override
4882 public User updatePasswordReset(long userId, boolean passwordReset)
4883 throws PortalException {
4884
4885 User user = userPersistence.findByPrimaryKey(userId);
4886
4887 user.setPasswordReset(passwordReset);
4888
4889 userPersistence.update(user);
4890
4891 return user;
4892 }
4893
4894
4903 @Override
4904 public User updatePortrait(long userId, byte[] bytes)
4905 throws PortalException {
4906
4907 User user = userPersistence.findByPrimaryKey(userId);
4908
4909 PortalUtil.updateImageId(
4910 user, true, bytes, "portraitId",
4911 PrefsPropsUtil.getLong(PropsKeys.USERS_IMAGE_MAX_SIZE),
4912 PropsValues.USERS_IMAGE_MAX_HEIGHT,
4913 PropsValues.USERS_IMAGE_MAX_WIDTH);
4914
4915 return userPersistence.update(user);
4916 }
4917
4918
4928 @Override
4929 public User updateReminderQuery(long userId, String question, String answer)
4930 throws PortalException {
4931
4932 validateReminderQuery(question, answer);
4933
4934 User user = userPersistence.findByPrimaryKey(userId);
4935
4936 user.setReminderQueryQuestion(question);
4937 user.setReminderQueryAnswer(answer);
4938
4939 userPersistence.update(user);
4940
4941 return user;
4942 }
4943
4944
4953 @Override
4954 public User updateScreenName(long userId, String screenName)
4955 throws PortalException {
4956
4957
4958
4959 User user = userPersistence.findByPrimaryKey(userId);
4960
4961 screenName = getLogin(screenName);
4962
4963 validateScreenName(user.getCompanyId(), userId, screenName);
4964
4965 if (!StringUtil.equalsIgnoreCase(user.getScreenName(), screenName)) {
4966 user.setDigest(StringPool.BLANK);
4967 }
4968
4969 user.setScreenName(screenName);
4970
4971 userPersistence.update(user);
4972
4973
4974
4975 Group group = groupLocalService.getUserGroup(
4976 user.getCompanyId(), userId);
4977
4978 group.setFriendlyURL(StringPool.SLASH + screenName);
4979
4980 groupPersistence.update(group);
4981
4982 return user;
4983 }
4984
4985
4996 @Deprecated
4997 @Override
4998 public User updateStatus(long userId, int status) throws PortalException {
4999 return updateStatus(userId, status, new ServiceContext());
5000 }
5001
5002
5013 @Override
5014 public User updateStatus(
5015 long userId, int status, ServiceContext serviceContext)
5016 throws PortalException {
5017
5018 User user = userPersistence.findByPrimaryKey(userId);
5019
5020 if ((status == WorkflowConstants.STATUS_APPROVED) &&
5021 (user.getStatus() != WorkflowConstants.STATUS_APPROVED)) {
5022
5023 validateCompanyMaxUsers(user.getCompanyId());
5024 }
5025
5026 String passwordUnencrypted = (String)serviceContext.getAttribute(
5027 "passwordUnencrypted");
5028
5029 if (Validator.isNotNull(passwordUnencrypted)) {
5030 user.setPasswordUnencrypted(passwordUnencrypted);
5031 }
5032
5033 user.setStatus(status);
5034
5035 userPersistence.update(user);
5036
5037 reindex(user);
5038
5039 return user;
5040 }
5041
5042
5099 @Override
5100 @SuppressWarnings("deprecation")
5101 public User updateUser(
5102 long userId, String oldPassword, String newPassword1,
5103 String newPassword2, boolean passwordReset,
5104 String reminderQueryQuestion, String reminderQueryAnswer,
5105 String screenName, String emailAddress, long facebookId,
5106 String openId, boolean portrait, byte[] portraitBytes,
5107 String languageId, String timeZoneId, String greeting,
5108 String comments, String firstName, String middleName,
5109 String lastName, int prefixId, int suffixId, boolean male,
5110 int birthdayMonth, int birthdayDay, int birthdayYear, String smsSn,
5111 String aimSn, String facebookSn, String icqSn, String jabberSn,
5112 String msnSn, String mySpaceSn, String skypeSn, String twitterSn,
5113 String ymSn, String jobTitle, long[] groupIds,
5114 long[] organizationIds, long[] roleIds,
5115 List<UserGroupRole> userGroupRoles, long[] userGroupIds,
5116 ServiceContext serviceContext)
5117 throws PortalException {
5118
5119
5120
5121 User user = userPersistence.findByPrimaryKey(userId);
5122 Company company = companyPersistence.findByPrimaryKey(
5123 user.getCompanyId());
5124 String password = oldPassword;
5125 screenName = getLogin(screenName);
5126 emailAddress = StringUtil.toLowerCase(emailAddress.trim());
5127 openId = openId.trim();
5128 String oldFullName = user.getFullName();
5129 aimSn = StringUtil.toLowerCase(aimSn.trim());
5130 facebookSn = StringUtil.toLowerCase(facebookSn.trim());
5131 icqSn = StringUtil.toLowerCase(icqSn.trim());
5132 jabberSn = StringUtil.toLowerCase(jabberSn.trim());
5133 msnSn = StringUtil.toLowerCase(msnSn.trim());
5134 mySpaceSn = StringUtil.toLowerCase(mySpaceSn.trim());
5135 skypeSn = StringUtil.toLowerCase(skypeSn.trim());
5136 twitterSn = StringUtil.toLowerCase(twitterSn.trim());
5137 ymSn = StringUtil.toLowerCase(ymSn.trim());
5138 Date now = new Date();
5139
5140 EmailAddressGenerator emailAddressGenerator =
5141 EmailAddressGeneratorFactory.getInstance();
5142
5143 if (emailAddressGenerator.isGenerated(emailAddress)) {
5144 emailAddress = StringPool.BLANK;
5145 }
5146
5147 if (!PropsValues.USERS_EMAIL_ADDRESS_REQUIRED &&
5148 Validator.isNull(emailAddress)) {
5149
5150 emailAddress = emailAddressGenerator.generate(
5151 user.getCompanyId(), userId);
5152 }
5153
5154 validate(
5155 userId, screenName, emailAddress, openId, firstName, middleName,
5156 lastName, smsSn);
5157
5158 if (Validator.isNotNull(newPassword1) ||
5159 Validator.isNotNull(newPassword2)) {
5160
5161 user = updatePassword(
5162 userId, newPassword1, newPassword2, passwordReset);
5163
5164 password = newPassword1;
5165
5166 user.setDigest(StringPool.BLANK);
5167 }
5168
5169 user.setModifiedDate(now);
5170
5171 if (user.getContactId() <= 0) {
5172 user.setContactId(counterLocalService.increment());
5173 }
5174
5175 user.setPasswordReset(passwordReset);
5176
5177 if (Validator.isNotNull(reminderQueryQuestion) &&
5178 Validator.isNotNull(reminderQueryAnswer)) {
5179
5180 user.setReminderQueryQuestion(reminderQueryQuestion);
5181 user.setReminderQueryAnswer(reminderQueryAnswer);
5182 }
5183
5184 if (!StringUtil.equalsIgnoreCase(user.getScreenName(), screenName)) {
5185 user.setScreenName(screenName);
5186
5187 user.setDigest(StringPool.BLANK);
5188 }
5189
5190 boolean sendEmailAddressVerification = false;
5191
5192 if (company.isStrangersVerify() &&
5193 !StringUtil.equalsIgnoreCase(
5194 emailAddress, user.getEmailAddress())) {
5195
5196 sendEmailAddressVerification = true;
5197 }
5198 else {
5199 setEmailAddress(
5200 user, password, firstName, middleName, lastName, emailAddress);
5201 }
5202
5203 if (serviceContext != null) {
5204 String uuid = serviceContext.getUuid();
5205
5206 if (Validator.isNotNull(uuid)) {
5207 user.setUuid(uuid);
5208 }
5209 }
5210
5211 user.setFacebookId(facebookId);
5212
5213 Long ldapServerId = (Long)serviceContext.getAttribute("ldapServerId");
5214
5215 if (ldapServerId != null) {
5216 user.setLdapServerId(ldapServerId);
5217 }
5218
5219 user.setOpenId(openId);
5220
5221 PortalUtil.updateImageId(
5222 user, portrait, portraitBytes, "portraitId",
5223 PrefsPropsUtil.getLong(PropsKeys.USERS_IMAGE_MAX_SIZE),
5224 PropsValues.USERS_IMAGE_MAX_HEIGHT,
5225 PropsValues.USERS_IMAGE_MAX_WIDTH);
5226
5227 user.setLanguageId(languageId);
5228 user.setTimeZoneId(timeZoneId);
5229 user.setGreeting(greeting);
5230 user.setComments(comments);
5231 user.setFirstName(firstName);
5232 user.setMiddleName(middleName);
5233 user.setLastName(lastName);
5234 user.setJobTitle(jobTitle);
5235 user.setExpandoBridgeAttributes(serviceContext);
5236
5237 userPersistence.update(user, serviceContext);
5238
5239
5240
5241 Date birthday = getBirthday(birthdayMonth, birthdayDay, birthdayYear);
5242
5243 long contactId = user.getContactId();
5244
5245 Contact contact = contactPersistence.fetchByPrimaryKey(contactId);
5246
5247 if (contact == null) {
5248 contact = contactPersistence.create(contactId);
5249
5250 contact.setCompanyId(user.getCompanyId());
5251 contact.setUserName(StringPool.BLANK);
5252 contact.setCreateDate(now);
5253 contact.setClassName(User.class.getName());
5254 contact.setClassPK(user.getUserId());
5255 contact.setAccountId(company.getAccountId());
5256 contact.setParentContactId(
5257 ContactConstants.DEFAULT_PARENT_CONTACT_ID);
5258 }
5259
5260 contact.setModifiedDate(now);
5261 contact.setEmailAddress(user.getEmailAddress());
5262 contact.setFirstName(firstName);
5263 contact.setMiddleName(middleName);
5264 contact.setLastName(lastName);
5265 contact.setPrefixId(prefixId);
5266 contact.setSuffixId(suffixId);
5267 contact.setMale(male);
5268 contact.setBirthday(birthday);
5269 contact.setSmsSn(smsSn);
5270 contact.setAimSn(aimSn);
5271 contact.setFacebookSn(facebookSn);
5272 contact.setIcqSn(icqSn);
5273 contact.setJabberSn(jabberSn);
5274 contact.setMsnSn(msnSn);
5275 contact.setMySpaceSn(mySpaceSn);
5276 contact.setSkypeSn(skypeSn);
5277 contact.setTwitterSn(twitterSn);
5278 contact.setYmSn(ymSn);
5279 contact.setJobTitle(jobTitle);
5280
5281 contactPersistence.update(contact, serviceContext);
5282
5283
5284
5285 Group group = groupLocalService.getUserGroup(
5286 user.getCompanyId(), userId);
5287
5288 group.setFriendlyURL(StringPool.SLASH + screenName);
5289
5290 groupPersistence.update(group);
5291
5292
5293
5294
5295
5296
5297
5298 List<UserGroupRole> previousUserGroupRoles =
5299 userGroupRolePersistence.findByUserId(userId);
5300
5301 updateGroups(userId, groupIds, serviceContext, false);
5302 updateOrganizations(userId, organizationIds, false);
5303
5304
5305
5306 if (roleIds != null) {
5307 roleIds = UsersAdminUtil.addRequiredRoles(user, roleIds);
5308
5309 userPersistence.setRoles(userId, roleIds);
5310 }
5311
5312
5313
5314 updateUserGroupRoles(
5315 user, groupIds, organizationIds, userGroupRoles,
5316 previousUserGroupRoles);
5317
5318
5319
5320 if (userGroupIds != null) {
5321 if (PropsValues.USER_GROUPS_COPY_LAYOUTS_TO_USER_PERSONAL_SITE) {
5322 userGroupLocalService.copyUserGroupLayouts(
5323 userGroupIds, userId);
5324 }
5325
5326 userPersistence.setUserGroups(userId, userGroupIds);
5327 }
5328
5329
5330
5331 announcementsDeliveryLocalService.getUserDeliveries(user.getUserId());
5332
5333
5334
5335 if (serviceContext != null) {
5336 updateAsset(
5337 userId, user, serviceContext.getAssetCategoryIds(),
5338 serviceContext.getAssetTagNames());
5339 }
5340
5341
5342
5343 if (GetterUtil.getBoolean(
5344 PropsKeys.USERS_UPDATE_USER_NAME + MBMessage.class.getName()) &&
5345 !oldFullName.equals(user.getFullName())) {
5346
5347 mbMessageLocalService.updateUserName(userId, user.getFullName());
5348 }
5349
5350
5351
5352 if ((serviceContext == null) || serviceContext.isIndexingEnabled()) {
5353 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
5354 User.class);
5355
5356 indexer.reindex(user);
5357 }
5358
5359
5360
5361 if ((serviceContext != null) && sendEmailAddressVerification) {
5362 sendEmailAddressVerification(user, emailAddress, serviceContext);
5363 }
5364
5365
5366
5367 PermissionCacheUtil.clearCache();
5368
5369 return user;
5370 }
5371
5372
5434 @Deprecated
5435 @Override
5436 public User updateUser(
5437 long userId, String oldPassword, String newPassword1,
5438 String newPassword2, boolean passwordReset,
5439 String reminderQueryQuestion, String reminderQueryAnswer,
5440 String screenName, String emailAddress, long facebookId,
5441 String openId, String languageId, String timeZoneId,
5442 String greeting, String comments, String firstName,
5443 String middleName, String lastName, int prefixId, int suffixId,
5444 boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
5445 String smsSn, String aimSn, String facebookSn, String icqSn,
5446 String jabberSn, String msnSn, String mySpaceSn, String skypeSn,
5447 String twitterSn, String ymSn, String jobTitle, long[] groupIds,
5448 long[] organizationIds, long[] roleIds,
5449 List<UserGroupRole> userGroupRoles, long[] userGroupIds,
5450 ServiceContext serviceContext)
5451 throws PortalException {
5452
5453 return updateUser(
5454 userId, oldPassword, newPassword1, newPassword2, passwordReset,
5455 reminderQueryQuestion, reminderQueryAnswer, screenName,
5456 emailAddress, facebookId, openId, true, null, languageId,
5457 timeZoneId, greeting, comments, firstName, middleName, lastName,
5458 prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
5459 smsSn, aimSn, facebookSn, icqSn, jabberSn, msnSn, mySpaceSn,
5460 skypeSn, twitterSn, ymSn, jobTitle, groupIds, organizationIds,
5461 roleIds, userGroupRoles, userGroupIds, serviceContext);
5462 }
5463
5464
5472 @Override
5473 public void verifyEmailAddress(String ticketKey) throws PortalException {
5474 Ticket ticket = ticketLocalService.getTicket(ticketKey);
5475
5476 if (ticket.isExpired() ||
5477 (ticket.getType() != TicketConstants.TYPE_EMAIL_ADDRESS)) {
5478
5479 throw new NoSuchTicketException("{ticketKey=" + ticketKey + "}");
5480 }
5481
5482 User user = userPersistence.findByPrimaryKey(ticket.getClassPK());
5483
5484 String emailAddress = ticket.getExtraInfo();
5485
5486 emailAddress = StringUtil.toLowerCase(emailAddress).trim();
5487
5488 if (!emailAddress.equals(user.getEmailAddress())) {
5489 if (userPersistence.fetchByC_EA(
5490 user.getCompanyId(), emailAddress) != null) {
5491
5492 throw new UserEmailAddressException.MustNotBeDuplicate(
5493 user.getUserId(), emailAddress);
5494 }
5495
5496 setEmailAddress(
5497 user, StringPool.BLANK, user.getFirstName(),
5498 user.getMiddleName(), user.getLastName(), emailAddress);
5499
5500 Contact contact = user.getContact();
5501
5502 contact.setEmailAddress(user.getEmailAddress());
5503
5504 contactPersistence.update(contact);
5505 }
5506
5507 user.setEmailAddressVerified(true);
5508
5509 userPersistence.update(user);
5510
5511 ticketLocalService.deleteTicket(ticket);
5512 }
5513
5514 protected void addDefaultRolesAndTeams(long groupId, long[] userIds)
5515 throws PortalException {
5516
5517 List<Role> defaultSiteRoles = new ArrayList<Role>();
5518
5519 Group group = groupLocalService.getGroup(groupId);
5520
5521 UnicodeProperties typeSettingsProperties =
5522 group.getTypeSettingsProperties();
5523
5524 long[] defaultSiteRoleIds = StringUtil.split(
5525 typeSettingsProperties.getProperty("defaultSiteRoleIds"), 0L);
5526
5527 for (long defaultSiteRoleId : defaultSiteRoleIds) {
5528 Role defaultSiteRole = rolePersistence.fetchByPrimaryKey(
5529 defaultSiteRoleId);
5530
5531 if (defaultSiteRole == null) {
5532 if (_log.isWarnEnabled()) {
5533 _log.warn("Unable to find role " + defaultSiteRoleId);
5534 }
5535
5536 continue;
5537 }
5538
5539 defaultSiteRoles.add(defaultSiteRole);
5540 }
5541
5542 List<Team> defaultTeams = new ArrayList<Team>();
5543
5544 long[] defaultTeamIds = StringUtil.split(
5545 typeSettingsProperties.getProperty("defaultTeamIds"), 0L);
5546
5547 for (long defaultTeamId : defaultTeamIds) {
5548 Team defaultTeam = teamPersistence.findByPrimaryKey(defaultTeamId);
5549
5550 if (defaultTeam == null) {
5551 if (_log.isWarnEnabled()) {
5552 _log.warn("Unable to find team " + defaultTeamId);
5553 }
5554
5555 continue;
5556 }
5557
5558 defaultTeams.add(defaultTeam);
5559 }
5560
5561 for (long userId : userIds) {
5562 Set<Long> userRoleIdsSet = new HashSet<Long>();
5563
5564 for (Role role : defaultSiteRoles) {
5565 if (!userPersistence.containsRole(userId, role.getRoleId())) {
5566 userRoleIdsSet.add(role.getRoleId());
5567 }
5568 }
5569
5570 long[] userRoleIds = ArrayUtil.toArray(
5571 userRoleIdsSet.toArray(new Long[userRoleIdsSet.size()]));
5572
5573 userGroupRoleLocalService.addUserGroupRoles(
5574 userId, groupId, userRoleIds);
5575
5576 Set<Long> userTeamIdsSet = new HashSet<Long>();
5577
5578 for (Team team : defaultTeams) {
5579 if (!userPersistence.containsTeam(userId, team.getTeamId())) {
5580 userTeamIdsSet.add(team.getTeamId());
5581 }
5582 }
5583
5584 long[] userTeamIds = ArrayUtil.toArray(
5585 userTeamIdsSet.toArray(new Long[userTeamIdsSet.size()]));
5586
5587 userPersistence.addTeams(userId, userTeamIds);
5588 }
5589 }
5590
5591
5636 protected int authenticate(
5637 long companyId, String login, String password, String authType,
5638 Map<String, String[]> headerMap, Map<String, String[]> parameterMap,
5639 Map<String, Object> resultsMap)
5640 throws PortalException {
5641
5642 if (PropsValues.AUTH_LOGIN_DISABLED) {
5643 return Authenticator.FAILURE;
5644 }
5645
5646 login = StringUtil.toLowerCase(login.trim());
5647
5648 long userId = GetterUtil.getLong(login);
5649
5650
5651
5652 if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
5653 if (Validator.isNull(login)) {
5654 throw new UserEmailAddressException.MustNotBeNull();
5655 }
5656 }
5657 else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
5658 if (Validator.isNull(login)) {
5659 throw new UserScreenNameException.MustNotBeNull();
5660 }
5661 }
5662 else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
5663 if (Validator.isNull(login)) {
5664 throw new UserIdException.MustNotBeNull();
5665 }
5666 }
5667
5668 if (Validator.isNull(password)) {
5669 throw new UserPasswordException(
5670 UserPasswordException.PASSWORD_INVALID);
5671 }
5672
5673 int authResult = Authenticator.FAILURE;
5674
5675
5676
5677 if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
5678 authResult = AuthPipeline.authenticateByEmailAddress(
5679 PropsKeys.AUTH_PIPELINE_PRE, companyId, login, password,
5680 headerMap, parameterMap);
5681 }
5682 else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
5683 authResult = AuthPipeline.authenticateByScreenName(
5684 PropsKeys.AUTH_PIPELINE_PRE, companyId, login, password,
5685 headerMap, parameterMap);
5686 }
5687 else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
5688 authResult = AuthPipeline.authenticateByUserId(
5689 PropsKeys.AUTH_PIPELINE_PRE, companyId, userId, password,
5690 headerMap, parameterMap);
5691 }
5692
5693
5694
5695 User user = null;
5696
5697 if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
5698 user = fetchUserByEmailAddress(companyId, login);
5699 }
5700 else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
5701 user = fetchUserByScreenName(companyId, login);
5702 }
5703 else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
5704 user = userPersistence.fetchByPrimaryKey(GetterUtil.getLong(login));
5705 }
5706
5707 if (user == null) {
5708 return Authenticator.DNE;
5709 }
5710
5711 if (user.isDefaultUser()) {
5712 if (_log.isInfoEnabled()) {
5713 _log.info("Authentication is disabled for the default user");
5714 }
5715
5716 return Authenticator.DNE;
5717 }
5718 else if (!user.isActive()) {
5719 if (_log.isInfoEnabled()) {
5720 _log.info(
5721 "Authentication is disabled for inactive user " +
5722 user.getUserId());
5723 }
5724
5725 return Authenticator.FAILURE;
5726 }
5727
5728 if (!user.isPasswordEncrypted()) {
5729 user.setPassword(PasswordEncryptorUtil.encrypt(user.getPassword()));
5730 user.setPasswordEncrypted(true);
5731
5732 userPersistence.update(user);
5733 }
5734
5735
5736
5737
5738 checkLockout(user);
5739
5740 checkPasswordExpired(user);
5741
5742
5743
5744 boolean skipLiferayCheck = false;
5745
5746 if (authResult == Authenticator.SKIP_LIFERAY_CHECK) {
5747 authResult = Authenticator.SUCCESS;
5748
5749 skipLiferayCheck = true;
5750 }
5751 else if ((authResult == Authenticator.SUCCESS) &&
5752 PropsValues.AUTH_PIPELINE_ENABLE_LIFERAY_CHECK) {
5753
5754 boolean authenticated = PwdAuthenticator.authenticate(
5755 login, password, user.getPassword());
5756
5757 if (authenticated) {
5758 authResult = Authenticator.SUCCESS;
5759 }
5760 else {
5761 authResult = Authenticator.FAILURE;
5762 }
5763 }
5764
5765
5766
5767 if (authResult == Authenticator.SUCCESS) {
5768 if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
5769 authResult = AuthPipeline.authenticateByEmailAddress(
5770 PropsKeys.AUTH_PIPELINE_POST, companyId, login, password,
5771 headerMap, parameterMap);
5772 }
5773 else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
5774 authResult = AuthPipeline.authenticateByScreenName(
5775 PropsKeys.AUTH_PIPELINE_POST, companyId, login, password,
5776 headerMap, parameterMap);
5777 }
5778 else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
5779 authResult = AuthPipeline.authenticateByUserId(
5780 PropsKeys.AUTH_PIPELINE_POST, companyId, userId, password,
5781 headerMap, parameterMap);
5782 }
5783 }
5784
5785 if (authResult == Authenticator.SUCCESS) {
5786 if (resultsMap != null) {
5787 resultsMap.put("userId", user.getUserId());
5788 }
5789
5790
5791
5792 if (skipLiferayCheck ||
5793 !PropsValues.AUTH_PIPELINE_ENABLE_LIFERAY_CHECK ||
5794 Validator.isNull(user.getDigest())) {
5795
5796 user = userPersistence.fetchByPrimaryKey(user.getUserId());
5797
5798 String digest = user.getDigest(password);
5799
5800 user.setDigest(digest);
5801
5802 userPersistence.update(user);
5803 }
5804 }
5805
5806
5807
5808 if (authResult == Authenticator.FAILURE) {
5809 try {
5810 if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
5811 AuthPipeline.onFailureByEmailAddress(
5812 PropsKeys.AUTH_FAILURE, companyId, login, headerMap,
5813 parameterMap);
5814 }
5815 else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
5816 AuthPipeline.onFailureByScreenName(
5817 PropsKeys.AUTH_FAILURE, companyId, login, headerMap,
5818 parameterMap);
5819 }
5820 else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
5821 AuthPipeline.onFailureByUserId(
5822 PropsKeys.AUTH_FAILURE, companyId, userId, headerMap,
5823 parameterMap);
5824 }
5825
5826 user = userPersistence.fetchByPrimaryKey(user.getUserId());
5827
5828 if (user == null) {
5829 return Authenticator.DNE;
5830 }
5831
5832
5833
5834 if (!LDAPSettingsUtil.isPasswordPolicyEnabled(
5835 user.getCompanyId())) {
5836
5837 PasswordPolicy passwordPolicy = user.getPasswordPolicy();
5838
5839 user = userPersistence.fetchByPrimaryKey(user.getUserId());
5840
5841 int failedLoginAttempts = user.getFailedLoginAttempts();
5842 int maxFailures = passwordPolicy.getMaxFailure();
5843
5844 if ((failedLoginAttempts >= maxFailures) &&
5845 (maxFailures != 0)) {
5846
5847 if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
5848 AuthPipeline.onMaxFailuresByEmailAddress(
5849 PropsKeys.AUTH_MAX_FAILURES, companyId, login,
5850 headerMap, parameterMap);
5851 }
5852 else if (authType.equals(
5853 CompanyConstants.AUTH_TYPE_SN)) {
5854
5855 AuthPipeline.onMaxFailuresByScreenName(
5856 PropsKeys.AUTH_MAX_FAILURES, companyId, login,
5857 headerMap, parameterMap);
5858 }
5859 else if (authType.equals(
5860 CompanyConstants.AUTH_TYPE_ID)) {
5861
5862 AuthPipeline.onMaxFailuresByUserId(
5863 PropsKeys.AUTH_MAX_FAILURES, companyId, userId,
5864 headerMap, parameterMap);
5865 }
5866 }
5867 }
5868 }
5869 catch (Exception e) {
5870 _log.error(e, e);
5871 }
5872 }
5873
5874
5875
5876 return authResult;
5877 }
5878
5879 protected SearchContext buildSearchContext(
5880 long companyId, String firstName, String middleName, String lastName,
5881 String fullName, String screenName, String emailAddress, String street,
5882 String city, String zip, String region, String country, int status,
5883 LinkedHashMap<String, Object> params, boolean andSearch, int start,
5884 int end, Sort sort) {
5885
5886 SearchContext searchContext = new SearchContext();
5887
5888 searchContext.setAndSearch(andSearch);
5889
5890 Map<String, Serializable> attributes =
5891 new HashMap<String, Serializable>();
5892
5893 attributes.put("city", city);
5894 attributes.put("country", country);
5895 attributes.put("emailAddress", emailAddress);
5896 attributes.put("firstName", firstName);
5897 attributes.put("fullName", fullName);
5898 attributes.put("lastName", lastName);
5899 attributes.put("middleName", middleName);
5900 attributes.put("params", params);
5901 attributes.put("region", region);
5902 attributes.put("screenName", screenName);
5903 attributes.put("street", street);
5904 attributes.put("status", status);
5905 attributes.put("zip", zip);
5906
5907 searchContext.setAttributes(attributes);
5908
5909 searchContext.setCompanyId(companyId);
5910 searchContext.setEnd(end);
5911
5912 if (params != null) {
5913 String keywords = (String)params.remove("keywords");
5914
5915 if (Validator.isNotNull(keywords)) {
5916 searchContext.setKeywords(keywords);
5917 }
5918 }
5919
5920 if (sort != null) {
5921 searchContext.setSorts(sort);
5922 }
5923
5924 searchContext.setStart(start);
5925
5926 QueryConfig queryConfig = searchContext.getQueryConfig();
5927
5928 queryConfig.setHighlightEnabled(false);
5929 queryConfig.setScoreEnabled(false);
5930
5931 return searchContext;
5932 }
5933
5934 protected Date getBirthday(
5935 int birthdayMonth, int birthdayDay, int birthdayYear)
5936 throws PortalException {
5937
5938 Date birthday = PortalUtil.getDate(
5939 birthdayMonth, birthdayDay, birthdayYear,
5940 ContactBirthdayException.class);
5941
5942 Date now = new Date();
5943
5944 if (birthday.after(now)) {
5945 throw new ContactBirthdayException();
5946 }
5947
5948 return birthday;
5949 }
5950
5951 protected String getLogin(String login) {
5952 return StringUtil.lowerCase(StringUtil.trim(login));
5953 }
5954
5955 protected boolean isUseCustomSQL(LinkedHashMap<String, Object> params) {
5956 if (MapUtil.isEmpty(params)) {
5957 return false;
5958 }
5959
5960 for (String key : params.keySet()) {
5961 if (!key.equals("inherit") &&
5962 !key.equals("usersGroups") &&
5963 !key.equals("usersOrgs") &&
5964 !key.equals("usersOrgsCount") &&
5965 !key.equals("usersRoles") &&
5966 !key.equals("usersTeams") &&
5967 !key.equals("usersUserGroups")) {
5968
5969 return true;
5970 }
5971 }
5972
5973 Boolean inherit = (Boolean)params.get("inherit");
5974
5975 if ((inherit != null) && inherit) {
5976 return true;
5977 }
5978
5979 return false;
5980 }
5981
5982 protected void notifyUser(
5983 User user, String password, ServiceContext serviceContext) {
5984
5985 if (!PrefsPropsUtil.getBoolean(
5986 user.getCompanyId(),
5987 PropsKeys.ADMIN_EMAIL_USER_ADDED_ENABLED)) {
5988
5989 return;
5990 }
5991
5992 String fromName = PrefsPropsUtil.getString(
5993 user.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_NAME);
5994 String fromAddress = PrefsPropsUtil.getString(
5995 user.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
5996
5997 String toName = user.getFullName();
5998 String toAddress = user.getEmailAddress();
5999
6000 PortletPreferences companyPortletPreferences =
6001 PrefsPropsUtil.getPreferences(user.getCompanyId(), true);
6002
6003 Map<Locale, String> localizedSubjectMap =
6004 LocalizationUtil.getLocalizationMap(
6005 companyPortletPreferences, "adminEmailUserAddedSubject",
6006 PropsKeys.ADMIN_EMAIL_USER_ADDED_SUBJECT);
6007
6008 Map<Locale, String> localizedBodyMap = null;
6009
6010 if (Validator.isNotNull(password)) {
6011 localizedBodyMap = LocalizationUtil.getLocalizationMap(
6012 companyPortletPreferences, "adminEmailUserAddedBody",
6013 PropsKeys.ADMIN_EMAIL_USER_ADDED_BODY);
6014 }
6015 else {
6016 localizedBodyMap = LocalizationUtil.getLocalizationMap(
6017 companyPortletPreferences, "adminEmailUserAddedNoPasswordBody",
6018 PropsKeys.ADMIN_EMAIL_USER_ADDED_NO_PASSWORD_BODY);
6019 }
6020
6021 SubscriptionSender subscriptionSender = new SubscriptionSender();
6022
6023 subscriptionSender.setCompanyId(user.getCompanyId());
6024 subscriptionSender.setContextAttributes(
6025 "[$USER_ID$]", user.getUserId(), "[$USER_PASSWORD$]", password,
6026 "[$USER_SCREENNAME$]", user.getScreenName());
6027 subscriptionSender.setFrom(fromAddress, fromName);
6028 subscriptionSender.setHtmlFormat(true);
6029 subscriptionSender.setLocalizedBodyMap(localizedBodyMap);
6030 subscriptionSender.setLocalizedSubjectMap(localizedSubjectMap);
6031 subscriptionSender.setMailId("user", user.getUserId());
6032 subscriptionSender.setServiceContext(serviceContext);
6033 subscriptionSender.setUserId(user.getUserId());
6034
6035 subscriptionSender.addRuntimeSubscribers(toAddress, toName);
6036
6037 subscriptionSender.flushNotificationsAsync();
6038 }
6039
6040 protected void reindex(final User user) {
6041 final Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
6042 User.class);
6043
6044 Callable<Void> callable = new ShardCallable<Void>(
6045 user.getCompanyId()) {
6046
6047 @Override
6048 protected Void doCall() throws Exception {
6049 indexer.reindex(user);
6050
6051 return null;
6052 }
6053
6054 };
6055
6056 TransactionCommitCallbackRegistryUtil.registerCallback(callable);
6057 }
6058
6059 protected BaseModelSearchResult<User> searchUsers(
6060 SearchContext searchContext)
6061 throws PortalException {
6062
6063 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
6064
6065 for (int i = 0; i < 10; i++) {
6066 Hits hits = indexer.search(searchContext);
6067
6068 List<User> users = UsersAdminUtil.getUsers(hits);
6069
6070 if (users != null) {
6071 return new BaseModelSearchResult<User>(users, hits.getLength());
6072 }
6073 }
6074
6075 throw new SearchException(
6076 "Unable to fix the search index after 10 attempts");
6077 }
6078
6079 protected void setEmailAddress(
6080 User user, String password, String firstName, String middleName,
6081 String lastName, String emailAddress)
6082 throws PortalException {
6083
6084 if (StringUtil.equalsIgnoreCase(emailAddress, user.getEmailAddress())) {
6085 return;
6086 }
6087
6088 long userId = user.getUserId();
6089
6090
6091
6092 if (!user.hasCompanyMx() && user.hasCompanyMx(emailAddress) &&
6093 Validator.isNotNull(password)) {
6094
6095 mailService.addUser(
6096 user.getCompanyId(), userId, password, firstName, middleName,
6097 lastName, emailAddress);
6098 }
6099
6100
6101
6102 else if (user.hasCompanyMx() && user.hasCompanyMx(emailAddress)) {
6103 mailService.updateEmailAddress(
6104 user.getCompanyId(), userId, emailAddress);
6105 }
6106
6107
6108
6109 else if (user.hasCompanyMx() && !user.hasCompanyMx(emailAddress)) {
6110 mailService.deleteEmailAddress(user.getCompanyId(), userId);
6111 }
6112
6113 user.setEmailAddress(emailAddress);
6114 user.setDigest(StringPool.BLANK);
6115 }
6116
6117 protected void updateGroups(
6118 long userId, long[] newGroupIds, ServiceContext serviceContext,
6119 boolean indexingEnabled)
6120 throws PortalException {
6121
6122 if (newGroupIds == null) {
6123 return;
6124 }
6125
6126 long[] oldGroupIds = getGroupPrimaryKeys(userId);
6127
6128 for (long oldGroupId : oldGroupIds) {
6129 if (!ArrayUtil.contains(newGroupIds, oldGroupId)) {
6130 unsetGroupUsers(
6131 oldGroupId, new long[] {userId}, serviceContext);
6132 }
6133 }
6134
6135 for (long newGroupId : newGroupIds) {
6136 if (!ArrayUtil.contains(oldGroupIds, newGroupId)) {
6137 addGroupUsers(newGroupId, new long[] {userId});
6138 }
6139 }
6140
6141 if (indexingEnabled) {
6142 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
6143 User.class);
6144
6145 indexer.reindex(new long[] {userId});
6146 }
6147
6148 PermissionCacheUtil.clearCache();
6149 }
6150
6151 protected void updateOrganizations(
6152 long userId, long[] newOrganizationIds, boolean indexingEnabled)
6153 throws PortalException {
6154
6155 if (newOrganizationIds == null) {
6156 return;
6157 }
6158
6159 long[] oldOrganizationIds = getOrganizationPrimaryKeys(userId);
6160
6161 for (long oldOrganizationId : oldOrganizationIds) {
6162 if (!ArrayUtil.contains(newOrganizationIds, oldOrganizationId)) {
6163 unsetOrganizationUsers(oldOrganizationId, new long[] {userId});
6164 }
6165 }
6166
6167 for (long newOrganizationId : newOrganizationIds) {
6168 if (!ArrayUtil.contains(oldOrganizationIds, newOrganizationId)) {
6169 addOrganizationUsers(newOrganizationId, new long[] {userId});
6170 }
6171 }
6172
6173 if (indexingEnabled) {
6174 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
6175 User.class);
6176
6177 indexer.reindex(new long[] {userId});
6178 }
6179
6180 PermissionCacheUtil.clearCache();
6181 }
6182
6183 protected void updateUserGroupRoles(
6184 User user, long[] groupIds, long[] organizationIds,
6185 List<UserGroupRole> userGroupRoles,
6186 List<UserGroupRole> previousUserGroupRoles)
6187 throws PortalException {
6188
6189 if (userGroupRoles == null) {
6190 return;
6191 }
6192
6193 userGroupRoles = new ArrayList<UserGroupRole>(userGroupRoles);
6194
6195 for (UserGroupRole userGroupRole : previousUserGroupRoles) {
6196 if (userGroupRoles.contains(userGroupRole)) {
6197 userGroupRoles.remove(userGroupRole);
6198 }
6199 else {
6200 userGroupRoleLocalService.deleteUserGroupRole(userGroupRole);
6201 }
6202 }
6203
6204 if (ListUtil.isEmpty(userGroupRoles)) {
6205 return;
6206 }
6207
6208 long[] validGroupIds = null;
6209
6210 if (groupIds != null) {
6211 validGroupIds = ArrayUtil.clone(groupIds);
6212 }
6213 else {
6214 validGroupIds = user.getGroupIds();
6215 }
6216
6217 if (organizationIds == null) {
6218 organizationIds = user.getOrganizationIds();
6219 }
6220
6221 for (long organizationId : organizationIds) {
6222 Organization organization =
6223 organizationPersistence.findByPrimaryKey(organizationId);
6224
6225 validGroupIds = ArrayUtil.append(
6226 validGroupIds, organization.getGroupId());
6227 }
6228
6229 Arrays.sort(validGroupIds);
6230
6231 for (UserGroupRole userGroupRole : userGroupRoles) {
6232 if (Arrays.binarySearch(
6233 validGroupIds, userGroupRole.getGroupId()) >= 0) {
6234
6235 userGroupRoleLocalService.addUserGroupRole(userGroupRole);
6236 }
6237 }
6238 }
6239
6240 protected void validate(
6241 long companyId, long userId, boolean autoPassword, String password1,
6242 String password2, boolean autoScreenName, String screenName,
6243 String emailAddress, String openId, String firstName,
6244 String middleName, String lastName, long[] organizationIds)
6245 throws PortalException {
6246
6247 validateCompanyMaxUsers(companyId);
6248
6249 if (!autoScreenName) {
6250 validateScreenName(companyId, userId, screenName);
6251 }
6252
6253 if (!autoPassword) {
6254 PasswordPolicy passwordPolicy =
6255 passwordPolicyLocalService.getDefaultPasswordPolicy(companyId);
6256
6257 PwdToolkitUtil.validate(
6258 companyId, 0, password1, password2, passwordPolicy);
6259 }
6260
6261 validateEmailAddress(companyId, emailAddress);
6262
6263 if (Validator.isNotNull(emailAddress)) {
6264 User user = userPersistence.fetchByC_EA(companyId, emailAddress);
6265
6266 if ((user != null) && (user.getUserId() != userId)) {
6267 throw new UserEmailAddressException.MustNotBeDuplicate(
6268 userId, emailAddress);
6269 }
6270 }
6271
6272 validateOpenId(companyId, userId, openId);
6273
6274 validateFullName(companyId, firstName, middleName, lastName);
6275
6276 if (organizationIds != null) {
6277 for (long organizationId : organizationIds) {
6278 Organization organization =
6279 organizationPersistence.fetchByPrimaryKey(organizationId);
6280
6281 if (organization == null) {
6282 throw new NoSuchOrganizationException(
6283 "{organizationId=" + organizationId + "}");
6284 }
6285 }
6286 }
6287 }
6288
6289 protected void validate(
6290 long userId, String screenName, String emailAddress, String openId,
6291 String firstName, String middleName, String lastName, String smsSn)
6292 throws PortalException {
6293
6294 User user = userPersistence.findByPrimaryKey(userId);
6295
6296 if (!StringUtil.equalsIgnoreCase(user.getScreenName(), screenName)) {
6297 validateScreenName(user.getCompanyId(), userId, screenName);
6298 }
6299
6300 validateEmailAddress(user.getCompanyId(), emailAddress);
6301
6302 validateOpenId(user.getCompanyId(), userId, openId);
6303
6304 if (!user.isDefaultUser()) {
6305 if (Validator.isNotNull(emailAddress) &&
6306 !StringUtil.equalsIgnoreCase(
6307 user.getEmailAddress(), emailAddress)) {
6308
6309 if (userPersistence.fetchByC_EA(
6310 user.getCompanyId(), emailAddress) != null) {
6311
6312 throw new UserEmailAddressException.MustNotBeDuplicate(
6313 userId, emailAddress);
6314 }
6315 }
6316
6317 validateFullName(
6318 user.getCompanyId(), firstName, middleName, lastName);
6319 }
6320
6321 if (Validator.isNotNull(smsSn) && !Validator.isEmailAddress(smsSn)) {
6322 throw new UserSmsException.MustBeEmailAddress(smsSn);
6323 }
6324 }
6325
6326 protected void validateCompanyMaxUsers(long companyId)
6327 throws PortalException {
6328
6329 Company company = companyPersistence.findByPrimaryKey(companyId);
6330
6331 if (company.isSystem() || (company.getMaxUsers() == 0)) {
6332 return;
6333 }
6334
6335 int userCount = searchCount(
6336 companyId, null, WorkflowConstants.STATUS_APPROVED, null);
6337
6338 if (userCount >= company.getMaxUsers()) {
6339 throw new CompanyMaxUsersException();
6340 }
6341 }
6342
6343 protected void validateEmailAddress(long companyId, String emailAddress)
6344 throws PortalException {
6345
6346 if (Validator.isNull(emailAddress) &&
6347 !PropsValues.USERS_EMAIL_ADDRESS_REQUIRED) {
6348
6349 return;
6350 }
6351
6352 EmailAddressValidator emailAddressValidator =
6353 EmailAddressValidatorFactory.getInstance();
6354
6355 if (!emailAddressValidator.validate(companyId, emailAddress)) {
6356 throw new UserEmailAddressException.MustValidate(
6357 emailAddress, emailAddressValidator);
6358 }
6359
6360 String pop3User = PrefsPropsUtil.getString(
6361 PropsKeys.MAIL_SESSION_MAIL_POP3_USER,
6362 PropsValues.MAIL_SESSION_MAIL_POP3_USER);
6363
6364 if (StringUtil.equalsIgnoreCase(emailAddress, pop3User)) {
6365 throw new ReservedUserEmailAddressException();
6366 }
6367
6368 String[] reservedEmailAddresses = PrefsPropsUtil.getStringArray(
6369 companyId, PropsKeys.ADMIN_RESERVED_EMAIL_ADDRESSES,
6370 StringPool.NEW_LINE, PropsValues.ADMIN_RESERVED_EMAIL_ADDRESSES);
6371
6372 for (String reservedEmailAddress : reservedEmailAddresses) {
6373 if (StringUtil.equalsIgnoreCase(
6374 emailAddress, reservedEmailAddress)) {
6375
6376 throw new ReservedUserEmailAddressException();
6377 }
6378 }
6379 }
6380
6381 protected void validateEmailAddress(
6382 User user, String emailAddress1, String emailAddress2)
6383 throws PortalException {
6384
6385 if (!emailAddress1.equals(emailAddress2)) {
6386 throw new UserEmailAddressException.MustBeEqual(
6387 user, emailAddress1, emailAddress2);
6388 }
6389
6390 validateEmailAddress(user.getCompanyId(), emailAddress1);
6391 validateEmailAddress(user.getCompanyId(), emailAddress2);
6392
6393 if (!StringUtil.equalsIgnoreCase(
6394 emailAddress1, user.getEmailAddress())) {
6395
6396 if (userPersistence.fetchByC_EA(
6397 user.getCompanyId(), emailAddress1) != null) {
6398
6399 throw new UserEmailAddressException.MustNotBeDuplicate(
6400 user.getUserId(), emailAddress1);
6401 }
6402 }
6403 }
6404
6405 protected void validateFullName(
6406 long companyId, String firstName, String middleName,
6407 String lastName)
6408 throws PortalException {
6409
6410 if (Validator.isNull(firstName)) {
6411 throw new ContactFirstNameException();
6412 }
6413 else if (Validator.isNull(lastName) &&
6414 PrefsPropsUtil.getBoolean(
6415 companyId, PropsKeys.USERS_LAST_NAME_REQUIRED,
6416 PropsValues.USERS_LAST_NAME_REQUIRED)) {
6417
6418 throw new ContactLastNameException();
6419 }
6420
6421 FullNameValidator fullNameValidator =
6422 FullNameValidatorFactory.getInstance();
6423
6424 if (!fullNameValidator.validate(
6425 companyId, firstName, middleName, lastName)) {
6426
6427 throw new ContactFullNameException();
6428 }
6429 }
6430
6431 protected void validateOpenId(long companyId, long userId, String openId)
6432 throws PortalException {
6433
6434 if (Validator.isNull(openId)) {
6435 return;
6436 }
6437
6438 User user = userPersistence.fetchByC_O(companyId, openId);
6439
6440 if ((user != null) && (user.getUserId() != userId)) {
6441 throw new DuplicateOpenIdException("{userId=" + userId + "}");
6442 }
6443 }
6444
6445 protected void validatePassword(
6446 long companyId, long userId, String password1, String password2)
6447 throws PortalException {
6448
6449 if (Validator.isNull(password1) || Validator.isNull(password2)) {
6450 throw new UserPasswordException(
6451 UserPasswordException.PASSWORD_INVALID);
6452 }
6453
6454 if (!password1.equals(password2)) {
6455 throw new UserPasswordException(
6456 UserPasswordException.PASSWORDS_DO_NOT_MATCH);
6457 }
6458
6459 PasswordPolicy passwordPolicy =
6460 passwordPolicyLocalService.getPasswordPolicyByUserId(userId);
6461
6462 PwdToolkitUtil.validate(
6463 companyId, userId, password1, password2, passwordPolicy);
6464 }
6465
6466 protected void validateReminderQuery(String question, String answer)
6467 throws PortalException {
6468
6469 if (!PropsValues.USERS_REMINDER_QUERIES_ENABLED) {
6470 return;
6471 }
6472
6473 if (Validator.isNull(question)) {
6474 throw new UserReminderQueryException("Question is null");
6475 }
6476
6477 if (Validator.isNull(answer)) {
6478 throw new UserReminderQueryException("Answer is null");
6479 }
6480 }
6481
6482 protected void validateScreenName(
6483 long companyId, long userId, String screenName)
6484 throws PortalException {
6485
6486 if (Validator.isNull(screenName)) {
6487 throw new UserScreenNameException.MustNotBeNull(userId);
6488 }
6489
6490 ScreenNameValidator screenNameValidator =
6491 ScreenNameValidatorFactory.getInstance();
6492
6493 if (!screenNameValidator.validate(companyId, screenName)) {
6494 throw new UserScreenNameException.MustValidate(
6495 userId, screenName, screenNameValidator);
6496 }
6497
6498 if (Validator.isNumber(screenName)) {
6499 if (!PropsValues.USERS_SCREEN_NAME_ALLOW_NUMERIC) {
6500 throw new UserScreenNameException.MustNotBeNumeric(
6501 userId, screenName);
6502 }
6503
6504 if (!screenName.equals(String.valueOf(userId))) {
6505 Group group = groupPersistence.fetchByPrimaryKey(
6506 GetterUtil.getLong(screenName));
6507
6508 if (group != null) {
6509 throw new UserScreenNameException.MustNotBeUsedByGroup(
6510 userId, screenName, group);
6511 }
6512 }
6513 }
6514
6515 for (char c : screenName.toCharArray()) {
6516 if (!Validator.isChar(c) && !Validator.isDigit(c) &&
6517 (c != CharPool.DASH) && (c != CharPool.PERIOD) &&
6518 (c != CharPool.UNDERLINE)) {
6519
6520 throw new UserScreenNameException.MustBeAlphaNumeric(
6521 userId, screenName, CharPool.DASH, CharPool.PERIOD,
6522 CharPool.UNDERLINE);
6523 }
6524 }
6525
6526 String[] anonymousNames = BaseServiceImpl.ANONYMOUS_NAMES;
6527
6528 for (String anonymousName : anonymousNames) {
6529 if (StringUtil.equalsIgnoreCase(screenName, anonymousName)) {
6530 throw new UserScreenNameException.MustNotBeReservedForAnonymous(
6531 userId, screenName, anonymousNames);
6532 }
6533 }
6534
6535 User user = userPersistence.fetchByC_SN(companyId, screenName);
6536
6537 if ((user != null) && (user.getUserId() != userId)) {
6538 throw new UserScreenNameException.MustNotBeDuplicate(
6539 userId, screenName);
6540 }
6541
6542 String friendlyURL = StringPool.SLASH + screenName;
6543
6544 Group group = groupPersistence.fetchByC_F(companyId, friendlyURL);
6545
6546 if ((group != null) && (group.getClassPK() != userId)) {
6547 GroupFriendlyURLException gfurle = new GroupFriendlyURLException(
6548 GroupFriendlyURLException.DUPLICATE);
6549
6550 gfurle.setDuplicateClassPK(group.getGroupId());
6551 gfurle.setDuplicateClassName(Group.class.getName());
6552
6553 throw gfurle;
6554 }
6555
6556 int exceptionType = LayoutImpl.validateFriendlyURL(friendlyURL);
6557
6558 if (exceptionType != -1) {
6559 throw new UserScreenNameException.MustProduceValidFriendlyURL(
6560 userId, screenName, exceptionType);
6561 }
6562
6563 String[] reservedScreenNames = PrefsPropsUtil.getStringArray(
6564 companyId, PropsKeys.ADMIN_RESERVED_SCREEN_NAMES,
6565 StringPool.NEW_LINE, PropsValues.ADMIN_RESERVED_SCREEN_NAMES);
6566
6567 for (String reservedScreenName : reservedScreenNames) {
6568 if (StringUtil.equalsIgnoreCase(screenName, reservedScreenName)) {
6569 throw new ReservedUserScreenNameException();
6570 }
6571 }
6572 }
6573
6574 private static Log _log = LogFactoryUtil.getLog(UserLocalServiceImpl.class);
6575
6576 private Map<Long, User> _defaultUsers = new ConcurrentHashMap<Long, User>();
6577
6578 }