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.DuplicateUserEmailAddressException;
024 import com.liferay.portal.DuplicateUserScreenNameException;
025 import com.liferay.portal.GroupFriendlyURLException;
026 import com.liferay.portal.ModelListenerException;
027 import com.liferay.portal.NoSuchImageException;
028 import com.liferay.portal.NoSuchOrganizationException;
029 import com.liferay.portal.NoSuchRoleException;
030 import com.liferay.portal.NoSuchTicketException;
031 import com.liferay.portal.NoSuchUserException;
032 import com.liferay.portal.NoSuchUserGroupException;
033 import com.liferay.portal.PasswordExpiredException;
034 import com.liferay.portal.RequiredUserException;
035 import com.liferay.portal.ReservedUserEmailAddressException;
036 import com.liferay.portal.ReservedUserScreenNameException;
037 import com.liferay.portal.UserEmailAddressException;
038 import com.liferay.portal.UserIdException;
039 import com.liferay.portal.UserLockoutException;
040 import com.liferay.portal.UserPasswordException;
041 import com.liferay.portal.UserPortraitSizeException;
042 import com.liferay.portal.UserPortraitTypeException;
043 import com.liferay.portal.UserReminderQueryException;
044 import com.liferay.portal.UserScreenNameException;
045 import com.liferay.portal.UserSmsException;
046 import com.liferay.portal.kernel.dao.shard.ShardCallable;
047 import com.liferay.portal.kernel.exception.PortalException;
048 import com.liferay.portal.kernel.exception.SystemException;
049 import com.liferay.portal.kernel.image.ImageBag;
050 import com.liferay.portal.kernel.image.ImageToolUtil;
051 import com.liferay.portal.kernel.language.LanguageUtil;
052 import com.liferay.portal.kernel.log.Log;
053 import com.liferay.portal.kernel.log.LogFactoryUtil;
054 import com.liferay.portal.kernel.search.Hits;
055 import com.liferay.portal.kernel.search.Indexer;
056 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
057 import com.liferay.portal.kernel.search.QueryConfig;
058 import com.liferay.portal.kernel.search.SearchContext;
059 import com.liferay.portal.kernel.search.Sort;
060 import com.liferay.portal.kernel.spring.aop.Skip;
061 import com.liferay.portal.kernel.transaction.Propagation;
062 import com.liferay.portal.kernel.transaction.TransactionCommitCallbackRegistryUtil;
063 import com.liferay.portal.kernel.transaction.Transactional;
064 import com.liferay.portal.kernel.util.ArrayUtil;
065 import com.liferay.portal.kernel.util.CharPool;
066 import com.liferay.portal.kernel.util.Digester;
067 import com.liferay.portal.kernel.util.DigesterUtil;
068 import com.liferay.portal.kernel.util.GetterUtil;
069 import com.liferay.portal.kernel.util.KeyValuePair;
070 import com.liferay.portal.kernel.util.LocaleUtil;
071 import com.liferay.portal.kernel.util.OrderByComparator;
072 import com.liferay.portal.kernel.util.ParamUtil;
073 import com.liferay.portal.kernel.util.PropsKeys;
074 import com.liferay.portal.kernel.util.StringBundler;
075 import com.liferay.portal.kernel.util.StringPool;
076 import com.liferay.portal.kernel.util.StringUtil;
077 import com.liferay.portal.kernel.util.UnicodeProperties;
078 import com.liferay.portal.kernel.util.Validator;
079 import com.liferay.portal.kernel.workflow.WorkflowConstants;
080 import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
081 import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
082 import com.liferay.portal.model.Account;
083 import com.liferay.portal.model.Company;
084 import com.liferay.portal.model.CompanyConstants;
085 import com.liferay.portal.model.Contact;
086 import com.liferay.portal.model.ContactConstants;
087 import com.liferay.portal.model.Group;
088 import com.liferay.portal.model.GroupConstants;
089 import com.liferay.portal.model.Layout;
090 import com.liferay.portal.model.Organization;
091 import com.liferay.portal.model.PasswordPolicy;
092 import com.liferay.portal.model.ResourceConstants;
093 import com.liferay.portal.model.Role;
094 import com.liferay.portal.model.RoleConstants;
095 import com.liferay.portal.model.Team;
096 import com.liferay.portal.model.Ticket;
097 import com.liferay.portal.model.TicketConstants;
098 import com.liferay.portal.model.User;
099 import com.liferay.portal.model.UserGroup;
100 import com.liferay.portal.model.UserGroupRole;
101 import com.liferay.portal.model.impl.LayoutImpl;
102 import com.liferay.portal.security.auth.AuthPipeline;
103 import com.liferay.portal.security.auth.Authenticator;
104 import com.liferay.portal.security.auth.EmailAddressGenerator;
105 import com.liferay.portal.security.auth.EmailAddressGeneratorFactory;
106 import com.liferay.portal.security.auth.EmailAddressValidator;
107 import com.liferay.portal.security.auth.EmailAddressValidatorFactory;
108 import com.liferay.portal.security.auth.FullNameGenerator;
109 import com.liferay.portal.security.auth.FullNameGeneratorFactory;
110 import com.liferay.portal.security.auth.FullNameValidator;
111 import com.liferay.portal.security.auth.FullNameValidatorFactory;
112 import com.liferay.portal.security.auth.PrincipalException;
113 import com.liferay.portal.security.auth.ScreenNameGenerator;
114 import com.liferay.portal.security.auth.ScreenNameGeneratorFactory;
115 import com.liferay.portal.security.auth.ScreenNameValidator;
116 import com.liferay.portal.security.auth.ScreenNameValidatorFactory;
117 import com.liferay.portal.security.ldap.LDAPSettingsUtil;
118 import com.liferay.portal.security.permission.PermissionCacheUtil;
119 import com.liferay.portal.security.pwd.PasswordEncryptorUtil;
120 import com.liferay.portal.security.pwd.PwdAuthenticator;
121 import com.liferay.portal.security.pwd.PwdToolkitUtil;
122 import com.liferay.portal.security.pwd.RegExpToolkit;
123 import com.liferay.portal.service.BaseServiceImpl;
124 import com.liferay.portal.service.ServiceContext;
125 import com.liferay.portal.service.base.UserLocalServiceBaseImpl;
126 import com.liferay.portal.util.PortalUtil;
127 import com.liferay.portal.util.PrefsPropsUtil;
128 import com.liferay.portal.util.PropsValues;
129 import com.liferay.portal.util.SubscriptionSender;
130 import com.liferay.portlet.documentlibrary.ImageSizeException;
131 import com.liferay.portlet.messageboards.model.MBMessage;
132 import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
133 import com.liferay.util.Encryptor;
134 import com.liferay.util.EncryptorException;
135 import com.liferay.util.PwdGenerator;
136
137 import java.awt.image.RenderedImage;
138
139 import java.io.IOException;
140 import java.io.Serializable;
141
142 import java.util.ArrayList;
143 import java.util.Arrays;
144 import java.util.Calendar;
145 import java.util.Date;
146 import java.util.HashMap;
147 import java.util.HashSet;
148 import java.util.LinkedHashMap;
149 import java.util.List;
150 import java.util.Locale;
151 import java.util.Map;
152 import java.util.Set;
153 import java.util.concurrent.Callable;
154 import java.util.concurrent.ConcurrentHashMap;
155
156
168 public class UserLocalServiceImpl extends UserLocalServiceBaseImpl {
169
170
184 @Override
185 public User addDefaultAdminUser(
186 long companyId, String screenName, String emailAddress,
187 Locale locale, String firstName, String middleName, String lastName)
188 throws PortalException, SystemException {
189
190 long creatorUserId = 0;
191 boolean autoPassword = false;
192 String password1 = PropsValues.DEFAULT_ADMIN_PASSWORD;
193 String password2 = password1;
194 boolean autoScreenName = false;
195
196 screenName = getLogin(screenName);
197
198 for (int i = 1;; i++) {
199 User screenNameUser = userPersistence.fetchByC_SN(
200 companyId, screenName);
201
202 if (screenNameUser == null) {
203 break;
204 }
205
206 screenName = screenName + i;
207 }
208
209 long facebookId = 0;
210 String openId = StringPool.BLANK;
211 int prefixId = 0;
212 int suffixId = 0;
213 boolean male = true;
214 int birthdayMonth = Calendar.JANUARY;
215 int birthdayDay = 1;
216 int birthdayYear = 1970;
217 String jobTitle = StringPool.BLANK;
218
219 Group guestGroup = groupLocalService.getGroup(
220 companyId, GroupConstants.GUEST);
221
222 long[] groupIds = {guestGroup.getGroupId()};
223
224 long[] organizationIds = null;
225
226 Role adminRole = roleLocalService.getRole(
227 companyId, RoleConstants.ADMINISTRATOR);
228
229 Role powerUserRole = roleLocalService.getRole(
230 companyId, RoleConstants.POWER_USER);
231
232 long[] roleIds = {adminRole.getRoleId(), powerUserRole.getRoleId()};
233
234 long[] userGroupIds = null;
235 boolean sendEmail = false;
236 ServiceContext serviceContext = new ServiceContext();
237
238 User defaultAdminUser = addUser(
239 creatorUserId, companyId, autoPassword, password1, password2,
240 autoScreenName, screenName, emailAddress, facebookId, openId,
241 locale, firstName, middleName, lastName, prefixId, suffixId, male,
242 birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
243 organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
244
245 updateEmailAddressVerified(defaultAdminUser.getUserId(), true);
246
247 updateLastLogin(
248 defaultAdminUser.getUserId(), defaultAdminUser.getLoginIP());
249
250 updatePasswordReset(defaultAdminUser.getUserId(), false);
251
252 return defaultAdminUser;
253 }
254
255
265 @Override
266 public void addDefaultGroups(long userId)
267 throws PortalException, SystemException {
268
269 User user = userPersistence.findByPrimaryKey(userId);
270
271 Set<Long> groupIdsSet = new HashSet<Long>();
272
273 String[] defaultGroupNames = PrefsPropsUtil.getStringArray(
274 user.getCompanyId(), PropsKeys.ADMIN_DEFAULT_GROUP_NAMES,
275 StringPool.NEW_LINE, PropsValues.ADMIN_DEFAULT_GROUP_NAMES);
276
277 for (String defaultGroupName : defaultGroupNames) {
278 Company company = companyPersistence.findByPrimaryKey(
279 user.getCompanyId());
280
281 Account account = company.getAccount();
282
283 if (StringUtil.equalsIgnoreCase(
284 defaultGroupName, account.getName())) {
285
286 defaultGroupName = GroupConstants.GUEST;
287 }
288
289 Group group = groupPersistence.fetchByC_N(
290 user.getCompanyId(), defaultGroupName);
291
292 if ((group != null) &&
293 !userPersistence.containsGroup(
294 userId, group.getGroupId())) {
295
296 groupIdsSet.add(group.getGroupId());
297 }
298 }
299
300 String[] defaultOrganizationGroupNames = PrefsPropsUtil.getStringArray(
301 user.getCompanyId(),
302 PropsKeys.ADMIN_DEFAULT_ORGANIZATION_GROUP_NAMES,
303 StringPool.NEW_LINE,
304 PropsValues.ADMIN_DEFAULT_ORGANIZATION_GROUP_NAMES);
305
306 for (String defaultOrganizationGroupName :
307 defaultOrganizationGroupNames) {
308
309 defaultOrganizationGroupName +=
310 GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX;
311
312 Group group = groupPersistence.fetchByC_N(
313 user.getCompanyId(), defaultOrganizationGroupName);
314
315 if ((group != null) &&
316 !userPersistence.containsGroup(
317 userId, group.getGroupId())) {
318
319 groupIdsSet.add(group.getGroupId());
320 }
321 }
322
323 long[] groupIds = ArrayUtil.toArray(
324 groupIdsSet.toArray(new Long[groupIdsSet.size()]));
325
326 groupLocalService.addUserGroups(userId, groupIds);
327 }
328
329
339 @Override
340 public void addDefaultRoles(long userId)
341 throws PortalException, SystemException {
342
343 User user = userPersistence.findByPrimaryKey(userId);
344
345 Set<Long> roleIdSet = new HashSet<Long>();
346
347 String[] defaultRoleNames = PrefsPropsUtil.getStringArray(
348 user.getCompanyId(), PropsKeys.ADMIN_DEFAULT_ROLE_NAMES,
349 StringPool.NEW_LINE, PropsValues.ADMIN_DEFAULT_ROLE_NAMES);
350
351 for (String defaultRoleName : defaultRoleNames) {
352 try {
353 Role role = rolePersistence.findByC_N(
354 user.getCompanyId(), defaultRoleName);
355
356 if (!userPersistence.containsRole(userId, role.getRoleId())) {
357 roleIdSet.add(role.getRoleId());
358 }
359 }
360 catch (NoSuchRoleException nsre) {
361 }
362 }
363
364 long[] roleIds = ArrayUtil.toArray(
365 roleIdSet.toArray(new Long[roleIdSet.size()]));
366
367 roleIds = UsersAdminUtil.addRequiredRoles(user, roleIds);
368
369 userPersistence.addRoles(userId, roleIds);
370 }
371
372
382 @Override
383 @SuppressWarnings("deprecation")
384 public void addDefaultUserGroups(long userId)
385 throws PortalException, SystemException {
386
387 User user = userPersistence.findByPrimaryKey(userId);
388
389 Set<Long> userGroupIdSet = new HashSet<Long>();
390
391 String[] defaultUserGroupNames = PrefsPropsUtil.getStringArray(
392 user.getCompanyId(), PropsKeys.ADMIN_DEFAULT_USER_GROUP_NAMES,
393 StringPool.NEW_LINE, PropsValues.ADMIN_DEFAULT_USER_GROUP_NAMES);
394
395 for (String defaultUserGroupName : defaultUserGroupNames) {
396 try {
397 UserGroup userGroup = userGroupPersistence.findByC_N(
398 user.getCompanyId(), defaultUserGroupName);
399
400 if (!userPersistence.containsUserGroup(
401 userId, userGroup.getUserGroupId())) {
402
403 userGroupIdSet.add(userGroup.getUserGroupId());
404 }
405 }
406 catch (NoSuchUserGroupException nsuge) {
407 }
408 }
409
410 long[] userGroupIds = ArrayUtil.toArray(
411 userGroupIdSet.toArray(new Long[userGroupIdSet.size()]));
412
413 if (PropsValues.USER_GROUPS_COPY_LAYOUTS_TO_USER_PERSONAL_SITE) {
414 for (long userGroupId : userGroupIds) {
415 userGroupLocalService.copyUserGroupLayouts(userGroupId, userId);
416 }
417 }
418
419 userPersistence.addUserGroups(userId, userGroupIds);
420 }
421
422
431 @Override
432 public void addGroupUsers(long groupId, long[] userIds)
433 throws PortalException, SystemException {
434
435 groupPersistence.addUsers(groupId, userIds);
436
437 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
438
439 indexer.reindex(userIds);
440
441 PermissionCacheUtil.clearCache();
442
443 addDefaultRolesAndTeams(groupId, userIds);
444 }
445
446
455 @Override
456 public void addOrganizationUsers(long organizationId, long[] userIds)
457 throws PortalException, SystemException {
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
476 @Override
477 public void addPasswordPolicyUsers(long passwordPolicyId, long[] userIds)
478 throws SystemException {
479
480 passwordPolicyRelLocalService.addPasswordPolicyRels(
481 passwordPolicyId, User.class.getName(), userIds);
482 }
483
484
493 @Override
494 public void addRoleUsers(long roleId, long[] userIds)
495 throws PortalException, SystemException {
496
497 rolePersistence.addUsers(roleId, userIds);
498
499 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
500
501 indexer.reindex(userIds);
502
503 PermissionCacheUtil.clearCache();
504 }
505
506
515 @Override
516 public void addTeamUsers(long teamId, long[] userIds)
517 throws PortalException, SystemException {
518
519 teamPersistence.addUsers(teamId, userIds);
520
521 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
522
523 indexer.reindex(userIds);
524
525 PermissionCacheUtil.clearCache();
526 }
527
528
576 @Override
577 public User addUser(
578 long creatorUserId, long companyId, boolean autoPassword,
579 String password1, String password2, boolean autoScreenName,
580 String screenName, String emailAddress, long facebookId,
581 String openId, Locale locale, String firstName, String middleName,
582 String lastName, int prefixId, int suffixId, boolean male,
583 int birthdayMonth, int birthdayDay, int birthdayYear,
584 String jobTitle, long[] groupIds, long[] organizationIds,
585 long[] roleIds, long[] userGroupIds, boolean sendEmail,
586 ServiceContext serviceContext)
587 throws PortalException, SystemException {
588
589 boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
590
591 try {
592 WorkflowThreadLocal.setEnabled(false);
593
594 if (serviceContext == null) {
595 serviceContext = new ServiceContext();
596 }
597
598 if (serviceContext.getWorkflowAction() !=
599 WorkflowConstants.ACTION_PUBLISH) {
600
601 serviceContext.setWorkflowAction(
602 WorkflowConstants.ACTION_PUBLISH);
603 }
604
605 return addUserWithWorkflow(
606 creatorUserId, companyId, autoPassword, password1, password2,
607 autoScreenName, screenName, emailAddress, facebookId, openId,
608 locale, firstName, middleName, lastName, prefixId, suffixId,
609 male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
610 groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
611 serviceContext);
612 }
613 finally {
614 WorkflowThreadLocal.setEnabled(workflowEnabled);
615 }
616 }
617
618
627 @Override
628 @SuppressWarnings("deprecation")
629 public void addUserGroupUsers(long userGroupId, long[] userIds)
630 throws PortalException, SystemException {
631
632 if (PropsValues.USER_GROUPS_COPY_LAYOUTS_TO_USER_PERSONAL_SITE) {
633 userGroupLocalService.copyUserGroupLayouts(userGroupId, userIds);
634 }
635
636 userGroupPersistence.addUsers(userGroupId, userIds);
637
638 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
639
640 indexer.reindex(userIds);
641
642 PermissionCacheUtil.clearCache();
643 }
644
645
693 @Override
694 @SuppressWarnings("deprecation")
695 public User addUserWithWorkflow(
696 long creatorUserId, long companyId, boolean autoPassword,
697 String password1, String password2, boolean autoScreenName,
698 String screenName, String emailAddress, long facebookId,
699 String openId, Locale locale, String firstName, String middleName,
700 String lastName, int prefixId, int suffixId, boolean male,
701 int birthdayMonth, int birthdayDay, int birthdayYear,
702 String jobTitle, long[] groupIds, long[] organizationIds,
703 long[] roleIds, long[] userGroupIds, boolean sendEmail,
704 ServiceContext serviceContext)
705 throws PortalException, SystemException {
706
707
708
709 Company company = companyPersistence.findByPrimaryKey(companyId);
710 screenName = getLogin(screenName);
711 openId = StringUtil.trim(openId);
712 Date now = new Date();
713
714 if (PrefsPropsUtil.getBoolean(
715 companyId, PropsKeys.USERS_SCREEN_NAME_ALWAYS_AUTOGENERATE)) {
716
717 autoScreenName = true;
718 }
719
720
721
722 long userId = counterLocalService.increment();
723
724 EmailAddressGenerator emailAddressGenerator =
725 EmailAddressGeneratorFactory.getInstance();
726
727 if ((emailAddress == null) ||
728 emailAddressGenerator.isGenerated(emailAddress)) {
729
730 emailAddress = StringPool.BLANK;
731 }
732 else {
733 emailAddress = StringUtil.toLowerCase(emailAddress.trim());
734 }
735
736 if (!PrefsPropsUtil.getBoolean(
737 companyId, PropsKeys.USERS_EMAIL_ADDRESS_REQUIRED) &&
738 Validator.isNull(emailAddress)) {
739
740 emailAddress = emailAddressGenerator.generate(companyId, userId);
741 }
742
743 validate(
744 companyId, userId, autoPassword, password1, password2,
745 autoScreenName, screenName, emailAddress, openId, firstName,
746 middleName, lastName, organizationIds);
747
748 if (!autoPassword) {
749 if (Validator.isNull(password1) || Validator.isNull(password2)) {
750 throw new UserPasswordException(
751 UserPasswordException.PASSWORD_INVALID);
752 }
753 }
754
755 if (autoScreenName) {
756 ScreenNameGenerator screenNameGenerator =
757 ScreenNameGeneratorFactory.getInstance();
758
759 try {
760 screenName = screenNameGenerator.generate(
761 companyId, userId, emailAddress);
762 }
763 catch (Exception e) {
764 throw new SystemException(e);
765 }
766 }
767
768 User defaultUser = getDefaultUser(companyId);
769
770 FullNameGenerator fullNameGenerator =
771 FullNameGeneratorFactory.getInstance();
772
773 String fullName = fullNameGenerator.getFullName(
774 firstName, middleName, lastName);
775
776 String greeting = LanguageUtil.format(
777 locale, "welcome-x", " " + fullName, false);
778
779 User user = userPersistence.create(userId);
780
781 if (serviceContext != null) {
782 String uuid = serviceContext.getUuid();
783
784 if (Validator.isNotNull(uuid)) {
785 user.setUuid(uuid);
786 }
787 }
788
789 user.setCompanyId(companyId);
790 user.setCreateDate(now);
791 user.setModifiedDate(now);
792 user.setDefaultUser(false);
793 user.setContactId(counterLocalService.increment());
794
795 if (Validator.isNotNull(password1)) {
796 user.setPassword(PasswordEncryptorUtil.encrypt(password1));
797 user.setPasswordUnencrypted(password1);
798 }
799
800 user.setPasswordEncrypted(true);
801
802 PasswordPolicy passwordPolicy = defaultUser.getPasswordPolicy();
803
804 boolean passwordReset = false;
805
806 if (passwordPolicy != null) {
807 if (passwordPolicy.isChangeable() &&
808 passwordPolicy.isChangeRequired()) {
809
810 passwordReset = true;
811 }
812
813 addPasswordPolicyUsers(
814 passwordPolicy.getPasswordPolicyId(), new long[] {userId});
815 }
816
817 user.setPasswordReset(passwordReset);
818
819 user.setDigest(StringPool.BLANK);
820 user.setScreenName(screenName);
821 user.setEmailAddress(emailAddress);
822 user.setFacebookId(facebookId);
823
824 Long ldapServerId = (Long)serviceContext.getAttribute("ldapServerId");
825
826 if (ldapServerId != null) {
827 user.setLdapServerId(ldapServerId);
828 }
829 else {
830 user.setLdapServerId(-1);
831 }
832
833 user.setOpenId(openId);
834 user.setLanguageId(LocaleUtil.toLanguageId(locale));
835 user.setTimeZoneId(defaultUser.getTimeZoneId());
836 user.setGreeting(greeting);
837 user.setFirstName(firstName);
838 user.setMiddleName(middleName);
839 user.setLastName(lastName);
840 user.setJobTitle(jobTitle);
841 user.setStatus(WorkflowConstants.STATUS_DRAFT);
842 user.setExpandoBridgeAttributes(serviceContext);
843
844 userPersistence.update(user, serviceContext);
845
846
847
848 String creatorUserName = StringPool.BLANK;
849
850 if (creatorUserId <= 0) {
851 creatorUserId = user.getUserId();
852
853
854
855
856
857 }
858 else {
859 User creatorUser = userPersistence.findByPrimaryKey(creatorUserId);
860
861 creatorUserName = creatorUser.getFullName();
862 }
863
864 resourceLocalService.addResources(
865 companyId, 0, creatorUserId, User.class.getName(), user.getUserId(),
866 false, false, false);
867
868
869
870 Date birthday = getBirthday(birthdayMonth, birthdayDay, birthdayYear);
871
872 Contact contact = contactPersistence.create(user.getContactId());
873
874 contact.setCompanyId(user.getCompanyId());
875 contact.setUserId(creatorUserId);
876 contact.setUserName(creatorUserName);
877 contact.setCreateDate(now);
878 contact.setModifiedDate(now);
879 contact.setClassName(User.class.getName());
880 contact.setClassPK(user.getUserId());
881 contact.setAccountId(company.getAccountId());
882 contact.setParentContactId(ContactConstants.DEFAULT_PARENT_CONTACT_ID);
883 contact.setEmailAddress(user.getEmailAddress());
884 contact.setFirstName(firstName);
885 contact.setMiddleName(middleName);
886 contact.setLastName(lastName);
887 contact.setPrefixId(prefixId);
888 contact.setSuffixId(suffixId);
889 contact.setMale(male);
890 contact.setBirthday(birthday);
891 contact.setJobTitle(jobTitle);
892
893 contactPersistence.update(contact, serviceContext);
894
895
896
897 groupLocalService.addGroup(
898 user.getUserId(), GroupConstants.DEFAULT_PARENT_GROUP_ID,
899 User.class.getName(), user.getUserId(), null, null, 0,
900 StringPool.SLASH + screenName, false, true, null);
901
902
903
904 if (groupIds != null) {
905 List<Group> groups = new ArrayList<Group>();
906
907 for (long groupId : groupIds) {
908 Group group = groupLocalService.fetchGroup(groupId);
909
910 if (group != null) {
911 groups.add(group);
912 }
913 else {
914 if (_log.isWarnEnabled()) {
915 _log.warn("Group " + groupId + " does not exist");
916 }
917 }
918 }
919
920 groupLocalService.addUserGroups(userId, groups);
921 }
922
923 addDefaultGroups(userId);
924
925
926
927 updateOrganizations(userId, organizationIds, false);
928
929
930
931 if (roleIds != null) {
932 roleIds = UsersAdminUtil.addRequiredRoles(user, roleIds);
933
934 userPersistence.setRoles(userId, roleIds);
935 }
936
937 addDefaultRoles(userId);
938
939
940
941 if (userGroupIds != null) {
942 if (PropsValues.USER_GROUPS_COPY_LAYOUTS_TO_USER_PERSONAL_SITE) {
943 for (long userGroupId : userGroupIds) {
944 userGroupLocalService.copyUserGroupLayouts(
945 userGroupId, new long[] {userId});
946 }
947 }
948
949 userPersistence.setUserGroups(userId, userGroupIds);
950 }
951
952 addDefaultUserGroups(userId);
953
954
955
956 if (serviceContext != null) {
957 updateAsset(
958 creatorUserId, user, serviceContext.getAssetCategoryIds(),
959 serviceContext.getAssetTagNames());
960 }
961
962
963
964 if ((serviceContext == null) || serviceContext.isIndexingEnabled()) {
965 reindex(user);
966 }
967
968
969
970 long workflowUserId = creatorUserId;
971
972 if (workflowUserId == userId) {
973 workflowUserId = defaultUser.getUserId();
974 }
975
976 ServiceContext workflowServiceContext = serviceContext;
977
978 if (workflowServiceContext == null) {
979 workflowServiceContext = new ServiceContext();
980 }
981
982 workflowServiceContext.setAttribute("autoPassword", autoPassword);
983 workflowServiceContext.setAttribute("sendEmail", sendEmail);
984
985 startWorkflowInstance(
986 companyId, workflowUserId, userId, user, workflowServiceContext);
987
988 if (serviceContext != null) {
989 String passwordUnencrypted = (String)serviceContext.getAttribute(
990 "passwordUnencrypted");
991
992 if (Validator.isNotNull(passwordUnencrypted)) {
993 user.setPasswordUnencrypted(passwordUnencrypted);
994 }
995 }
996
997 return user;
998 }
999
1000
1024 @Override
1025 public int authenticateByEmailAddress(
1026 long companyId, String emailAddress, String password,
1027 Map<String, String[]> headerMap, Map<String, String[]> parameterMap,
1028 Map<String, Object> resultsMap)
1029 throws PortalException, SystemException {
1030
1031 return authenticate(
1032 companyId, emailAddress, password, CompanyConstants.AUTH_TYPE_EA,
1033 headerMap, parameterMap, resultsMap);
1034 }
1035
1036
1060 @Override
1061 public int authenticateByScreenName(
1062 long companyId, String screenName, String password,
1063 Map<String, String[]> headerMap, Map<String, String[]> parameterMap,
1064 Map<String, Object> resultsMap)
1065 throws PortalException, SystemException {
1066
1067 return authenticate(
1068 companyId, screenName, password, CompanyConstants.AUTH_TYPE_SN,
1069 headerMap, parameterMap, resultsMap);
1070 }
1071
1072
1096 @Override
1097 public int authenticateByUserId(
1098 long companyId, long userId, String password,
1099 Map<String, String[]> headerMap, Map<String, String[]> parameterMap,
1100 Map<String, Object> resultsMap)
1101 throws PortalException, SystemException {
1102
1103 return authenticate(
1104 companyId, String.valueOf(userId), password,
1105 CompanyConstants.AUTH_TYPE_ID, headerMap, parameterMap, resultsMap);
1106 }
1107
1108
1148 @Override
1149 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
1150 public long authenticateForBasic(
1151 long companyId, String authType, String login, String password)
1152 throws PortalException, SystemException {
1153
1154 if (PropsValues.AUTH_LOGIN_DISABLED) {
1155 return 0;
1156 }
1157
1158 User user = null;
1159
1160 if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
1161 user = fetchUserByEmailAddress(companyId, login);
1162 }
1163 else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
1164 user = fetchUserByScreenName(companyId, login);
1165 }
1166 else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
1167 user = userPersistence.fetchByPrimaryKey(GetterUtil.getLong(login));
1168 }
1169
1170 if (user == null) {
1171 return 0;
1172 }
1173
1174 if (user.isDefaultUser()) {
1175 if (_log.isInfoEnabled()) {
1176 _log.info(
1177 "Basic authentication is disabled for the default " +
1178 "user");
1179 }
1180
1181 return 0;
1182 }
1183 else if (!user.isActive()) {
1184 if (_log.isInfoEnabled()) {
1185 _log.info(
1186 "Basic authentication is disabled for inactive user " +
1187 user.getUserId());
1188 }
1189
1190 return 0;
1191 }
1192
1193 if (!PropsValues.BASIC_AUTH_PASSWORD_REQUIRED) {
1194 return user.getUserId();
1195 }
1196
1197 String userPassword = user.getPassword();
1198
1199 if (!user.isPasswordEncrypted()) {
1200 userPassword = PasswordEncryptorUtil.encrypt(userPassword);
1201 }
1202
1203 String encPassword = PasswordEncryptorUtil.encrypt(
1204 password, userPassword);
1205
1206 if (userPassword.equals(password) || userPassword.equals(encPassword)) {
1207 return user.getUserId();
1208 }
1209
1210 return 0;
1211 }
1212
1213
1231 @Override
1232 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
1233 public long authenticateForDigest(
1234 long companyId, String username, String realm, String nonce,
1235 String method, String uri, String response)
1236 throws PortalException, SystemException {
1237
1238 if (PropsValues.AUTH_LOGIN_DISABLED) {
1239 return 0;
1240 }
1241
1242
1243
1244 User user = fetchUserByEmailAddress(companyId, username);
1245
1246 if (user == null) {
1247 user = fetchUserByScreenName(companyId, username);
1248 }
1249
1250 if (user == null) {
1251 user = userPersistence.fetchByPrimaryKey(
1252 GetterUtil.getLong(username));
1253 }
1254
1255 if (user == null) {
1256 return 0;
1257 }
1258
1259 if (user.isDefaultUser()) {
1260 if (_log.isInfoEnabled()) {
1261 _log.info(
1262 "Digest authentication is disabled for the default user");
1263 }
1264
1265 return 0;
1266 }
1267 else if (!user.isActive()) {
1268 if (_log.isInfoEnabled()) {
1269 _log.info(
1270 "Digest authentication is disabled for inactive user " +
1271 user.getUserId());
1272 }
1273
1274 return 0;
1275 }
1276
1277
1278
1279 String digest = user.getDigest();
1280
1281 if (Validator.isNull(digest)) {
1282 _log.error(
1283 "User must first login through the portal " + user.getUserId());
1284
1285 return 0;
1286 }
1287
1288 String[] digestArray = StringUtil.split(user.getDigest());
1289
1290 for (String ha1 : digestArray) {
1291 String ha2 = DigesterUtil.digestHex(Digester.MD5, method, uri);
1292
1293 String curResponse = DigesterUtil.digestHex(
1294 Digester.MD5, ha1, nonce, ha2);
1295
1296 if (response.equals(curResponse)) {
1297 return user.getUserId();
1298 }
1299 }
1300
1301 return 0;
1302 }
1303
1304
1313 @Override
1314 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
1315 public boolean authenticateForJAAS(long userId, String encPassword) {
1316 if (PropsValues.AUTH_LOGIN_DISABLED) {
1317 return false;
1318 }
1319
1320 try {
1321 User user = userPersistence.findByPrimaryKey(userId);
1322
1323 if (user.isDefaultUser()) {
1324 if (_log.isInfoEnabled()) {
1325 _log.info(
1326 "JAAS authentication is disabled for the default user");
1327 }
1328
1329 return false;
1330 }
1331 else if (!user.isActive()) {
1332 if (_log.isInfoEnabled()) {
1333 _log.info(
1334 "JAAS authentication is disabled for inactive user " +
1335 userId);
1336 }
1337
1338 return false;
1339 }
1340
1341 String userPassword = user.getPassword();
1342
1343 if (user.isPasswordEncrypted()) {
1344 if (userPassword.equals(encPassword)) {
1345 return true;
1346 }
1347
1348 if (!PropsValues.PORTAL_JAAS_STRICT_PASSWORD) {
1349 encPassword = PasswordEncryptorUtil.encrypt(
1350 encPassword, userPassword);
1351
1352 if (userPassword.equals(encPassword)) {
1353 return true;
1354 }
1355 }
1356 }
1357 else {
1358 if (!PropsValues.PORTAL_JAAS_STRICT_PASSWORD) {
1359 if (userPassword.equals(encPassword)) {
1360 return true;
1361 }
1362 }
1363
1364 userPassword = PasswordEncryptorUtil.encrypt(
1365 userPassword, encPassword);
1366
1367 if (userPassword.equals(encPassword)) {
1368 return true;
1369 }
1370 }
1371 }
1372 catch (Exception e) {
1373 _log.error(e);
1374 }
1375
1376 return false;
1377 }
1378
1379
1387 @Override
1388 public void checkLockout(User user)
1389 throws PortalException, SystemException {
1390
1391 if (LDAPSettingsUtil.isPasswordPolicyEnabled(user.getCompanyId())) {
1392 return;
1393 }
1394
1395 PasswordPolicy passwordPolicy = user.getPasswordPolicy();
1396
1397 if (!passwordPolicy.isLockout()) {
1398 return;
1399 }
1400
1401
1402
1403 Date now = new Date();
1404 int failedLoginAttempts = user.getFailedLoginAttempts();
1405
1406 if (failedLoginAttempts > 0) {
1407 long failedLoginTime = user.getLastFailedLoginDate().getTime();
1408 long elapsedTime = now.getTime() - failedLoginTime;
1409 long requiredElapsedTime =
1410 passwordPolicy.getResetFailureCount() * 1000;
1411
1412 if ((requiredElapsedTime != 0) &&
1413 (elapsedTime > requiredElapsedTime)) {
1414
1415 user.setLastFailedLoginDate(null);
1416 user.setFailedLoginAttempts(0);
1417
1418 userPersistence.update(user);
1419 }
1420 }
1421
1422
1423
1424 if (user.isLockout()) {
1425 long lockoutTime = user.getLockoutDate().getTime();
1426 long elapsedTime = now.getTime() - lockoutTime;
1427 long requiredElapsedTime =
1428 passwordPolicy.getLockoutDuration() * 1000;
1429
1430 if ((requiredElapsedTime != 0) &&
1431 (elapsedTime > requiredElapsedTime)) {
1432
1433 user.setLockout(false);
1434 user.setLockoutDate(null);
1435
1436 userPersistence.update(user);
1437 }
1438 }
1439
1440 if (user.isLockout()) {
1441 throw new UserLockoutException();
1442 }
1443 }
1444
1445
1452 @Override
1453 public void checkLoginFailure(User user) throws SystemException {
1454 Date now = new Date();
1455
1456 int failedLoginAttempts = user.getFailedLoginAttempts();
1457
1458 user.setLastFailedLoginDate(now);
1459 user.setFailedLoginAttempts(++failedLoginAttempts);
1460
1461 userPersistence.update(user);
1462 }
1463
1464
1474 @Override
1475 public void checkLoginFailureByEmailAddress(
1476 long companyId, String emailAddress)
1477 throws PortalException, SystemException {
1478
1479 User user = getUserByEmailAddress(companyId, emailAddress);
1480
1481 checkLoginFailure(user);
1482 }
1483
1484
1492 @Override
1493 public void checkLoginFailureById(long userId)
1494 throws PortalException, SystemException {
1495
1496 User user = userPersistence.findByPrimaryKey(userId);
1497
1498 checkLoginFailure(user);
1499 }
1500
1501
1510 @Override
1511 public void checkLoginFailureByScreenName(long companyId, String screenName)
1512 throws PortalException, SystemException {
1513
1514 User user = getUserByScreenName(companyId, screenName);
1515
1516 checkLoginFailure(user);
1517 }
1518
1519
1529 @Override
1530 public void checkPasswordExpired(User user)
1531 throws PortalException, SystemException {
1532
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
1577 @Override
1578 public void clearOrganizationUsers(long organizationId)
1579 throws SystemException {
1580
1581 organizationPersistence.clearUsers(organizationId);
1582
1583 PermissionCacheUtil.clearCache();
1584 }
1585
1586
1592 @Override
1593 public void clearUserGroupUsers(long userGroupId) throws SystemException {
1594 userGroupPersistence.clearUsers(userGroupId);
1595
1596 PermissionCacheUtil.clearCache();
1597 }
1598
1599
1612 @Override
1613 public void completeUserRegistration(
1614 User user, ServiceContext serviceContext)
1615 throws PortalException, SystemException {
1616
1617 boolean autoPassword = ParamUtil.getBoolean(
1618 serviceContext, "autoPassword");
1619
1620 String password = null;
1621
1622 if (autoPassword) {
1623 if (LDAPSettingsUtil.isPasswordPolicyEnabled(user.getCompanyId())) {
1624 if (_log.isWarnEnabled()) {
1625 StringBundler sb = new StringBundler(4);
1626
1627 sb.append("When LDAP password policy is enabled, it is ");
1628 sb.append("possible that portal generated passwords will ");
1629 sb.append("not match the LDAP policy. Using ");
1630 sb.append("RegExpToolkit to generate new password.");
1631
1632 _log.warn(sb.toString());
1633 }
1634
1635 RegExpToolkit regExpToolkit = new RegExpToolkit();
1636
1637 password = regExpToolkit.generate(null);
1638 }
1639 else {
1640 PasswordPolicy passwordPolicy =
1641 passwordPolicyLocalService.getPasswordPolicy(
1642 user.getCompanyId(), user.getOrganizationIds());
1643
1644 password = PwdToolkitUtil.generate(passwordPolicy);
1645 }
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 String mailPassword = password;
1660
1661 if (Validator.isNull(mailPassword)) {
1662 mailPassword = user.getPasswordUnencrypted();
1663 }
1664
1665 mailService.addUser(
1666 user.getCompanyId(), user.getUserId(), mailPassword,
1667 user.getFirstName(), user.getMiddleName(), user.getLastName(),
1668 user.getEmailAddress());
1669 }
1670
1671 boolean sendEmail = ParamUtil.getBoolean(serviceContext, "sendEmail");
1672
1673 if (sendEmail) {
1674 sendEmail(user, password, serviceContext);
1675 }
1676
1677 Company company = companyPersistence.findByPrimaryKey(
1678 user.getCompanyId());
1679
1680 if (company.isStrangersVerify() && (serviceContext.getPlid() > 0)) {
1681 sendEmailAddressVerification(
1682 user, user.getEmailAddress(), serviceContext);
1683 }
1684 }
1685
1686
1699 @Override
1700 public KeyValuePair decryptUserId(
1701 long companyId, String name, String password)
1702 throws PortalException, SystemException {
1703
1704 Company company = companyPersistence.findByPrimaryKey(companyId);
1705
1706 try {
1707 name = Encryptor.decrypt(company.getKeyObj(), name);
1708 }
1709 catch (EncryptorException ee) {
1710 throw new SystemException(ee);
1711 }
1712
1713 long userId = GetterUtil.getLong(name);
1714
1715 User user = userPersistence.findByPrimaryKey(userId);
1716
1717 try {
1718 password = Encryptor.decrypt(company.getKeyObj(), password);
1719 }
1720 catch (EncryptorException ee) {
1721 throw new SystemException(ee);
1722 }
1723
1724 String userPassword = user.getPassword();
1725 String encPassword = PasswordEncryptorUtil.encrypt(
1726 password, userPassword);
1727
1728 if (userPassword.equals(encPassword)) {
1729 if (isPasswordExpired(user)) {
1730 user.setPasswordReset(true);
1731
1732 userPersistence.update(user);
1733 }
1734
1735 return new KeyValuePair(name, password);
1736 }
1737 else {
1738 throw new PrincipalException();
1739 }
1740 }
1741
1742
1750 @Override
1751 public void deletePortrait(long userId)
1752 throws PortalException, SystemException {
1753
1754 User user = userPersistence.findByPrimaryKey(userId);
1755
1756 long portraitId = user.getPortraitId();
1757
1758 if (portraitId > 0) {
1759 user.setPortraitId(0);
1760
1761 userPersistence.update(user);
1762
1763 imageLocalService.deleteImage(portraitId);
1764 }
1765 }
1766
1767
1776 @Override
1777 public void deleteRoleUser(long roleId, long userId)
1778 throws PortalException, SystemException {
1779
1780 rolePersistence.removeUser(roleId, userId);
1781
1782 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
1783
1784 indexer.reindex(userId);
1785
1786 PermissionCacheUtil.clearCache();
1787 }
1788
1789
1797 @Override
1798 public User deleteUser(long userId)
1799 throws PortalException, SystemException {
1800
1801 User user = userPersistence.findByPrimaryKey(userId);
1802
1803 return deleteUser(user);
1804 }
1805
1806
1814 @Override
1815 public User deleteUser(User user) throws PortalException, SystemException {
1816 if (!PropsValues.USERS_DELETE) {
1817 throw new RequiredUserException();
1818 }
1819
1820
1821
1822 browserTrackerLocalService.deleteUserBrowserTracker(user.getUserId());
1823
1824
1825
1826 Group group = null;
1827
1828 if (!user.isDefaultUser()) {
1829 group = user.getGroup();
1830 }
1831
1832 if (group != null) {
1833 groupLocalService.deleteGroup(group);
1834 }
1835
1836
1837
1838 try {
1839 imageLocalService.deleteImage(user.getPortraitId());
1840 }
1841 catch (NoSuchImageException nsie) {
1842 if (_log.isWarnEnabled()) {
1843 _log.warn("Unable to delete image " + user.getPortraitId());
1844 }
1845 }
1846
1847
1848
1849 passwordPolicyRelLocalService.deletePasswordPolicyRel(
1850 User.class.getName(), user.getUserId());
1851
1852
1853
1854 passwordTrackerLocalService.deletePasswordTrackers(user.getUserId());
1855
1856
1857
1858 subscriptionLocalService.deleteSubscriptions(user.getUserId());
1859
1860
1861
1862 userIdMapperLocalService.deleteUserIdMappers(user.getUserId());
1863
1864
1865
1866 announcementsDeliveryLocalService.deleteDeliveries(user.getUserId());
1867
1868
1869
1870 assetEntryLocalService.deleteEntry(
1871 User.class.getName(), user.getUserId());
1872
1873
1874
1875 blogsStatsUserLocalService.deleteStatsUserByUserId(user.getUserId());
1876
1877
1878
1879 dlFileRankLocalService.deleteFileRanksByUserId(user.getUserId());
1880
1881
1882
1883 expandoRowLocalService.deleteRows(user.getUserId());
1884
1885
1886
1887 mbBanLocalService.deleteBansByBanUserId(user.getUserId());
1888 mbStatsUserLocalService.deleteStatsUsersByUserId(user.getUserId());
1889 mbThreadFlagLocalService.deleteThreadFlagsByUserId(user.getUserId());
1890
1891
1892
1893 membershipRequestLocalService.deleteMembershipRequestsByUserId(
1894 user.getUserId());
1895
1896
1897
1898 shoppingCartLocalService.deleteUserCarts(user.getUserId());
1899
1900
1901
1902 socialActivityLocalService.deleteUserActivities(user.getUserId());
1903 socialRequestLocalService.deleteReceiverUserRequests(user.getUserId());
1904 socialRequestLocalService.deleteUserRequests(user.getUserId());
1905
1906
1907
1908 mailService.deleteUser(user.getCompanyId(), user.getUserId());
1909
1910
1911
1912 Contact contact = contactLocalService.fetchContact(user.getContactId());
1913
1914 if (contact != null) {
1915 contactLocalService.deleteContact(contact);
1916 }
1917
1918
1919
1920 resourceLocalService.deleteResource(
1921 user.getCompanyId(), User.class.getName(),
1922 ResourceConstants.SCOPE_INDIVIDUAL, user.getUserId());
1923
1924
1925
1926 userGroupRoleLocalService.deleteUserGroupRolesByUserId(
1927 user.getUserId());
1928
1929
1930
1931 userPersistence.remove(user);
1932
1933
1934
1935 PermissionCacheUtil.clearCache();
1936
1937
1938
1939 workflowInstanceLinkLocalService.deleteWorkflowInstanceLinks(
1940 user.getCompanyId(), 0, User.class.getName(), user.getUserId());
1941
1942 return user;
1943 }
1944
1945
1953 @Override
1954 public void deleteUserGroupUser(long userGroupId, long userId)
1955 throws PortalException, SystemException {
1956
1957 userGroupPersistence.removeUser(userGroupId, userId);
1958
1959 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
1960
1961 indexer.reindex(userId);
1962
1963 PermissionCacheUtil.clearCache();
1964 }
1965
1966
1975 @Override
1976 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
1977 public String encryptUserId(String name)
1978 throws PortalException, SystemException {
1979
1980 long userId = GetterUtil.getLong(name);
1981
1982 User user = userPersistence.findByPrimaryKey(userId);
1983
1984 Company company = companyPersistence.findByPrimaryKey(
1985 user.getCompanyId());
1986
1987 try {
1988 return Encryptor.encrypt(company.getKeyObj(), name);
1989 }
1990 catch (EncryptorException ee) {
1991 throw new SystemException(ee);
1992 }
1993 }
1994
1995
2004 @Override
2005 public User fetchUserByEmailAddress(long companyId, String emailAddress)
2006 throws SystemException {
2007
2008 emailAddress = getLogin(emailAddress);
2009
2010 return userPersistence.fetchByC_EA(companyId, emailAddress);
2011 }
2012
2013
2022 @Override
2023 public User fetchUserByFacebookId(long companyId, long facebookId)
2024 throws SystemException {
2025
2026 return userPersistence.fetchByC_FID(companyId, facebookId);
2027 }
2028
2029
2037 @Override
2038 public User fetchUserById(long userId) throws SystemException {
2039 return userPersistence.fetchByPrimaryKey(userId);
2040 }
2041
2042
2051 @Override
2052 public User fetchUserByOpenId(long companyId, String openId)
2053 throws SystemException {
2054
2055 return userPersistence.fetchByC_O(companyId, openId);
2056 }
2057
2058
2067 @Override
2068 public User fetchUserByScreenName(long companyId, String screenName)
2069 throws SystemException {
2070
2071 screenName = getLogin(screenName);
2072
2073 return userPersistence.fetchByC_SN(companyId, screenName);
2074 }
2075
2076
2095 @Override
2096 public List<User> getCompanyUsers(long companyId, int start, int end)
2097 throws SystemException {
2098
2099 return userPersistence.findByCompanyId(companyId, start, end);
2100 }
2101
2102
2109 @Override
2110 public int getCompanyUsersCount(long companyId) throws SystemException {
2111 return userPersistence.countByCompanyId(companyId);
2112 }
2113
2114
2123 @Override
2124 @Skip
2125 public User getDefaultUser(long companyId)
2126 throws PortalException, SystemException {
2127
2128 User userModel = _defaultUsers.get(companyId);
2129
2130 if (userModel == null) {
2131 userModel = userLocalService.loadGetDefaultUser(companyId);
2132
2133 _defaultUsers.put(companyId, userModel);
2134 }
2135
2136 return userModel;
2137 }
2138
2139
2148 @Override
2149 @Skip
2150 public long getDefaultUserId(long companyId)
2151 throws PortalException, SystemException {
2152
2153 User user = getDefaultUser(companyId);
2154
2155 return user.getUserId();
2156 }
2157
2158
2165 @Override
2166 public long[] getGroupUserIds(long groupId) throws SystemException {
2167 return getUserIds(getGroupUsers(groupId));
2168 }
2169
2170
2180 @Override
2181 public int getGroupUsersCount(long groupId, int status)
2182 throws PortalException, SystemException {
2183
2184 Group group = groupPersistence.findByPrimaryKey(groupId);
2185
2186 LinkedHashMap<String, Object> params =
2187 new LinkedHashMap<String, Object>();
2188
2189 params.put("usersGroups", new Long(groupId));
2190
2191 return searchCount(group.getCompanyId(), null, status, params);
2192 }
2193
2194
2202 @Override
2203 public List<User> getNoAnnouncementsDeliveries(String type)
2204 throws SystemException {
2205
2206 return userFinder.findByNoAnnouncementsDeliveries(type);
2207 }
2208
2209
2215 @Override
2216 public List<User> getNoContacts() throws SystemException {
2217 return userFinder.findByNoContacts();
2218 }
2219
2220
2227 @Override
2228 public List<User> getNoGroups() throws SystemException {
2229 return userFinder.findByNoGroups();
2230 }
2231
2232
2239 @Override
2240 public long[] getOrganizationUserIds(long organizationId)
2241 throws SystemException {
2242
2243 return getUserIds(getOrganizationUsers(organizationId));
2244 }
2245
2246
2257 @Override
2258 public int getOrganizationUsersCount(long organizationId, int status)
2259 throws PortalException, SystemException {
2260
2261 Organization organization = organizationPersistence.findByPrimaryKey(
2262 organizationId);
2263
2264 LinkedHashMap<String, Object> params =
2265 new LinkedHashMap<String, Object>();
2266
2267 params.put("usersOrgs", new Long(organizationId));
2268
2269 return searchCount(organization.getCompanyId(), null, status, params);
2270 }
2271
2272
2279 @Override
2280 public long[] getRoleUserIds(long roleId) throws SystemException {
2281 return getUserIds(getRoleUsers(roleId));
2282 }
2283
2284
2294 @Override
2295 public int getRoleUsersCount(long roleId, int status)
2296 throws PortalException, SystemException {
2297
2298 Role role = rolePersistence.findByPrimaryKey(roleId);
2299
2300 LinkedHashMap<String, Object> params =
2301 new LinkedHashMap<String, Object>();
2302
2303 params.put("usersRoles", new Long(roleId));
2304
2305 return searchCount(role.getCompanyId(), null, status, params);
2306 }
2307
2308
2335 @Override
2336 public List<User> getSocialUsers(
2337 long userId, int type, int start, int end, OrderByComparator obc)
2338 throws PortalException, SystemException {
2339
2340 User user = userPersistence.findByPrimaryKey(userId);
2341
2342 LinkedHashMap<String, Object> params =
2343 new LinkedHashMap<String, Object>();
2344
2345 params.put("socialRelationType", new Long[] {userId, new Long(type)});
2346
2347 return search(
2348 user.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,
2349 params, start, end, obc);
2350 }
2351
2352
2375 @Override
2376 public List<User> getSocialUsers(
2377 long userId, int start, int end, OrderByComparator obc)
2378 throws PortalException, SystemException {
2379
2380 User user = userPersistence.findByPrimaryKey(userId);
2381
2382 LinkedHashMap<String, Object> params =
2383 new LinkedHashMap<String, Object>();
2384
2385 params.put("socialRelation", new Long[] {userId});
2386
2387 return search(
2388 user.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,
2389 params, start, end, obc);
2390 }
2391
2392
2420 @Override
2421 public List<User> getSocialUsers(
2422 long userId1, long userId2, int type, int start, int end,
2423 OrderByComparator obc)
2424 throws PortalException, SystemException {
2425
2426 User user1 = userPersistence.findByPrimaryKey(userId1);
2427
2428 LinkedHashMap<String, Object> params =
2429 new LinkedHashMap<String, Object>();
2430
2431 params.put(
2432 "socialMutualRelationType",
2433 new Long[] {userId1, new Long(type), userId2, new Long(type)});
2434
2435 return search(
2436 user1.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,
2437 params, start, end, obc);
2438 }
2439
2440
2465 @Override
2466 public List<User> getSocialUsers(
2467 long userId1, long userId2, int start, int end,
2468 OrderByComparator obc)
2469 throws PortalException, SystemException {
2470
2471 User user1 = userPersistence.findByPrimaryKey(userId1);
2472
2473 LinkedHashMap<String, Object> params =
2474 new LinkedHashMap<String, Object>();
2475
2476 params.put("socialMutualRelation", new Long[] {userId1, userId2});
2477
2478 return search(
2479 user1.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,
2480 params, start, end, obc);
2481 }
2482
2483
2491 @Override
2492 public int getSocialUsersCount(long userId)
2493 throws PortalException, SystemException {
2494
2495 User user = userPersistence.findByPrimaryKey(userId);
2496
2497 LinkedHashMap<String, Object> params =
2498 new LinkedHashMap<String, Object>();
2499
2500 params.put("socialRelation", new Long[] {userId});
2501
2502 return searchCount(
2503 user.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,
2504 params);
2505 }
2506
2507
2520 @Override
2521 public int getSocialUsersCount(long userId, int type)
2522 throws PortalException, SystemException {
2523
2524 User user = userPersistence.findByPrimaryKey(userId);
2525
2526 LinkedHashMap<String, Object> params =
2527 new LinkedHashMap<String, Object>();
2528
2529 params.put("socialRelationType", new Long[] {userId, new Long(type)});
2530
2531 return searchCount(
2532 user.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,
2533 params);
2534 }
2535
2536
2546 @Override
2547 public int getSocialUsersCount(long userId1, long userId2)
2548 throws PortalException, SystemException {
2549
2550 User user1 = userPersistence.findByPrimaryKey(userId1);
2551
2552 LinkedHashMap<String, Object> params =
2553 new LinkedHashMap<String, Object>();
2554
2555 params.put("socialMutualRelation", new Long[] {userId1, userId2});
2556
2557 return searchCount(
2558 user1.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,
2559 params);
2560 }
2561
2562
2576 @Override
2577 public int getSocialUsersCount(long userId1, long userId2, int type)
2578 throws PortalException, SystemException {
2579
2580 User user1 = userPersistence.findByPrimaryKey(userId1);
2581
2582 LinkedHashMap<String, Object> params =
2583 new LinkedHashMap<String, Object>();
2584
2585 params.put(
2586 "socialMutualRelationType",
2587 new Long[] {userId1, new Long(type), userId2, new Long(type)});
2588
2589 return searchCount(
2590 user1.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,
2591 params);
2592 }
2593
2594
2602 @Override
2603 public User getUserByContactId(long contactId)
2604 throws PortalException, SystemException {
2605
2606 return userPersistence.findByContactId(contactId);
2607 }
2608
2609
2619 @Override
2620 public User getUserByEmailAddress(long companyId, String emailAddress)
2621 throws PortalException, SystemException {
2622
2623 emailAddress = getLogin(emailAddress);
2624
2625 return userPersistence.findByC_EA(companyId, emailAddress);
2626 }
2627
2628
2637 @Override
2638 public User getUserByFacebookId(long companyId, long facebookId)
2639 throws PortalException, SystemException {
2640
2641 return userPersistence.findByC_FID(companyId, facebookId);
2642 }
2643
2644
2652 @Override
2653 public User getUserById(long userId)
2654 throws PortalException, SystemException {
2655
2656 return userPersistence.findByPrimaryKey(userId);
2657 }
2658
2659
2669 @Override
2670 public User getUserById(long companyId, long userId)
2671 throws PortalException, SystemException {
2672
2673 return userPersistence.findByC_U(companyId, userId);
2674 }
2675
2676
2685 @Override
2686 public User getUserByOpenId(long companyId, String openId)
2687 throws PortalException, SystemException {
2688
2689 return userPersistence.findByC_O(companyId, openId);
2690 }
2691
2692
2700 @Override
2701 public User getUserByPortraitId(long portraitId)
2702 throws PortalException, SystemException {
2703
2704 return userPersistence.findByPortraitId(portraitId);
2705 }
2706
2707
2716 @Override
2717 public User getUserByScreenName(long companyId, String screenName)
2718 throws PortalException, SystemException {
2719
2720 screenName = getLogin(screenName);
2721
2722 return userPersistence.findByC_SN(companyId, screenName);
2723 }
2724
2725
2735 @Override
2736 public User getUserByUuid(String uuid)
2737 throws PortalException, SystemException {
2738
2739 List<User> users = userPersistence.findByUuid(uuid);
2740
2741 if (users.isEmpty()) {
2742 throw new NoSuchUserException();
2743 }
2744 else {
2745 return users.get(0);
2746 }
2747 }
2748
2749
2758 @Override
2759 public User getUserByUuidAndCompanyId(String uuid, long companyId)
2760 throws PortalException, SystemException {
2761
2762 List<User> users = userPersistence.findByUuid_C(uuid, companyId);
2763
2764 if (users.isEmpty()) {
2765 throw new NoSuchUserException();
2766 }
2767 else {
2768 return users.get(0);
2769 }
2770 }
2771
2772
2782 @Override
2783 public int getUserGroupUsersCount(long userGroupId, int status)
2784 throws PortalException, SystemException {
2785
2786 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
2787 userGroupId);
2788
2789 LinkedHashMap<String, Object> params =
2790 new LinkedHashMap<String, Object>();
2791
2792 params.put("usersUserGroups", new Long(userGroupId));
2793
2794 return searchCount(userGroup.getCompanyId(), null, status, params);
2795 }
2796
2797
2807 @Override
2808 public long getUserIdByEmailAddress(long companyId, String emailAddress)
2809 throws PortalException, SystemException {
2810
2811 emailAddress = StringUtil.toLowerCase(emailAddress.trim());
2812
2813 User user = userPersistence.findByC_EA(companyId, emailAddress);
2814
2815 return user.getUserId();
2816 }
2817
2818
2827 @Override
2828 public long getUserIdByScreenName(long companyId, String screenName)
2829 throws PortalException, SystemException {
2830
2831 screenName = getLogin(screenName);
2832
2833 User user = userPersistence.findByC_SN(companyId, screenName);
2834
2835 return user.getUserId();
2836 }
2837
2838
2848 @Override
2849 public boolean hasPasswordPolicyUser(long passwordPolicyId, long userId)
2850 throws SystemException {
2851
2852 return passwordPolicyRelLocalService.hasPasswordPolicyRel(
2853 passwordPolicyId, User.class.getName(), userId);
2854 }
2855
2856
2871 @Override
2872 public boolean hasRoleUser(
2873 long companyId, String name, long userId, boolean inherited)
2874 throws PortalException, SystemException {
2875
2876 return roleLocalService.hasUserRole(userId, companyId, name, inherited);
2877 }
2878
2879
2889 @Override
2890 public boolean isPasswordExpired(User user)
2891 throws PortalException, SystemException {
2892
2893 PasswordPolicy passwordPolicy = user.getPasswordPolicy();
2894
2895 if ((passwordPolicy != null) && passwordPolicy.getExpireable()) {
2896 Date now = new Date();
2897
2898 if (user.getPasswordModifiedDate() == null) {
2899 user.setPasswordModifiedDate(now);
2900
2901 userLocalService.updateUser(user);
2902 }
2903
2904 long passwordStartTime = user.getPasswordModifiedDate().getTime();
2905 long elapsedTime = now.getTime() - passwordStartTime;
2906
2907 if (elapsedTime > (passwordPolicy.getMaxAge() * 1000)) {
2908 return true;
2909 }
2910 else {
2911 return false;
2912 }
2913 }
2914
2915 return false;
2916 }
2917
2918
2930 @Override
2931 public boolean isPasswordExpiringSoon(User user)
2932 throws PortalException, SystemException {
2933
2934 PasswordPolicy passwordPolicy = user.getPasswordPolicy();
2935
2936 if ((passwordPolicy != null) && passwordPolicy.isExpireable() &&
2937 (passwordPolicy.getWarningTime() > 0)) {
2938
2939 Date now = new Date();
2940
2941 if (user.getPasswordModifiedDate() == null) {
2942 user.setPasswordModifiedDate(now);
2943
2944 userLocalService.updateUser(user);
2945 }
2946
2947 long timeModified = user.getPasswordModifiedDate().getTime();
2948 long passwordExpiresOn =
2949 (passwordPolicy.getMaxAge() * 1000) + timeModified;
2950
2951 long timeStartWarning =
2952 passwordExpiresOn - (passwordPolicy.getWarningTime() * 1000);
2953
2954 if (now.getTime() > timeStartWarning) {
2955 return true;
2956 }
2957 else {
2958 return false;
2959 }
2960 }
2961
2962 return false;
2963 }
2964
2965
2973 @Override
2974 public User loadGetDefaultUser(long companyId)
2975 throws PortalException, SystemException {
2976
2977 return userPersistence.findByC_DU(companyId, true);
2978 }
2979
2980
3012 @Override
3013 public List<User> search(
3014 long companyId, String keywords, int status,
3015 LinkedHashMap<String, Object> params, int start, int end,
3016 OrderByComparator obc)
3017 throws SystemException {
3018
3019 return userFinder.findByKeywords(
3020 companyId, keywords, status, params, start, end, obc);
3021 }
3022
3023
3054 @Override
3055 public Hits search(
3056 long companyId, String keywords, int status,
3057 LinkedHashMap<String, Object> params, int start, int end, Sort sort)
3058 throws SystemException {
3059
3060 String firstName = null;
3061 String middleName = null;
3062 String lastName = null;
3063 String fullName = null;
3064 String screenName = null;
3065 String emailAddress = null;
3066 String street = null;
3067 String city = null;
3068 String zip = null;
3069 String region = null;
3070 String country = null;
3071 boolean andOperator = false;
3072
3073 if (Validator.isNotNull(keywords)) {
3074 firstName = keywords;
3075 middleName = keywords;
3076 lastName = keywords;
3077 fullName = keywords;
3078 screenName = keywords;
3079 emailAddress = keywords;
3080 street = keywords;
3081 city = keywords;
3082 zip = keywords;
3083 region = keywords;
3084 country = keywords;
3085 }
3086 else {
3087 andOperator = true;
3088 }
3089
3090 if (params != null) {
3091 params.put("keywords", keywords);
3092 }
3093
3094 return search(
3095 companyId, firstName, middleName, lastName, fullName, screenName,
3096 emailAddress, street, city, zip, region, country, status, params,
3097 andOperator, start, end, sort);
3098 }
3099
3100
3140 @Override
3141 public List<User> search(
3142 long companyId, String firstName, String middleName,
3143 String lastName, String screenName, String emailAddress, int status,
3144 LinkedHashMap<String, Object> params, boolean andSearch, int start,
3145 int end, OrderByComparator obc)
3146 throws SystemException {
3147
3148 return userFinder.findByC_FN_MN_LN_SN_EA_S(
3149 companyId, firstName, middleName, lastName, screenName,
3150 emailAddress, status, params, andSearch, start, end, obc);
3151 }
3152
3153
3192 @Override
3193 public Hits search(
3194 long companyId, String firstName, String middleName,
3195 String lastName, String screenName, String emailAddress, int status,
3196 LinkedHashMap<String, Object> params, boolean andSearch, int start,
3197 int end, Sort sort)
3198 throws SystemException {
3199
3200 return search(
3201 companyId, firstName, middleName, lastName, null, screenName,
3202 emailAddress, null, null, null, null, null, status, params,
3203 andSearch, start, end, sort);
3204 }
3205
3206
3220 @Override
3221 public int searchCount(
3222 long companyId, String keywords, int status,
3223 LinkedHashMap<String, Object> params)
3224 throws SystemException {
3225
3226 return userFinder.countByKeywords(companyId, keywords, status, params);
3227 }
3228
3229
3251 @Override
3252 public int searchCount(
3253 long companyId, String firstName, String middleName,
3254 String lastName, String screenName, String emailAddress, int status,
3255 LinkedHashMap<String, Object> params, boolean andSearch)
3256 throws SystemException {
3257
3258 return userFinder.countByC_FN_MN_LN_SN_EA_S(
3259 companyId, firstName, middleName, lastName, screenName,
3260 emailAddress, status, params, andSearch);
3261 }
3262
3263
3274 @Override
3275 public void sendEmailAddressVerification(
3276 User user, String emailAddress, ServiceContext serviceContext)
3277 throws PortalException, SystemException {
3278
3279 if (user.isEmailAddressVerified() &&
3280 StringUtil.equalsIgnoreCase(emailAddress, user.getEmailAddress())) {
3281
3282 return;
3283 }
3284
3285 Ticket ticket = ticketLocalService.addTicket(
3286 user.getCompanyId(), User.class.getName(), user.getUserId(),
3287 TicketConstants.TYPE_EMAIL_ADDRESS, emailAddress, null,
3288 serviceContext);
3289
3290 String verifyEmailAddressURL =
3291 serviceContext.getPortalURL() + serviceContext.getPathMain() +
3292 "/portal/verify_email_address?ticketKey=" + ticket.getKey();
3293
3294 Layout layout = layoutLocalService.getLayout(serviceContext.getPlid());
3295
3296 Group group = layout.getGroup();
3297
3298 if (!layout.isPrivateLayout() && !group.isUser()) {
3299 verifyEmailAddressURL += "&p_l_id=" + serviceContext.getPlid();
3300 }
3301
3302 String fromName = PrefsPropsUtil.getString(
3303 user.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_NAME);
3304 String fromAddress = PrefsPropsUtil.getString(
3305 user.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
3306
3307 String toName = user.getFullName();
3308 String toAddress = emailAddress;
3309
3310 String subject = PrefsPropsUtil.getContent(
3311 user.getCompanyId(), PropsKeys.ADMIN_EMAIL_VERIFICATION_SUBJECT);
3312
3313 String body = PrefsPropsUtil.getContent(
3314 user.getCompanyId(), PropsKeys.ADMIN_EMAIL_VERIFICATION_BODY);
3315
3316 SubscriptionSender subscriptionSender = new SubscriptionSender();
3317
3318 subscriptionSender.setBody(body);
3319 subscriptionSender.setCompanyId(user.getCompanyId());
3320 subscriptionSender.setContextAttributes(
3321 "[$EMAIL_VERIFICATION_CODE$]", ticket.getKey(),
3322 "[$EMAIL_VERIFICATION_URL$]", verifyEmailAddressURL,
3323 "[$REMOTE_ADDRESS$]", serviceContext.getRemoteAddr(),
3324 "[$REMOTE_HOST$]", serviceContext.getRemoteHost(), "[$USER_ID$]",
3325 user.getUserId(), "[$USER_SCREENNAME$]", user.getScreenName());
3326 subscriptionSender.setFrom(fromAddress, fromName);
3327 subscriptionSender.setHtmlFormat(true);
3328 subscriptionSender.setMailId(
3329 "user", user.getUserId(), System.currentTimeMillis(),
3330 PwdGenerator.getPassword());
3331 subscriptionSender.setServiceContext(serviceContext);
3332 subscriptionSender.setSubject(subject);
3333 subscriptionSender.setUserId(user.getUserId());
3334
3335 subscriptionSender.addRuntimeSubscribers(toAddress, toName);
3336
3337 subscriptionSender.flushNotificationsAsync();
3338 }
3339
3340
3359 @Override
3360 public void sendPassword(
3361 long companyId, String emailAddress, String fromName,
3362 String fromAddress, String subject, String body,
3363 ServiceContext serviceContext)
3364 throws PortalException, SystemException {
3365
3366 Company company = companyPersistence.findByPrimaryKey(companyId);
3367
3368 if (!company.isSendPassword() && !company.isSendPasswordResetLink()) {
3369 return;
3370 }
3371
3372 emailAddress = StringUtil.toLowerCase(emailAddress.trim());
3373
3374 if (Validator.isNull(emailAddress)) {
3375 throw new UserEmailAddressException();
3376 }
3377
3378 User user = userPersistence.findByC_EA(companyId, emailAddress);
3379
3380 PasswordPolicy passwordPolicy = user.getPasswordPolicy();
3381
3382 String newPassword = StringPool.BLANK;
3383 String passwordResetURL = StringPool.BLANK;
3384
3385 if (company.isSendPasswordResetLink()) {
3386 Date expirationDate = null;
3387
3388 if ((passwordPolicy != null) &&
3389 (passwordPolicy.getResetTicketMaxAge() > 0)) {
3390
3391 expirationDate = new Date(
3392 System.currentTimeMillis() +
3393 (passwordPolicy.getResetTicketMaxAge() * 1000));
3394 }
3395
3396 Ticket ticket = ticketLocalService.addTicket(
3397 companyId, User.class.getName(), user.getUserId(),
3398 TicketConstants.TYPE_PASSWORD, null, expirationDate,
3399 serviceContext);
3400
3401 passwordResetURL =
3402 serviceContext.getPortalURL() + serviceContext.getPathMain() +
3403 "/portal/update_password?p_l_id="+
3404 serviceContext.getPlid() +
3405 "&ticketKey=" + ticket.getKey();
3406 }
3407 else {
3408 if (!PasswordEncryptorUtil.PASSWORDS_ENCRYPTION_ALGORITHM.equals(
3409 PasswordEncryptorUtil.TYPE_NONE)) {
3410
3411 if (LDAPSettingsUtil.isPasswordPolicyEnabled(
3412 user.getCompanyId())) {
3413
3414 if (_log.isWarnEnabled()) {
3415 StringBundler sb = new StringBundler(5);
3416
3417 sb.append("When LDAP password policy is enabled, ");
3418 sb.append("it is possible that portal generated ");
3419 sb.append("passwords will not match the LDAP policy.");
3420 sb.append("Using RegExpToolkit to generate new ");
3421 sb.append("password.");
3422
3423 _log.warn(sb.toString());
3424 }
3425
3426 RegExpToolkit regExpToolkit = new RegExpToolkit();
3427
3428 newPassword = regExpToolkit.generate(null);
3429 }
3430 else {
3431 newPassword = PwdToolkitUtil.generate(passwordPolicy);
3432 }
3433
3434 boolean passwordReset = false;
3435
3436 if (passwordPolicy.getChangeable() &&
3437 passwordPolicy.getChangeRequired()) {
3438
3439 passwordReset = true;
3440 }
3441
3442 user.setPassword(PasswordEncryptorUtil.encrypt(newPassword));
3443 user.setPasswordUnencrypted(newPassword);
3444 user.setPasswordEncrypted(true);
3445 user.setPasswordReset(passwordReset);
3446 user.setPasswordModified(true);
3447 user.setPasswordModifiedDate(new Date());
3448
3449 userPersistence.update(user);
3450
3451 user.setPasswordModified(false);
3452 }
3453 else {
3454 newPassword = user.getPassword();
3455 }
3456 }
3457
3458 if (Validator.isNull(fromName)) {
3459 fromName = PrefsPropsUtil.getString(
3460 companyId, PropsKeys.ADMIN_EMAIL_FROM_NAME);
3461 }
3462
3463 if (Validator.isNull(fromAddress)) {
3464 fromAddress = PrefsPropsUtil.getString(
3465 companyId, PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
3466 }
3467
3468 String toName = user.getFullName();
3469 String toAddress = user.getEmailAddress();
3470
3471 if (Validator.isNull(subject)) {
3472 if (company.isSendPasswordResetLink()) {
3473 subject = PrefsPropsUtil.getContent(
3474 companyId, PropsKeys.ADMIN_EMAIL_PASSWORD_RESET_SUBJECT);
3475 }
3476 else {
3477 subject = PrefsPropsUtil.getContent(
3478 companyId, PropsKeys.ADMIN_EMAIL_PASSWORD_SENT_SUBJECT);
3479 }
3480 }
3481
3482 if (Validator.isNull(body)) {
3483 if (company.isSendPasswordResetLink()) {
3484 body = PrefsPropsUtil.getContent(
3485 companyId, PropsKeys.ADMIN_EMAIL_PASSWORD_RESET_BODY);
3486 }
3487 else {
3488 body = PrefsPropsUtil.getContent(
3489 companyId, PropsKeys.ADMIN_EMAIL_PASSWORD_SENT_BODY);
3490 }
3491 }
3492
3493 SubscriptionSender subscriptionSender = new SubscriptionSender();
3494
3495 subscriptionSender.setBody(body);
3496 subscriptionSender.setCompanyId(companyId);
3497 subscriptionSender.setContextAttributes(
3498 "[$PASSWORD_RESET_URL$]", passwordResetURL, "[$REMOTE_ADDRESS$]",
3499 serviceContext.getRemoteAddr(), "[$REMOTE_HOST$]",
3500 serviceContext.getRemoteHost(), "[$USER_ID$]", user.getUserId(),
3501 "[$USER_PASSWORD$]", newPassword, "[$USER_SCREENNAME$]",
3502 user.getScreenName());
3503 subscriptionSender.setFrom(fromAddress, fromName);
3504 subscriptionSender.setHtmlFormat(true);
3505 subscriptionSender.setMailId(
3506 "user", user.getUserId(), System.currentTimeMillis(),
3507 PwdGenerator.getPassword());
3508 subscriptionSender.setServiceContext(serviceContext);
3509 subscriptionSender.setSubject(subject);
3510 subscriptionSender.setUserId(user.getUserId());
3511
3512 subscriptionSender.addRuntimeSubscribers(toAddress, toName);
3513
3514 subscriptionSender.flushNotificationsAsync();
3515 }
3516
3517
3526 @Override
3527 public void setRoleUsers(long roleId, long[] userIds)
3528 throws PortalException, SystemException {
3529
3530 rolePersistence.setUsers(roleId, userIds);
3531
3532 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
3533
3534 indexer.reindex(userIds);
3535
3536 PermissionCacheUtil.clearCache();
3537 }
3538
3539
3548 @Override
3549 @SuppressWarnings("deprecation")
3550 public void setUserGroupUsers(long userGroupId, long[] userIds)
3551 throws PortalException, SystemException {
3552
3553 if (PropsValues.USER_GROUPS_COPY_LAYOUTS_TO_USER_PERSONAL_SITE) {
3554 userGroupLocalService.copyUserGroupLayouts(userGroupId, userIds);
3555 }
3556
3557 userGroupPersistence.setUsers(userGroupId, userIds);
3558
3559 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
3560
3561 indexer.reindex(userIds);
3562
3563 PermissionCacheUtil.clearCache();
3564 }
3565
3566
3574 @Override
3575 public void unsetGroupTeamsUsers(long groupId, long[] userIds)
3576 throws PortalException, SystemException {
3577
3578 List<Team> teams = teamPersistence.findByGroupId(groupId);
3579
3580 for (Team team : teams) {
3581 unsetTeamUsers(team.getTeamId(), userIds);
3582 }
3583
3584 PermissionCacheUtil.clearCache();
3585 }
3586
3587
3597 @Override
3598 public void unsetGroupUsers(
3599 long groupId, long[] userIds, ServiceContext serviceContext)
3600 throws PortalException, SystemException {
3601
3602 userGroupRoleLocalService.deleteUserGroupRoles(
3603 userIds, groupId, RoleConstants.TYPE_SITE);
3604
3605 userLocalService.unsetGroupTeamsUsers(groupId, userIds);
3606
3607 groupPersistence.removeUsers(groupId, userIds);
3608
3609 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
3610
3611 indexer.reindex(userIds);
3612
3613 PermissionCacheUtil.clearCache();
3614 }
3615
3616
3624 @Override
3625 public void unsetOrganizationUsers(long organizationId, long[] userIds)
3626 throws PortalException, SystemException {
3627
3628 Organization organization = organizationPersistence.findByPrimaryKey(
3629 organizationId);
3630
3631 Group group = organization.getGroup();
3632
3633 userGroupRoleLocalService.deleteUserGroupRoles(
3634 userIds, group.getGroupId(), RoleConstants.TYPE_ORGANIZATION);
3635
3636 organizationPersistence.removeUsers(organizationId, userIds);
3637
3638 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
3639
3640 indexer.reindex(userIds);
3641
3642 PermissionCacheUtil.clearCache();
3643 }
3644
3645
3652 @Override
3653 public void unsetPasswordPolicyUsers(long passwordPolicyId, long[] userIds)
3654 throws SystemException {
3655
3656 passwordPolicyRelLocalService.deletePasswordPolicyRels(
3657 passwordPolicyId, User.class.getName(), userIds);
3658 }
3659
3660
3668 @Override
3669 public void unsetRoleUsers(long roleId, List<User> users)
3670 throws PortalException, SystemException {
3671
3672 Role role = rolePersistence.findByPrimaryKey(roleId);
3673
3674 String roleName = role.getName();
3675
3676 if ((roleName.equals(RoleConstants.ADMINISTRATOR) &&
3677 (getRoleUsersCount(role.getRoleId()) <= 1)) ||
3678 roleName.equals(RoleConstants.USER)) {
3679
3680 return;
3681 }
3682
3683 rolePersistence.removeUsers(roleId, users);
3684
3685 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
3686
3687 indexer.reindex(users);
3688
3689 PermissionCacheUtil.clearCache();
3690 }
3691
3692
3700 @Override
3701 public void unsetRoleUsers(long roleId, long[] userIds)
3702 throws PortalException, SystemException {
3703
3704 Role role = rolePersistence.findByPrimaryKey(roleId);
3705
3706 String roleName = role.getName();
3707
3708 if (roleName.equals(RoleConstants.USER) ||
3709 (roleName.equals(RoleConstants.ADMINISTRATOR) &&
3710 getRoleUsersCount(role.getRoleId()) <= 1)) {
3711
3712 return;
3713 }
3714
3715 rolePersistence.removeUsers(roleId, userIds);
3716
3717 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
3718
3719 indexer.reindex(userIds);
3720
3721 PermissionCacheUtil.clearCache();
3722 }
3723
3724
3732 @Override
3733 public void unsetTeamUsers(long teamId, long[] userIds)
3734 throws PortalException, SystemException {
3735
3736 teamPersistence.removeUsers(teamId, userIds);
3737
3738 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
3739
3740 indexer.reindex(userIds);
3741
3742 PermissionCacheUtil.clearCache();
3743 }
3744
3745
3753 @Override
3754 public void unsetUserGroupUsers(long userGroupId, long[] userIds)
3755 throws PortalException, SystemException {
3756
3757 userGroupPersistence.removeUsers(userGroupId, userIds);
3758
3759 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
3760
3761 indexer.reindex(userIds);
3762
3763 PermissionCacheUtil.clearCache();
3764 }
3765
3766
3776 @Override
3777 public User updateAgreedToTermsOfUse(
3778 long userId, boolean agreedToTermsOfUse)
3779 throws PortalException, SystemException {
3780
3781 User user = userPersistence.findByPrimaryKey(userId);
3782
3783 user.setAgreedToTermsOfUse(agreedToTermsOfUse);
3784
3785 userPersistence.update(user);
3786
3787 return user;
3788 }
3789
3790
3801 @Override
3802 public void updateAsset(
3803 long userId, User user, long[] assetCategoryIds,
3804 String[] assetTagNames)
3805 throws PortalException, SystemException {
3806
3807 User owner = userPersistence.findByPrimaryKey(userId);
3808
3809 Company company = companyPersistence.findByPrimaryKey(
3810 owner.getCompanyId());
3811
3812 Group companyGroup = company.getGroup();
3813
3814 assetEntryLocalService.updateEntry(
3815 userId, companyGroup.getGroupId(), user.getCreateDate(),
3816 user.getModifiedDate(), User.class.getName(), user.getUserId(),
3817 user.getUuid(), 0, assetCategoryIds, assetTagNames, false, null,
3818 null, null, null, user.getFullName(), null, null, null, null, 0, 0,
3819 null, false);
3820 }
3821
3822
3831 @Override
3832 public User updateCreateDate(long userId, Date createDate)
3833 throws PortalException, SystemException {
3834
3835 User user = userPersistence.findByPrimaryKey(userId);
3836
3837 user.setCreateDate(createDate);
3838
3839 userPersistence.update(user);
3840
3841 return user;
3842 }
3843
3844
3855 @Override
3856 public User updateEmailAddress(
3857 long userId, String password, String emailAddress1,
3858 String emailAddress2)
3859 throws PortalException, SystemException {
3860
3861 emailAddress1 = StringUtil.toLowerCase(emailAddress1.trim());
3862 emailAddress2 = StringUtil.toLowerCase(emailAddress2.trim());
3863
3864 User user = userPersistence.findByPrimaryKey(userId);
3865
3866 validateEmailAddress(user, emailAddress1, emailAddress2);
3867
3868 setEmailAddress(
3869 user, password, user.getFirstName(), user.getMiddleName(),
3870 user.getLastName(), emailAddress1);
3871
3872 userPersistence.update(user);
3873
3874 Contact contact = user.getContact();
3875
3876 contact.setEmailAddress(user.getEmailAddress());
3877
3878 contactPersistence.update(contact);
3879
3880 return user;
3881 }
3882
3883
3897 @Override
3898 public User updateEmailAddress(
3899 long userId, String password, String emailAddress1,
3900 String emailAddress2, ServiceContext serviceContext)
3901 throws PortalException, SystemException {
3902
3903 emailAddress1 = StringUtil.toLowerCase(emailAddress1.trim());
3904 emailAddress2 = StringUtil.toLowerCase(emailAddress2.trim());
3905
3906 User user = userPersistence.findByPrimaryKey(userId);
3907
3908 validateEmailAddress(user, emailAddress1, emailAddress2);
3909
3910 Company company = companyPersistence.findByPrimaryKey(
3911 user.getCompanyId());
3912
3913 if (!company.isStrangersVerify()) {
3914 setEmailAddress(
3915 user, password, user.getFirstName(), user.getMiddleName(),
3916 user.getLastName(), emailAddress1);
3917
3918 userPersistence.update(user);
3919
3920 Contact contact = user.getContact();
3921
3922 contact.setEmailAddress(user.getEmailAddress());
3923
3924 contactPersistence.update(contact);
3925 }
3926 else {
3927 sendEmailAddressVerification(user, emailAddress1, serviceContext);
3928 }
3929
3930 return user;
3931 }
3932
3933
3942 @Override
3943 public User updateEmailAddressVerified(
3944 long userId, boolean emailAddressVerified)
3945 throws PortalException, SystemException {
3946
3947 User user = userPersistence.findByPrimaryKey(userId);
3948
3949 user.setEmailAddressVerified(emailAddressVerified);
3950
3951 userPersistence.update(user);
3952
3953 return user;
3954 }
3955
3956
3965 @Override
3966 public User updateFacebookId(long userId, long facebookId)
3967 throws PortalException, SystemException {
3968
3969 User user = userPersistence.findByPrimaryKey(userId);
3970
3971 user.setFacebookId(facebookId);
3972
3973 userPersistence.update(user);
3974
3975 return user;
3976 }
3977
3978
3988 @Override
3989 public void updateGroups(
3990 long userId, long[] newGroupIds, ServiceContext serviceContext)
3991 throws PortalException, SystemException {
3992
3993 updateGroups(
3994 userId, newGroupIds, serviceContext,
3995 serviceContext.isIndexingEnabled());
3996 }
3997
3998
4037 @Override
4038 public User updateIncompleteUser(
4039 long creatorUserId, long companyId, boolean autoPassword,
4040 String password1, String password2, boolean autoScreenName,
4041 String screenName, String emailAddress, long facebookId,
4042 String openId, Locale locale, String firstName, String middleName,
4043 String lastName, int prefixId, int suffixId, boolean male,
4044 int birthdayMonth, int birthdayDay, int birthdayYear,
4045 String jobTitle, boolean updateUserInformation, boolean sendEmail,
4046 ServiceContext serviceContext)
4047 throws PortalException, SystemException {
4048
4049 User user = getUserByEmailAddress(companyId, emailAddress);
4050
4051 if (user.getStatus() != WorkflowConstants.STATUS_INCOMPLETE) {
4052 throw new PortalException("Invalid user status");
4053 }
4054
4055 User defaultUser = getDefaultUser(companyId);
4056
4057 if (facebookId > 0) {
4058 autoPassword = false;
4059
4060 if ((password1 == null) || (password2 == null)) {
4061 password1 = PwdGenerator.getPassword();
4062 password2 = password1;
4063 }
4064
4065 sendEmail = false;
4066 }
4067
4068 if (updateUserInformation) {
4069 autoScreenName = false;
4070
4071 if (PrefsPropsUtil.getBoolean(
4072 companyId,
4073 PropsKeys.USERS_SCREEN_NAME_ALWAYS_AUTOGENERATE)) {
4074
4075 autoScreenName = true;
4076 }
4077
4078 validate(
4079 companyId, user.getUserId(), autoPassword, password1, password2,
4080 autoScreenName, screenName, emailAddress, openId, firstName,
4081 middleName, lastName, null);
4082
4083 if (!autoPassword) {
4084 if (Validator.isNull(password1) ||
4085 Validator.isNull(password2)) {
4086 throw new UserPasswordException(
4087 UserPasswordException.PASSWORD_INVALID);
4088 }
4089 }
4090
4091 if (autoScreenName) {
4092 ScreenNameGenerator screenNameGenerator =
4093 ScreenNameGeneratorFactory.getInstance();
4094
4095 try {
4096 screenName = screenNameGenerator.generate(
4097 companyId, user.getUserId(), emailAddress);
4098 }
4099 catch (Exception e) {
4100 throw new SystemException(e);
4101 }
4102 }
4103
4104 FullNameGenerator fullNameGenerator =
4105 FullNameGeneratorFactory.getInstance();
4106
4107 String fullName = fullNameGenerator.getFullName(
4108 firstName, middleName, lastName);
4109
4110 String greeting = LanguageUtil.format(
4111 locale, "welcome-x", " " + fullName, false);
4112
4113 if (Validator.isNotNull(password1)) {
4114 user.setPassword(PasswordEncryptorUtil.encrypt(password1));
4115 user.setPasswordUnencrypted(password1);
4116 }
4117
4118 user.setPasswordEncrypted(true);
4119
4120 PasswordPolicy passwordPolicy = defaultUser.getPasswordPolicy();
4121
4122 if ((passwordPolicy != null) && passwordPolicy.isChangeable() &&
4123 passwordPolicy.isChangeRequired()) {
4124
4125 user.setPasswordReset(true);
4126 }
4127 else {
4128 user.setPasswordReset(false);
4129 }
4130
4131 user.setScreenName(screenName);
4132 user.setFacebookId(facebookId);
4133 user.setOpenId(openId);
4134 user.setLanguageId(locale.toString());
4135 user.setTimeZoneId(defaultUser.getTimeZoneId());
4136 user.setGreeting(greeting);
4137 user.setFirstName(firstName);
4138 user.setMiddleName(middleName);
4139 user.setLastName(lastName);
4140 user.setJobTitle(jobTitle);
4141 user.setExpandoBridgeAttributes(serviceContext);
4142
4143 Date birthday = getBirthday(
4144 birthdayMonth, birthdayDay, birthdayYear);
4145
4146 Contact contact = user.getContact();
4147
4148 contact.setFirstName(firstName);
4149 contact.setMiddleName(middleName);
4150 contact.setLastName(lastName);
4151 contact.setPrefixId(prefixId);
4152 contact.setSuffixId(suffixId);
4153 contact.setMale(male);
4154 contact.setBirthday(birthday);
4155 contact.setJobTitle(jobTitle);
4156
4157 contactPersistence.update(contact, serviceContext);
4158
4159
4160
4161 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
4162 User.class);
4163
4164 indexer.reindex(user);
4165 }
4166
4167 user.setStatus(WorkflowConstants.STATUS_DRAFT);
4168
4169 userPersistence.update(user, serviceContext);
4170
4171
4172
4173 long workflowUserId = creatorUserId;
4174
4175 if (workflowUserId == user.getUserId()) {
4176 workflowUserId = defaultUser.getUserId();
4177 }
4178
4179 ServiceContext workflowServiceContext = serviceContext;
4180
4181 if (workflowServiceContext == null) {
4182 workflowServiceContext = new ServiceContext();
4183 }
4184
4185 workflowServiceContext.setAttribute("autoPassword", autoPassword);
4186 workflowServiceContext.setAttribute("sendEmail", sendEmail);
4187
4188 WorkflowHandlerRegistryUtil.startWorkflowInstance(
4189 companyId, workflowUserId, User.class.getName(), user.getUserId(),
4190 user, workflowServiceContext);
4191
4192 return getUserByEmailAddress(companyId, emailAddress);
4193 }
4194
4195
4205 @Override
4206 public User updateJobTitle(long userId, String jobTitle)
4207 throws PortalException, SystemException {
4208
4209 User user = userPersistence.findByPrimaryKey(userId);
4210
4211 user.setJobTitle(jobTitle);
4212
4213 userPersistence.update(user);
4214
4215 Contact contact = contactPersistence.findByPrimaryKey(
4216 user.getContactId());
4217
4218 contact.setJobTitle(jobTitle);
4219
4220 contactPersistence.update(contact);
4221
4222 return user;
4223 }
4224
4225
4234 @Override
4235 public User updateLastLogin(long userId, String loginIP)
4236 throws PortalException, SystemException {
4237
4238 User user = userPersistence.findByPrimaryKey(userId);
4239
4240 Date lastLoginDate = user.getLoginDate();
4241
4242 if (lastLoginDate == null) {
4243 lastLoginDate = new Date();
4244 }
4245
4246 user.setLoginDate(new Date());
4247 user.setLoginIP(loginIP);
4248 user.setLastLoginDate(lastLoginDate);
4249 user.setLastLoginIP(user.getLoginIP());
4250 user.setLastFailedLoginDate(null);
4251 user.setFailedLoginAttempts(0);
4252
4253 userPersistence.update(user);
4254
4255 return user;
4256 }
4257
4258
4267 @Override
4268 public User updateLockout(User user, boolean lockout)
4269 throws PortalException, SystemException {
4270
4271 PasswordPolicy passwordPolicy = user.getPasswordPolicy();
4272
4273 if ((passwordPolicy == null) || !passwordPolicy.isLockout()) {
4274 return user;
4275 }
4276
4277 Date lockoutDate = null;
4278
4279 if (lockout) {
4280 lockoutDate = new Date();
4281 }
4282
4283 user.setLockout(lockout);
4284 user.setLockoutDate(lockoutDate);
4285
4286 if (!lockout) {
4287 user.setLastFailedLoginDate(lockoutDate);
4288 user.setFailedLoginAttempts(0);
4289 }
4290
4291 userPersistence.update(user);
4292
4293 return user;
4294 }
4295
4296
4307 @Override
4308 public User updateLockoutByEmailAddress(
4309 long companyId, String emailAddress, boolean lockout)
4310 throws PortalException, SystemException {
4311
4312 User user = getUserByEmailAddress(companyId, emailAddress);
4313
4314 return updateLockout(user, lockout);
4315 }
4316
4317
4326 @Override
4327 public User updateLockoutById(long userId, boolean lockout)
4328 throws PortalException, SystemException {
4329
4330 User user = userPersistence.findByPrimaryKey(userId);
4331
4332 return updateLockout(user, lockout);
4333 }
4334
4335
4345 @Override
4346 public User updateLockoutByScreenName(
4347 long companyId, String screenName, boolean lockout)
4348 throws PortalException, SystemException {
4349
4350 User user = getUserByScreenName(companyId, screenName);
4351
4352 return updateLockout(user, lockout);
4353 }
4354
4355
4364 @Override
4365 public User updateModifiedDate(long userId, Date modifiedDate)
4366 throws PortalException, SystemException {
4367
4368 User user = userPersistence.findByPrimaryKey(userId);
4369
4370 user.setModifiedDate(modifiedDate);
4371
4372 userPersistence.update(user);
4373
4374 return user;
4375 }
4376
4377
4386 @Override
4387 public User updateOpenId(long userId, String openId)
4388 throws PortalException, SystemException {
4389
4390 openId = openId.trim();
4391
4392 User user = userPersistence.findByPrimaryKey(userId);
4393
4394 user.setOpenId(openId);
4395
4396 userPersistence.update(user);
4397
4398 return user;
4399 }
4400
4401
4412 @Override
4413 public void updateOrganizations(
4414 long userId, long[] newOrganizationIds,
4415 ServiceContext serviceContext)
4416 throws PortalException, SystemException {
4417
4418 updateOrganizations(
4419 userId, newOrganizationIds, serviceContext.isIndexingEnabled());
4420 }
4421
4422
4434 @Override
4435 public User updatePassword(
4436 long userId, String password1, String password2,
4437 boolean passwordReset)
4438 throws PortalException, SystemException {
4439
4440 return updatePassword(
4441 userId, password1, password2, passwordReset, false);
4442 }
4443
4444
4459 @Override
4460 public User updatePassword(
4461 long userId, String password1, String password2,
4462 boolean passwordReset, boolean silentUpdate)
4463 throws PortalException, SystemException {
4464
4465 User user = userPersistence.findByPrimaryKey(userId);
4466
4467 if (!silentUpdate) {
4468 validatePassword(user.getCompanyId(), userId, password1, password2);
4469 }
4470
4471 String oldEncPwd = user.getPassword();
4472
4473 if (!user.isPasswordEncrypted()) {
4474 oldEncPwd = PasswordEncryptorUtil.encrypt(user.getPassword());
4475 }
4476
4477 String newEncPwd = PasswordEncryptorUtil.encrypt(password1);
4478
4479 if (user.hasCompanyMx()) {
4480 mailService.updatePassword(user.getCompanyId(), userId, password1);
4481 }
4482
4483 user.setPassword(newEncPwd);
4484 user.setPasswordUnencrypted(password1);
4485 user.setPasswordEncrypted(true);
4486 user.setPasswordReset(passwordReset);
4487 user.setPasswordModifiedDate(new Date());
4488 user.setDigest(StringPool.BLANK);
4489 user.setGraceLoginCount(0);
4490
4491 if (!silentUpdate) {
4492 user.setPasswordModified(true);
4493 }
4494
4495 try {
4496 userPersistence.update(user);
4497 }
4498 catch (ModelListenerException mle) {
4499 String msg = GetterUtil.getString(mle.getCause().getMessage());
4500
4501 if (LDAPSettingsUtil.isPasswordPolicyEnabled(user.getCompanyId())) {
4502 String passwordHistory = PrefsPropsUtil.getString(
4503 user.getCompanyId(), PropsKeys.LDAP_ERROR_PASSWORD_HISTORY);
4504
4505 if (msg.contains(passwordHistory)) {
4506 throw new UserPasswordException(
4507 UserPasswordException.PASSWORD_ALREADY_USED);
4508 }
4509 }
4510
4511 throw new UserPasswordException(
4512 UserPasswordException.PASSWORD_INVALID);
4513 }
4514
4515 if (!silentUpdate) {
4516 user.setPasswordModified(false);
4517
4518 passwordTrackerLocalService.trackPassword(userId, oldEncPwd);
4519 }
4520
4521 return user;
4522 }
4523
4524
4538 @Override
4539 public User updatePasswordManually(
4540 long userId, String password, boolean passwordEncrypted,
4541 boolean passwordReset, Date passwordModifiedDate)
4542 throws PortalException, SystemException {
4543
4544
4545
4546 User user = userPersistence.findByPrimaryKey(userId);
4547
4548 user.setPassword(password);
4549 user.setPasswordEncrypted(passwordEncrypted);
4550 user.setPasswordReset(passwordReset);
4551 user.setPasswordModifiedDate(passwordModifiedDate);
4552 user.setDigest(StringPool.BLANK);
4553
4554 userPersistence.update(user);
4555
4556 return user;
4557 }
4558
4559
4570 @Override
4571 public User updatePasswordReset(long userId, boolean passwordReset)
4572 throws PortalException, SystemException {
4573
4574 User user = userPersistence.findByPrimaryKey(userId);
4575
4576 user.setPasswordReset(passwordReset);
4577
4578 userPersistence.update(user);
4579
4580 return user;
4581 }
4582
4583
4593 @Override
4594 public User updatePortrait(long userId, byte[] bytes)
4595 throws PortalException, SystemException {
4596
4597 User user = userPersistence.findByPrimaryKey(userId);
4598
4599 long imageMaxSize = PrefsPropsUtil.getLong(
4600 PropsKeys.USERS_IMAGE_MAX_SIZE);
4601
4602 if ((imageMaxSize > 0) &&
4603 ((bytes == null) || (bytes.length > imageMaxSize))) {
4604
4605 throw new UserPortraitSizeException();
4606 }
4607
4608 long portraitId = user.getPortraitId();
4609
4610 if (portraitId <= 0) {
4611 portraitId = counterLocalService.increment();
4612
4613 user.setPortraitId(portraitId);
4614 }
4615
4616 try {
4617 ImageBag imageBag = ImageToolUtil.read(bytes);
4618
4619 RenderedImage renderedImage = imageBag.getRenderedImage();
4620
4621 if (renderedImage == null) {
4622 throw new UserPortraitTypeException();
4623 }
4624
4625 renderedImage = ImageToolUtil.scale(
4626 renderedImage, PropsValues.USERS_IMAGE_MAX_HEIGHT,
4627 PropsValues.USERS_IMAGE_MAX_WIDTH);
4628
4629 String contentType = imageBag.getType();
4630
4631 imageLocalService.updateImage(
4632 portraitId,
4633 ImageToolUtil.getBytes(renderedImage, contentType));
4634 }
4635 catch (IOException ioe) {
4636 throw new ImageSizeException(ioe);
4637 }
4638
4639 userPersistence.update(user);
4640
4641 return user;
4642 }
4643
4644
4655 @Override
4656 public User updateReminderQuery(long userId, String question, String answer)
4657 throws PortalException, SystemException {
4658
4659 validateReminderQuery(question, answer);
4660
4661 User user = userPersistence.findByPrimaryKey(userId);
4662
4663 user.setReminderQueryQuestion(question);
4664 user.setReminderQueryAnswer(answer);
4665
4666 userPersistence.update(user);
4667
4668 return user;
4669 }
4670
4671
4681 @Override
4682 public User updateScreenName(long userId, String screenName)
4683 throws PortalException, SystemException {
4684
4685
4686
4687 User user = userPersistence.findByPrimaryKey(userId);
4688
4689 screenName = getLogin(screenName);
4690
4691 validateScreenName(user.getCompanyId(), userId, screenName);
4692
4693 if (!StringUtil.equalsIgnoreCase(user.getScreenName(), screenName)) {
4694 user.setDigest(StringPool.BLANK);
4695 }
4696
4697 user.setScreenName(screenName);
4698
4699 userPersistence.update(user);
4700
4701
4702
4703 Group group = groupLocalService.getUserGroup(
4704 user.getCompanyId(), userId);
4705
4706 group.setFriendlyURL(StringPool.SLASH + screenName);
4707
4708 groupPersistence.update(group);
4709
4710 return user;
4711 }
4712
4713
4722 @Override
4723 public User updateStatus(long userId, int status)
4724 throws PortalException, SystemException {
4725
4726 User user = userPersistence.findByPrimaryKey(userId);
4727
4728 if ((status == WorkflowConstants.STATUS_APPROVED) &&
4729 (user.getStatus() != WorkflowConstants.STATUS_APPROVED)) {
4730
4731 validateCompanyMaxUsers(user.getCompanyId());
4732 }
4733
4734 user.setStatus(status);
4735
4736 userPersistence.update(user);
4737
4738 reindex(user);
4739
4740 return user;
4741 }
4742
4743
4799 @Override
4800 @SuppressWarnings("deprecation")
4801 public User updateUser(
4802 long userId, String oldPassword, String newPassword1,
4803 String newPassword2, boolean passwordReset,
4804 String reminderQueryQuestion, String reminderQueryAnswer,
4805 String screenName, String emailAddress, long facebookId,
4806 String openId, String languageId, String timeZoneId,
4807 String greeting, String comments, String firstName,
4808 String middleName, String lastName, int prefixId, int suffixId,
4809 boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
4810 String smsSn, String aimSn, String facebookSn, String icqSn,
4811 String jabberSn, String msnSn, String mySpaceSn, String skypeSn,
4812 String twitterSn, String ymSn, String jobTitle, long[] groupIds,
4813 long[] organizationIds, long[] roleIds,
4814 List<UserGroupRole> userGroupRoles, long[] userGroupIds,
4815 ServiceContext serviceContext)
4816 throws PortalException, SystemException {
4817
4818
4819
4820 User user = userPersistence.findByPrimaryKey(userId);
4821 Company company = companyPersistence.findByPrimaryKey(
4822 user.getCompanyId());
4823 String password = oldPassword;
4824 screenName = getLogin(screenName);
4825 emailAddress = StringUtil.toLowerCase(emailAddress.trim());
4826 openId = openId.trim();
4827 String oldFullName = user.getFullName();
4828 aimSn = StringUtil.toLowerCase(aimSn.trim());
4829 facebookSn = StringUtil.toLowerCase(facebookSn.trim());
4830 icqSn = StringUtil.toLowerCase(icqSn.trim());
4831 jabberSn = StringUtil.toLowerCase(jabberSn.trim());
4832 msnSn = StringUtil.toLowerCase(msnSn.trim());
4833 mySpaceSn = StringUtil.toLowerCase(mySpaceSn.trim());
4834 skypeSn = StringUtil.toLowerCase(skypeSn.trim());
4835 twitterSn = StringUtil.toLowerCase(twitterSn.trim());
4836 ymSn = StringUtil.toLowerCase(ymSn.trim());
4837 Date now = new Date();
4838
4839 EmailAddressGenerator emailAddressGenerator =
4840 EmailAddressGeneratorFactory.getInstance();
4841
4842 if (emailAddressGenerator.isGenerated(emailAddress)) {
4843 emailAddress = StringPool.BLANK;
4844 }
4845
4846 if (!PropsValues.USERS_EMAIL_ADDRESS_REQUIRED &&
4847 Validator.isNull(emailAddress)) {
4848
4849 emailAddress = emailAddressGenerator.generate(
4850 user.getCompanyId(), userId);
4851 }
4852
4853 validate(
4854 userId, screenName, emailAddress, openId, firstName, middleName,
4855 lastName, smsSn);
4856
4857 if (Validator.isNotNull(newPassword1) ||
4858 Validator.isNotNull(newPassword2)) {
4859
4860 user = updatePassword(
4861 userId, newPassword1, newPassword2, passwordReset);
4862
4863 password = newPassword1;
4864
4865 user.setDigest(StringPool.BLANK);
4866 }
4867
4868 user.setModifiedDate(now);
4869
4870 if (user.getContactId() <= 0) {
4871 user.setContactId(counterLocalService.increment());
4872 }
4873
4874 user.setPasswordReset(passwordReset);
4875
4876 if (Validator.isNotNull(reminderQueryQuestion) &&
4877 Validator.isNotNull(reminderQueryAnswer)) {
4878
4879 user.setReminderQueryQuestion(reminderQueryQuestion);
4880 user.setReminderQueryAnswer(reminderQueryAnswer);
4881 }
4882
4883 if (!StringUtil.equalsIgnoreCase(user.getScreenName(), screenName)) {
4884 user.setScreenName(screenName);
4885
4886 user.setDigest(StringPool.BLANK);
4887 }
4888
4889 boolean sendEmailAddressVerification = false;
4890
4891 if (!company.isStrangersVerify()) {
4892 setEmailAddress(
4893 user, password, firstName, middleName, lastName, emailAddress);
4894 }
4895 else {
4896 sendEmailAddressVerification = true;
4897 }
4898
4899 if (serviceContext != null) {
4900 String uuid = serviceContext.getUuid();
4901
4902 if (Validator.isNotNull(uuid)) {
4903 user.setUuid(uuid);
4904 }
4905 }
4906
4907 user.setFacebookId(facebookId);
4908
4909 Long ldapServerId = (Long)serviceContext.getAttribute("ldapServerId");
4910
4911 if (ldapServerId != null) {
4912 user.setLdapServerId(ldapServerId);
4913 }
4914
4915 user.setOpenId(openId);
4916 user.setLanguageId(languageId);
4917 user.setTimeZoneId(timeZoneId);
4918 user.setGreeting(greeting);
4919 user.setComments(comments);
4920 user.setFirstName(firstName);
4921 user.setMiddleName(middleName);
4922 user.setLastName(lastName);
4923 user.setJobTitle(jobTitle);
4924 user.setExpandoBridgeAttributes(serviceContext);
4925
4926 userPersistence.update(user, serviceContext);
4927
4928
4929
4930 Date birthday = getBirthday(birthdayMonth, birthdayDay, birthdayYear);
4931
4932 long contactId = user.getContactId();
4933
4934 Contact contact = contactPersistence.fetchByPrimaryKey(contactId);
4935
4936 if (contact == null) {
4937 contact = contactPersistence.create(contactId);
4938
4939 contact.setCompanyId(user.getCompanyId());
4940 contact.setUserName(StringPool.BLANK);
4941 contact.setCreateDate(now);
4942 contact.setClassName(User.class.getName());
4943 contact.setClassPK(user.getUserId());
4944 contact.setAccountId(company.getAccountId());
4945 contact.setParentContactId(
4946 ContactConstants.DEFAULT_PARENT_CONTACT_ID);
4947 }
4948
4949 contact.setModifiedDate(now);
4950 contact.setEmailAddress(user.getEmailAddress());
4951 contact.setFirstName(firstName);
4952 contact.setMiddleName(middleName);
4953 contact.setLastName(lastName);
4954 contact.setPrefixId(prefixId);
4955 contact.setSuffixId(suffixId);
4956 contact.setMale(male);
4957 contact.setBirthday(birthday);
4958 contact.setSmsSn(smsSn);
4959 contact.setAimSn(aimSn);
4960 contact.setFacebookSn(facebookSn);
4961 contact.setIcqSn(icqSn);
4962 contact.setJabberSn(jabberSn);
4963 contact.setMsnSn(msnSn);
4964 contact.setMySpaceSn(mySpaceSn);
4965 contact.setSkypeSn(skypeSn);
4966 contact.setTwitterSn(twitterSn);
4967 contact.setYmSn(ymSn);
4968 contact.setJobTitle(jobTitle);
4969
4970 contactPersistence.update(contact, serviceContext);
4971
4972
4973
4974 Group group = groupLocalService.getUserGroup(
4975 user.getCompanyId(), userId);
4976
4977 group.setFriendlyURL(StringPool.SLASH + screenName);
4978
4979 groupPersistence.update(group);
4980
4981
4982
4983
4984
4985
4986
4987 List<UserGroupRole> previousUserGroupRoles =
4988 userGroupRolePersistence.findByUserId(userId);
4989
4990 updateGroups(userId, groupIds, serviceContext, false);
4991 updateOrganizations(userId, organizationIds, false);
4992
4993
4994
4995 if (roleIds != null) {
4996 roleIds = UsersAdminUtil.addRequiredRoles(user, roleIds);
4997
4998 userPersistence.setRoles(userId, roleIds);
4999 }
5000
5001
5002
5003 updateUserGroupRoles(
5004 user, groupIds, organizationIds, userGroupRoles,
5005 previousUserGroupRoles);
5006
5007
5008
5009 if (userGroupIds != null) {
5010 if (PropsValues.USER_GROUPS_COPY_LAYOUTS_TO_USER_PERSONAL_SITE) {
5011 userGroupLocalService.copyUserGroupLayouts(
5012 userGroupIds, userId);
5013 }
5014
5015 userPersistence.setUserGroups(userId, userGroupIds);
5016 }
5017
5018
5019
5020 announcementsDeliveryLocalService.getUserDeliveries(user.getUserId());
5021
5022
5023
5024 if (serviceContext != null) {
5025 updateAsset(
5026 userId, user, serviceContext.getAssetCategoryIds(),
5027 serviceContext.getAssetTagNames());
5028 }
5029
5030
5031
5032 if (GetterUtil.getBoolean(
5033 PropsKeys.USERS_UPDATE_USER_NAME + MBMessage.class.getName()) &&
5034 !oldFullName.equals(user.getFullName())) {
5035
5036 mbMessageLocalService.updateUserName(userId, user.getFullName());
5037 }
5038
5039
5040
5041 if ((serviceContext == null) || serviceContext.isIndexingEnabled()) {
5042 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
5043 User.class);
5044
5045 indexer.reindex(user);
5046 }
5047
5048
5049
5050 if ((serviceContext != null) && sendEmailAddressVerification) {
5051 sendEmailAddressVerification(user, emailAddress, serviceContext);
5052 }
5053
5054
5055
5056 PermissionCacheUtil.clearCache();
5057
5058 return user;
5059 }
5060
5061
5070 @Override
5071 public void verifyEmailAddress(String ticketKey)
5072 throws PortalException, SystemException {
5073
5074 Ticket ticket = ticketLocalService.getTicket(ticketKey);
5075
5076 if (ticket.isExpired() ||
5077 (ticket.getType() != TicketConstants.TYPE_EMAIL_ADDRESS)) {
5078
5079 throw new NoSuchTicketException();
5080 }
5081
5082 User user = userPersistence.findByPrimaryKey(ticket.getClassPK());
5083
5084 String emailAddress = ticket.getExtraInfo();
5085
5086 emailAddress = StringUtil.toLowerCase(emailAddress).trim();
5087
5088 if (!emailAddress.equals(user.getEmailAddress())) {
5089 if (userPersistence.fetchByC_EA(
5090 user.getCompanyId(), emailAddress) != null) {
5091
5092 throw new DuplicateUserEmailAddressException();
5093 }
5094
5095 setEmailAddress(
5096 user, StringPool.BLANK, user.getFirstName(),
5097 user.getMiddleName(), user.getLastName(), emailAddress);
5098
5099 Contact contact = user.getContact();
5100
5101 contact.setEmailAddress(user.getEmailAddress());
5102
5103 contactPersistence.update(contact);
5104 }
5105
5106 user.setEmailAddressVerified(true);
5107
5108 userPersistence.update(user);
5109
5110 ticketLocalService.deleteTicket(ticket);
5111 }
5112
5113 protected void addDefaultRolesAndTeams(long groupId, long[] userIds)
5114 throws PortalException, SystemException {
5115
5116 List<Role> defaultSiteRoles = new ArrayList<Role>();
5117
5118 Group group = groupLocalService.getGroup(groupId);
5119
5120 UnicodeProperties typeSettingsProperties =
5121 group.getTypeSettingsProperties();
5122
5123 long[] defaultSiteRoleIds = StringUtil.split(
5124 typeSettingsProperties.getProperty("defaultSiteRoleIds"), 0L);
5125
5126 for (long defaultSiteRoleId : defaultSiteRoleIds) {
5127 Role defaultSiteRole = rolePersistence.fetchByPrimaryKey(
5128 defaultSiteRoleId);
5129
5130 if (defaultSiteRole == null) {
5131 if (_log.isWarnEnabled()) {
5132 _log.warn("Unable to find role " + defaultSiteRoleId);
5133 }
5134
5135 continue;
5136 }
5137
5138 defaultSiteRoles.add(defaultSiteRole);
5139 }
5140
5141 List<Team> defaultTeams = new ArrayList<Team>();
5142
5143 long[] defaultTeamIds = StringUtil.split(
5144 typeSettingsProperties.getProperty("defaultTeamIds"), 0L);
5145
5146 for (long defaultTeamId : defaultTeamIds) {
5147 Team defaultTeam = teamPersistence.findByPrimaryKey(defaultTeamId);
5148
5149 if (defaultTeam == null) {
5150 if (_log.isWarnEnabled()) {
5151 _log.warn("Unable to find team " + defaultTeamId);
5152 }
5153
5154 continue;
5155 }
5156
5157 defaultTeams.add(defaultTeam);
5158 }
5159
5160 for (long userId : userIds) {
5161 Set<Long> userRoleIdsSet = new HashSet<Long>();
5162
5163 for (Role role : defaultSiteRoles) {
5164 if (!userPersistence.containsRole(userId, role.getRoleId())) {
5165 userRoleIdsSet.add(role.getRoleId());
5166 }
5167 }
5168
5169 long[] userRoleIds = ArrayUtil.toArray(
5170 userRoleIdsSet.toArray(new Long[userRoleIdsSet.size()]));
5171
5172 userGroupRoleLocalService.addUserGroupRoles(
5173 userId, groupId, userRoleIds);
5174
5175 Set<Long> userTeamIdsSet = new HashSet<Long>();
5176
5177 for (Team team : defaultTeams) {
5178 if (!userPersistence.containsTeam(userId, team.getTeamId())) {
5179 userTeamIdsSet.add(team.getTeamId());
5180 }
5181 }
5182
5183 long[] userTeamIds = ArrayUtil.toArray(
5184 userTeamIdsSet.toArray(new Long[userTeamIdsSet.size()]));
5185
5186 userPersistence.addTeams(userId, userTeamIds);
5187 }
5188 }
5189
5190
5236 protected int authenticate(
5237 long companyId, String login, String password, String authType,
5238 Map<String, String[]> headerMap, Map<String, String[]> parameterMap,
5239 Map<String, Object> resultsMap)
5240 throws PortalException, SystemException {
5241
5242 if (PropsValues.AUTH_LOGIN_DISABLED) {
5243 return Authenticator.FAILURE;
5244 }
5245
5246 login = StringUtil.toLowerCase(login.trim());
5247
5248 long userId = GetterUtil.getLong(login);
5249
5250
5251
5252 if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
5253 if (Validator.isNull(login)) {
5254 throw new UserEmailAddressException();
5255 }
5256 }
5257 else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
5258 if (Validator.isNull(login)) {
5259 throw new UserScreenNameException();
5260 }
5261 }
5262 else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
5263 if (Validator.isNull(login)) {
5264 throw new UserIdException();
5265 }
5266 }
5267
5268 if (Validator.isNull(password)) {
5269 throw new UserPasswordException(
5270 UserPasswordException.PASSWORD_INVALID);
5271 }
5272
5273 int authResult = Authenticator.FAILURE;
5274
5275
5276
5277 if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
5278 authResult = AuthPipeline.authenticateByEmailAddress(
5279 PropsKeys.AUTH_PIPELINE_PRE, companyId, login, password,
5280 headerMap, parameterMap);
5281 }
5282 else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
5283 authResult = AuthPipeline.authenticateByScreenName(
5284 PropsKeys.AUTH_PIPELINE_PRE, companyId, login, password,
5285 headerMap, parameterMap);
5286 }
5287 else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
5288 authResult = AuthPipeline.authenticateByUserId(
5289 PropsKeys.AUTH_PIPELINE_PRE, companyId, userId, password,
5290 headerMap, parameterMap);
5291 }
5292
5293
5294
5295 User user = null;
5296
5297 if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
5298 user = fetchUserByEmailAddress(companyId, login);
5299 }
5300 else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
5301 user = fetchUserByScreenName(companyId, login);
5302 }
5303 else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
5304 user = userPersistence.fetchByPrimaryKey(GetterUtil.getLong(login));
5305 }
5306
5307 if (user == null) {
5308 return Authenticator.DNE;
5309 }
5310
5311 if (user.isDefaultUser()) {
5312 if (_log.isInfoEnabled()) {
5313 _log.info("Authentication is disabled for the default user");
5314 }
5315
5316 return Authenticator.DNE;
5317 }
5318 else if (!user.isActive()) {
5319 if (_log.isInfoEnabled()) {
5320 _log.info(
5321 "Authentication is disabled for inactive user " +
5322 user.getUserId());
5323 }
5324
5325 return Authenticator.FAILURE;
5326 }
5327
5328 if (!user.isPasswordEncrypted()) {
5329 user.setPassword(PasswordEncryptorUtil.encrypt(user.getPassword()));
5330 user.setPasswordEncrypted(true);
5331
5332 userPersistence.update(user);
5333 }
5334
5335
5336
5337
5338 checkLockout(user);
5339
5340 checkPasswordExpired(user);
5341
5342
5343
5344 boolean skipLiferayCheck = false;
5345
5346 if (authResult == Authenticator.SKIP_LIFERAY_CHECK) {
5347 authResult = Authenticator.SUCCESS;
5348
5349 skipLiferayCheck = true;
5350 }
5351 else if ((authResult == Authenticator.SUCCESS) &&
5352 PropsValues.AUTH_PIPELINE_ENABLE_LIFERAY_CHECK) {
5353
5354 boolean authenticated = PwdAuthenticator.authenticate(
5355 login, password, user.getPassword());
5356
5357 if (authenticated) {
5358 authResult = Authenticator.SUCCESS;
5359 }
5360 else {
5361 authResult = Authenticator.FAILURE;
5362 }
5363 }
5364
5365
5366
5367 if (authResult == Authenticator.SUCCESS) {
5368 if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
5369 authResult = AuthPipeline.authenticateByEmailAddress(
5370 PropsKeys.AUTH_PIPELINE_POST, companyId, login, password,
5371 headerMap, parameterMap);
5372 }
5373 else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
5374 authResult = AuthPipeline.authenticateByScreenName(
5375 PropsKeys.AUTH_PIPELINE_POST, companyId, login, password,
5376 headerMap, parameterMap);
5377 }
5378 else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
5379 authResult = AuthPipeline.authenticateByUserId(
5380 PropsKeys.AUTH_PIPELINE_POST, companyId, userId, password,
5381 headerMap, parameterMap);
5382 }
5383 }
5384
5385 if (authResult == Authenticator.SUCCESS) {
5386 if (resultsMap != null) {
5387 resultsMap.put("userId", user.getUserId());
5388 }
5389
5390
5391
5392 if (skipLiferayCheck ||
5393 !PropsValues.AUTH_PIPELINE_ENABLE_LIFERAY_CHECK ||
5394 Validator.isNull(user.getDigest())) {
5395
5396 String digest = user.getDigest(password);
5397
5398 user.setDigest(digest);
5399
5400 userPersistence.update(user);
5401 }
5402 }
5403
5404
5405
5406 if (authResult == Authenticator.FAILURE) {
5407 try {
5408 if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
5409 AuthPipeline.onFailureByEmailAddress(
5410 PropsKeys.AUTH_FAILURE, companyId, login, headerMap,
5411 parameterMap);
5412 }
5413 else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
5414 AuthPipeline.onFailureByScreenName(
5415 PropsKeys.AUTH_FAILURE, companyId, login, headerMap,
5416 parameterMap);
5417 }
5418 else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
5419 AuthPipeline.onFailureByUserId(
5420 PropsKeys.AUTH_FAILURE, companyId, userId, headerMap,
5421 parameterMap);
5422 }
5423
5424 user = userPersistence.fetchByPrimaryKey(user.getUserId());
5425
5426 if (user == null) {
5427 return Authenticator.DNE;
5428 }
5429
5430
5431
5432 if (!LDAPSettingsUtil.isPasswordPolicyEnabled(
5433 user.getCompanyId())) {
5434
5435 PasswordPolicy passwordPolicy = user.getPasswordPolicy();
5436
5437 user = userPersistence.fetchByPrimaryKey(user.getUserId());
5438
5439 int failedLoginAttempts = user.getFailedLoginAttempts();
5440 int maxFailures = passwordPolicy.getMaxFailure();
5441
5442 if ((failedLoginAttempts >= maxFailures) &&
5443 (maxFailures != 0)) {
5444
5445 if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
5446 AuthPipeline.onMaxFailuresByEmailAddress(
5447 PropsKeys.AUTH_MAX_FAILURES, companyId, login,
5448 headerMap, parameterMap);
5449 }
5450 else if (authType.equals(
5451 CompanyConstants.AUTH_TYPE_SN)) {
5452
5453 AuthPipeline.onMaxFailuresByScreenName(
5454 PropsKeys.AUTH_MAX_FAILURES, companyId, login,
5455 headerMap, parameterMap);
5456 }
5457 else if (authType.equals(
5458 CompanyConstants.AUTH_TYPE_ID)) {
5459
5460 AuthPipeline.onMaxFailuresByUserId(
5461 PropsKeys.AUTH_MAX_FAILURES, companyId, userId,
5462 headerMap, parameterMap);
5463 }
5464 }
5465 }
5466 }
5467 catch (Exception e) {
5468 _log.error(e, e);
5469 }
5470 }
5471
5472
5473
5474 return authResult;
5475 }
5476
5477 protected Date getBirthday(
5478 int birthdayMonth, int birthdayDay, int birthdayYear)
5479 throws PortalException {
5480
5481 Date birthday = PortalUtil.getDate(
5482 birthdayMonth, birthdayDay, birthdayYear,
5483 ContactBirthdayException.class);
5484
5485 Date now = new Date();
5486
5487 if (birthday.after(now)) {
5488 throw new ContactBirthdayException();
5489 }
5490
5491 return birthday;
5492 }
5493
5494 protected String getLogin(String login) {
5495 return StringUtil.lowerCase(StringUtil.trim(login));
5496 }
5497
5498 protected long[] getUserIds(List<User> users) {
5499 long[] userIds = new long[users.size()];
5500
5501 for (int i = 0; i < users.size(); i++) {
5502 User user = users.get(i);
5503
5504 userIds[i] = user.getUserId();
5505 }
5506
5507 return userIds;
5508 }
5509
5510 protected void reindex(final User user) {
5511 final Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
5512 User.class);
5513
5514 Callable<Void> callable = new ShardCallable<Void>(
5515 user.getCompanyId()) {
5516
5517 @Override
5518 protected Void doCall() throws Exception {
5519 indexer.reindex(user);
5520
5521 return null;
5522 }
5523
5524 };
5525
5526 TransactionCommitCallbackRegistryUtil.registerCallback(callable);
5527 }
5528
5529 protected Hits search(
5530 long companyId, String firstName, String middleName,
5531 String lastName, String fullName, String screenName,
5532 String emailAddress, String street, String city, String zip,
5533 String region, String country, int status,
5534 LinkedHashMap<String, Object> params, boolean andSearch, int start,
5535 int end, Sort sort)
5536 throws SystemException {
5537
5538 try {
5539 SearchContext searchContext = new SearchContext();
5540
5541 searchContext.setAndSearch(andSearch);
5542
5543 Map<String, Serializable> attributes =
5544 new HashMap<String, Serializable>();
5545
5546 attributes.put("city", city);
5547 attributes.put("country", country);
5548 attributes.put("emailAddress", emailAddress);
5549 attributes.put("firstName", firstName);
5550 attributes.put("fullName", fullName);
5551 attributes.put("lastName", lastName);
5552 attributes.put("middleName", middleName);
5553 attributes.put("params", params);
5554 attributes.put("region", region);
5555 attributes.put("screenName", screenName);
5556 attributes.put("street", street);
5557 attributes.put("status", status);
5558 attributes.put("zip", zip);
5559
5560 searchContext.setAttributes(attributes);
5561
5562 searchContext.setCompanyId(companyId);
5563 searchContext.setEnd(end);
5564
5565 if (params != null) {
5566 String keywords = (String)params.remove("keywords");
5567
5568 if (Validator.isNotNull(keywords)) {
5569 searchContext.setKeywords(keywords);
5570 }
5571 }
5572
5573 QueryConfig queryConfig = new QueryConfig();
5574
5575 queryConfig.setHighlightEnabled(false);
5576 queryConfig.setScoreEnabled(false);
5577
5578 searchContext.setQueryConfig(queryConfig);
5579
5580 if (sort != null) {
5581 searchContext.setSorts(sort);
5582 }
5583
5584 searchContext.setStart(start);
5585
5586 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
5587 User.class);
5588
5589 return indexer.search(searchContext);
5590 }
5591 catch (Exception e) {
5592 throw new SystemException(e);
5593 }
5594 }
5595
5596 protected void sendEmail(
5597 User user, String password, ServiceContext serviceContext)
5598 throws SystemException {
5599
5600 if (!PrefsPropsUtil.getBoolean(
5601 user.getCompanyId(),
5602 PropsKeys.ADMIN_EMAIL_USER_ADDED_ENABLED)) {
5603
5604 return;
5605 }
5606
5607 String fromName = PrefsPropsUtil.getString(
5608 user.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_NAME);
5609 String fromAddress = PrefsPropsUtil.getString(
5610 user.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
5611
5612 String toName = user.getFullName();
5613 String toAddress = user.getEmailAddress();
5614
5615 String subject = PrefsPropsUtil.getContent(
5616 user.getCompanyId(), PropsKeys.ADMIN_EMAIL_USER_ADDED_SUBJECT);
5617
5618 String body = null;
5619
5620 if (Validator.isNotNull(password)) {
5621 body = PrefsPropsUtil.getContent(
5622 user.getCompanyId(), PropsKeys.ADMIN_EMAIL_USER_ADDED_BODY);
5623 }
5624 else {
5625 body = PrefsPropsUtil.getContent(
5626 user.getCompanyId(),
5627 PropsKeys.ADMIN_EMAIL_USER_ADDED_NO_PASSWORD_BODY);
5628 }
5629
5630 SubscriptionSender subscriptionSender = new SubscriptionSender();
5631
5632 subscriptionSender.setBody(body);
5633 subscriptionSender.setCompanyId(user.getCompanyId());
5634 subscriptionSender.setContextAttributes(
5635 "[$USER_ID$]", user.getUserId(), "[$USER_PASSWORD$]", password,
5636 "[$USER_SCREENNAME$]", user.getScreenName());
5637 subscriptionSender.setFrom(fromAddress, fromName);
5638 subscriptionSender.setHtmlFormat(true);
5639 subscriptionSender.setMailId(
5640 "user", user.getUserId(), System.currentTimeMillis(),
5641 PwdGenerator.getPassword());
5642 subscriptionSender.setServiceContext(serviceContext);
5643 subscriptionSender.setSubject(subject);
5644 subscriptionSender.setUserId(user.getUserId());
5645
5646 subscriptionSender.addRuntimeSubscribers(toAddress, toName);
5647
5648 subscriptionSender.flushNotificationsAsync();
5649 }
5650
5651 protected void setEmailAddress(
5652 User user, String password, String firstName, String middleName,
5653 String lastName, String emailAddress)
5654 throws PortalException, SystemException {
5655
5656 if (StringUtil.equalsIgnoreCase(emailAddress, user.getEmailAddress())) {
5657 return;
5658 }
5659
5660 long userId = user.getUserId();
5661
5662
5663
5664 if (!user.hasCompanyMx() && user.hasCompanyMx(emailAddress) &&
5665 Validator.isNotNull(password)) {
5666
5667 mailService.addUser(
5668 user.getCompanyId(), userId, password, firstName, middleName,
5669 lastName, emailAddress);
5670 }
5671
5672
5673
5674 else if (user.hasCompanyMx() && user.hasCompanyMx(emailAddress)) {
5675 mailService.updateEmailAddress(
5676 user.getCompanyId(), userId, emailAddress);
5677 }
5678
5679
5680
5681 else if (user.hasCompanyMx() && !user.hasCompanyMx(emailAddress)) {
5682 mailService.deleteEmailAddress(user.getCompanyId(), userId);
5683 }
5684
5685 user.setEmailAddress(emailAddress);
5686 user.setDigest(StringPool.BLANK);
5687 }
5688
5689 protected void startWorkflowInstance(
5690 final long companyId, final long workflowUserId, final long userId,
5691 final User user, final ServiceContext workflowServiceContext) {
5692
5693 Callable<Void> callable = new ShardCallable<Void>(companyId) {
5694
5695 @Override
5696 protected Void doCall() throws Exception {
5697 WorkflowHandlerRegistryUtil.startWorkflowInstance(
5698 companyId, workflowUserId, User.class.getName(), userId,
5699 user, workflowServiceContext);
5700
5701 return null;
5702 }
5703
5704 };
5705
5706 TransactionCommitCallbackRegistryUtil.registerCallback(callable);
5707 }
5708
5709 protected void updateGroups(
5710 long userId, long[] newGroupIds, ServiceContext serviceContext,
5711 boolean indexingEnabled)
5712 throws PortalException, SystemException {
5713
5714 if (newGroupIds == null) {
5715 return;
5716 }
5717
5718 List<Group> oldGroups = userPersistence.getGroups(userId);
5719
5720 Set<Long> oldGroupIds = new HashSet<Long>(oldGroups.size());
5721
5722 for (Group oldGroup : oldGroups) {
5723 long oldGroupId = oldGroup.getGroupId();
5724
5725 oldGroupIds.add(oldGroupId);
5726
5727 if (!ArrayUtil.contains(newGroupIds, oldGroupId)) {
5728 unsetGroupUsers(
5729 oldGroupId, new long[] {userId}, serviceContext);
5730 }
5731 }
5732
5733 for (long newGroupId : newGroupIds) {
5734 if (!oldGroupIds.contains(newGroupId)) {
5735 addGroupUsers(newGroupId, new long[] {userId});
5736 }
5737 }
5738
5739 if (indexingEnabled) {
5740 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
5741 User.class);
5742
5743 indexer.reindex(new long[] {userId});
5744 }
5745
5746 PermissionCacheUtil.clearCache();
5747 }
5748
5749 protected void updateOrganizations(
5750 long userId, long[] newOrganizationIds, boolean indexingEnabled)
5751 throws PortalException, SystemException {
5752
5753 if (newOrganizationIds == null) {
5754 return;
5755 }
5756
5757 List<Organization> oldOrganizations = userPersistence.getOrganizations(
5758 userId);
5759
5760 Set<Long> oldOrganizationIds = new HashSet<Long>(
5761 oldOrganizations.size());
5762
5763 for (Organization oldOrganization : oldOrganizations) {
5764 long oldOrganizationId = oldOrganization.getOrganizationId();
5765
5766 oldOrganizationIds.add(oldOrganizationId);
5767
5768 if (!ArrayUtil.contains(newOrganizationIds, oldOrganizationId)) {
5769 unsetOrganizationUsers(oldOrganizationId, new long[] {userId});
5770 }
5771 }
5772
5773 for (long newOrganizationId : newOrganizationIds) {
5774 if (!oldOrganizationIds.contains(newOrganizationId)) {
5775 addOrganizationUsers(newOrganizationId, new long[] {userId});
5776 }
5777 }
5778
5779 if (indexingEnabled) {
5780 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
5781 User.class);
5782
5783 indexer.reindex(new long[] {userId});
5784 }
5785
5786 PermissionCacheUtil.clearCache();
5787 }
5788
5789 protected void updateUserGroupRoles(
5790 User user, long[] groupIds, long[] organizationIds,
5791 List<UserGroupRole> userGroupRoles,
5792 List<UserGroupRole> previousUserGroupRoles)
5793 throws PortalException, SystemException {
5794
5795 if (userGroupRoles == null) {
5796 return;
5797 }
5798
5799 for (UserGroupRole userGroupRole : previousUserGroupRoles) {
5800 if (userGroupRoles.contains(userGroupRole)) {
5801 userGroupRoles.remove(userGroupRole);
5802 }
5803 else {
5804 userGroupRoleLocalService.deleteUserGroupRole(userGroupRole);
5805 }
5806 }
5807
5808 long[] validGroupIds = null;
5809
5810 if (groupIds != null) {
5811 validGroupIds = ArrayUtil.clone(groupIds);
5812 }
5813 else {
5814 validGroupIds = user.getGroupIds();
5815 }
5816
5817 if (organizationIds == null) {
5818 organizationIds = user.getOrganizationIds();
5819 }
5820
5821 long[] organizationGroupIds = new long[organizationIds.length];
5822
5823 for (int i = 0; i < organizationIds.length; i++) {
5824 long organizationId = organizationIds[i];
5825
5826 Organization organization =
5827 organizationPersistence.findByPrimaryKey(organizationId);
5828
5829 Group organizationGroup = organization.getGroup();
5830
5831 organizationGroupIds[i] = organizationGroup.getGroupId();
5832 }
5833
5834 validGroupIds = ArrayUtil.append(validGroupIds, organizationGroupIds);
5835
5836 Arrays.sort(validGroupIds);
5837
5838 for (UserGroupRole userGroupRole : userGroupRoles) {
5839 if (Arrays.binarySearch(
5840 validGroupIds, userGroupRole.getGroupId()) >= 0) {
5841
5842 userGroupRoleLocalService.addUserGroupRole(userGroupRole);
5843 }
5844 }
5845 }
5846
5847 protected void validate(
5848 long companyId, long userId, boolean autoPassword, String password1,
5849 String password2, boolean autoScreenName, String screenName,
5850 String emailAddress, String openId, String firstName,
5851 String middleName, String lastName, long[] organizationIds)
5852 throws PortalException, SystemException {
5853
5854 validateCompanyMaxUsers(companyId);
5855
5856 if (!autoScreenName) {
5857 validateScreenName(companyId, userId, screenName);
5858 }
5859
5860 if (!autoPassword) {
5861 PasswordPolicy passwordPolicy =
5862 passwordPolicyLocalService.getDefaultPasswordPolicy(companyId);
5863
5864 PwdToolkitUtil.validate(
5865 companyId, 0, password1, password2, passwordPolicy);
5866 }
5867
5868 validateEmailAddress(companyId, emailAddress);
5869
5870 if (Validator.isNotNull(emailAddress)) {
5871 User user = userPersistence.fetchByC_EA(companyId, emailAddress);
5872
5873 if ((user != null) && (user.getUserId() != userId)) {
5874 throw new DuplicateUserEmailAddressException();
5875 }
5876 }
5877
5878 validateOpenId(companyId, userId, openId);
5879
5880 validateFullName(companyId, firstName, middleName, lastName);
5881
5882 if (organizationIds != null) {
5883 for (long organizationId : organizationIds) {
5884 Organization organization =
5885 organizationPersistence.fetchByPrimaryKey(organizationId);
5886
5887 if (organization == null) {
5888 throw new NoSuchOrganizationException();
5889 }
5890 }
5891 }
5892 }
5893
5894 protected void validate(
5895 long userId, String screenName, String emailAddress, String openId,
5896 String firstName, String middleName, String lastName, String smsSn)
5897 throws PortalException, SystemException {
5898
5899 User user = userPersistence.findByPrimaryKey(userId);
5900
5901 if (!StringUtil.equalsIgnoreCase(user.getScreenName(), screenName)) {
5902 validateScreenName(user.getCompanyId(), userId, screenName);
5903 }
5904
5905 validateEmailAddress(user.getCompanyId(), emailAddress);
5906
5907 validateOpenId(user.getCompanyId(), userId, openId);
5908
5909 if (!user.isDefaultUser()) {
5910 if (Validator.isNotNull(emailAddress) &&
5911 !StringUtil.equalsIgnoreCase(
5912 user.getEmailAddress(), emailAddress)) {
5913
5914 if (userPersistence.fetchByC_EA(
5915 user.getCompanyId(), emailAddress) != null) {
5916
5917 throw new DuplicateUserEmailAddressException();
5918 }
5919 }
5920
5921 validateFullName(
5922 user.getCompanyId(), firstName, middleName, lastName);
5923 }
5924
5925 if (Validator.isNotNull(smsSn) && !Validator.isEmailAddress(smsSn)) {
5926 throw new UserSmsException();
5927 }
5928 }
5929
5930 protected void validateCompanyMaxUsers(long companyId)
5931 throws PortalException, SystemException {
5932
5933 Company company = companyPersistence.findByPrimaryKey(companyId);
5934
5935 if (company.isSystem() || (company.getMaxUsers() == 0)) {
5936 return;
5937 }
5938
5939 int userCount = searchCount(
5940 companyId, null, WorkflowConstants.STATUS_APPROVED, null);
5941
5942 if (userCount >= company.getMaxUsers()) {
5943 throw new CompanyMaxUsersException();
5944 }
5945 }
5946
5947 protected void validateEmailAddress(long companyId, String emailAddress)
5948 throws PortalException, SystemException {
5949
5950 if (Validator.isNull(emailAddress) &&
5951 !PropsValues.USERS_EMAIL_ADDRESS_REQUIRED) {
5952
5953 return;
5954 }
5955
5956 EmailAddressValidator emailAddressValidator =
5957 EmailAddressValidatorFactory.getInstance();
5958
5959 if (!emailAddressValidator.validate(companyId, emailAddress)) {
5960 throw new UserEmailAddressException();
5961 }
5962
5963 String pop3User = PrefsPropsUtil.getString(
5964 PropsKeys.MAIL_SESSION_MAIL_POP3_USER,
5965 PropsValues.MAIL_SESSION_MAIL_POP3_USER);
5966
5967 if (StringUtil.equalsIgnoreCase(emailAddress, pop3User)) {
5968 throw new ReservedUserEmailAddressException();
5969 }
5970
5971 String[] reservedEmailAddresses = PrefsPropsUtil.getStringArray(
5972 companyId, PropsKeys.ADMIN_RESERVED_EMAIL_ADDRESSES,
5973 StringPool.NEW_LINE, PropsValues.ADMIN_RESERVED_EMAIL_ADDRESSES);
5974
5975 for (String reservedEmailAddress : reservedEmailAddresses) {
5976 if (StringUtil.equalsIgnoreCase(
5977 emailAddress, reservedEmailAddress)) {
5978
5979 throw new ReservedUserEmailAddressException();
5980 }
5981 }
5982 }
5983
5984 protected void validateEmailAddress(
5985 User user, String emailAddress1, String emailAddress2)
5986 throws PortalException, SystemException {
5987
5988 if (!emailAddress1.equals(emailAddress2)) {
5989 throw new UserEmailAddressException();
5990 }
5991
5992 validateEmailAddress(user.getCompanyId(), emailAddress1);
5993 validateEmailAddress(user.getCompanyId(), emailAddress2);
5994
5995 if (!StringUtil.equalsIgnoreCase(
5996 emailAddress1, user.getEmailAddress())) {
5997
5998 if (userPersistence.fetchByC_EA(
5999 user.getCompanyId(), emailAddress1) != null) {
6000
6001 throw new DuplicateUserEmailAddressException();
6002 }
6003 }
6004 }
6005
6006 protected void validateFullName(
6007 long companyId, String firstName, String middleName,
6008 String lastName)
6009 throws PortalException, SystemException {
6010
6011 if (Validator.isNull(firstName)) {
6012 throw new ContactFirstNameException();
6013 }
6014 else if (Validator.isNull(lastName) &&
6015 PrefsPropsUtil.getBoolean(
6016 companyId, PropsKeys.USERS_LAST_NAME_REQUIRED,
6017 PropsValues.USERS_LAST_NAME_REQUIRED)) {
6018
6019 throw new ContactLastNameException();
6020 }
6021
6022 FullNameValidator fullNameValidator =
6023 FullNameValidatorFactory.getInstance();
6024
6025 if (!fullNameValidator.validate(
6026 companyId, firstName, middleName, lastName)) {
6027
6028 throw new ContactFullNameException();
6029 }
6030 }
6031
6032 protected void validateOpenId(long companyId, long userId, String openId)
6033 throws PortalException, SystemException {
6034
6035 if (Validator.isNull(openId)) {
6036 return;
6037 }
6038
6039 User user = userPersistence.fetchByC_O(companyId, openId);
6040
6041 if ((user != null) && (user.getUserId() != userId)) {
6042 throw new DuplicateOpenIdException();
6043 }
6044 }
6045
6046 protected void validatePassword(
6047 long companyId, long userId, String password1, String password2)
6048 throws PortalException, SystemException {
6049
6050 if (Validator.isNull(password1) || Validator.isNull(password2)) {
6051 throw new UserPasswordException(
6052 UserPasswordException.PASSWORD_INVALID);
6053 }
6054
6055 if (!password1.equals(password2)) {
6056 throw new UserPasswordException(
6057 UserPasswordException.PASSWORDS_DO_NOT_MATCH);
6058 }
6059
6060 PasswordPolicy passwordPolicy =
6061 passwordPolicyLocalService.getPasswordPolicyByUserId(userId);
6062
6063 PwdToolkitUtil.validate(
6064 companyId, userId, password1, password2, passwordPolicy);
6065 }
6066
6067 protected void validateReminderQuery(String question, String answer)
6068 throws PortalException {
6069
6070 if (!PropsValues.USERS_REMINDER_QUERIES_ENABLED) {
6071 return;
6072 }
6073
6074 if (Validator.isNull(question)) {
6075 throw new UserReminderQueryException("Question is null");
6076 }
6077
6078 if (Validator.isNull(answer)) {
6079 throw new UserReminderQueryException("Answer is null");
6080 }
6081 }
6082
6083 protected void validateScreenName(
6084 long companyId, long userId, String screenName)
6085 throws PortalException, SystemException {
6086
6087 if (Validator.isNull(screenName)) {
6088 throw new UserScreenNameException();
6089 }
6090
6091 ScreenNameValidator screenNameValidator =
6092 ScreenNameValidatorFactory.getInstance();
6093
6094 if (!screenNameValidator.validate(companyId, screenName)) {
6095 throw new UserScreenNameException();
6096 }
6097
6098 if (Validator.isNumber(screenName)) {
6099 if (!PropsValues.USERS_SCREEN_NAME_ALLOW_NUMERIC) {
6100 throw new UserScreenNameException();
6101 }
6102
6103 if (!screenName.equals(String.valueOf(userId))) {
6104 Group group = groupPersistence.fetchByPrimaryKey(
6105 GetterUtil.getLong(screenName));
6106
6107 if (group != null) {
6108 throw new UserScreenNameException();
6109 }
6110 }
6111 }
6112
6113 for (char c : screenName.toCharArray()) {
6114 if (!Validator.isChar(c) && !Validator.isDigit(c) &&
6115 (c != CharPool.DASH) && (c != CharPool.PERIOD) &&
6116 (c != CharPool.UNDERLINE)) {
6117
6118 throw new UserScreenNameException();
6119 }
6120 }
6121
6122 String[] anonymousNames = BaseServiceImpl.ANONYMOUS_NAMES;
6123
6124 for (String anonymousName : anonymousNames) {
6125 if (StringUtil.equalsIgnoreCase(screenName, anonymousName)) {
6126 throw new UserScreenNameException();
6127 }
6128 }
6129
6130 User user = userPersistence.fetchByC_SN(companyId, screenName);
6131
6132 if ((user != null) && (user.getUserId() != userId)) {
6133 throw new DuplicateUserScreenNameException();
6134 }
6135
6136 String friendlyURL = StringPool.SLASH + screenName;
6137
6138 Group group = groupPersistence.fetchByC_F(companyId, friendlyURL);
6139
6140 if ((group != null) && (group.getClassPK() != userId)) {
6141 throw new GroupFriendlyURLException(
6142 GroupFriendlyURLException.DUPLICATE);
6143 }
6144
6145 int exceptionType = LayoutImpl.validateFriendlyURL(friendlyURL);
6146
6147 if (exceptionType != -1) {
6148 throw new UserScreenNameException(
6149 new GroupFriendlyURLException(exceptionType));
6150 }
6151
6152 String[] reservedScreenNames = PrefsPropsUtil.getStringArray(
6153 companyId, PropsKeys.ADMIN_RESERVED_SCREEN_NAMES,
6154 StringPool.NEW_LINE, PropsValues.ADMIN_RESERVED_SCREEN_NAMES);
6155
6156 for (String reservedScreenName : reservedScreenNames) {
6157 if (StringUtil.equalsIgnoreCase(screenName, reservedScreenName)) {
6158 throw new ReservedUserScreenNameException();
6159 }
6160 }
6161 }
6162
6163 private static Log _log = LogFactoryUtil.getLog(UserLocalServiceImpl.class);
6164
6165 private Map<Long, User> _defaultUsers = new ConcurrentHashMap<Long, User>();
6166
6167 }