--- /dev/null
+/**\r
+ * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.\r
+ *\r
+ * This library is free software; you can redistribute it and/or modify it under\r
+ * the terms of the GNU Lesser General Public License as published by the Free\r
+ * Software Foundation; either version 2.1 of the License, or (at your option)\r
+ * any later version.\r
+ *\r
+ * This library is distributed in the hope that it will be useful, but WITHOUT\r
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more\r
+ * details.\r
+ */\r
+\r
+package com.liferay.portal.service.impl;\r
+\r
+import com.liferay.portal.CompanyMaxUsersException;\r
+import com.liferay.portal.ContactBirthdayException;\r
+import com.liferay.portal.ContactFirstNameException;\r
+import com.liferay.portal.ContactFullNameException;\r
+import com.liferay.portal.ContactLastNameException;\r
+import com.liferay.portal.DuplicateUserEmailAddressException;\r
+import com.liferay.portal.DuplicateUserScreenNameException;\r
+import com.liferay.portal.GroupFriendlyURLException;\r
+import com.liferay.portal.ModelListenerException;\r
+import com.liferay.portal.NoSuchContactException;\r
+import com.liferay.portal.NoSuchGroupException;\r
+import com.liferay.portal.NoSuchOrganizationException;\r
+import com.liferay.portal.NoSuchRoleException;\r
+import com.liferay.portal.NoSuchTicketException;\r
+import com.liferay.portal.NoSuchUserException;\r
+import com.liferay.portal.NoSuchUserGroupException;\r
+import com.liferay.portal.PasswordExpiredException;\r
+import com.liferay.portal.RequiredUserException;\r
+import com.liferay.portal.ReservedUserEmailAddressException;\r
+import com.liferay.portal.ReservedUserScreenNameException;\r
+import com.liferay.portal.UserEmailAddressException;\r
+import com.liferay.portal.UserIdException;\r
+import com.liferay.portal.UserLockoutException;\r
+import com.liferay.portal.UserPasswordException;\r
+import com.liferay.portal.UserPortraitSizeException;\r
+import com.liferay.portal.UserPortraitTypeException;\r
+import com.liferay.portal.UserReminderQueryException;\r
+import com.liferay.portal.UserScreenNameException;\r
+import com.liferay.portal.UserSmsException;\r
+import com.liferay.portal.kernel.exception.PortalException;\r
+import com.liferay.portal.kernel.exception.SystemException;\r
+import com.liferay.portal.kernel.image.ImageBag;\r
+import com.liferay.portal.kernel.image.ImageToolUtil;\r
+import com.liferay.portal.kernel.language.LanguageUtil;\r
+import com.liferay.portal.kernel.log.Log;\r
+import com.liferay.portal.kernel.log.LogFactoryUtil;\r
+import com.liferay.portal.kernel.search.Hits;\r
+import com.liferay.portal.kernel.search.Indexer;\r
+import com.liferay.portal.kernel.search.IndexerRegistryUtil;\r
+import com.liferay.portal.kernel.search.QueryConfig;\r
+import com.liferay.portal.kernel.search.SearchContext;\r
+import com.liferay.portal.kernel.search.Sort;\r
+import com.liferay.portal.kernel.spring.aop.Skip;\r
+import com.liferay.portal.kernel.transaction.Propagation;\r
+import com.liferay.portal.kernel.transaction.Transactional;\r
+import com.liferay.portal.kernel.util.ArrayUtil;\r
+import com.liferay.portal.kernel.util.CharPool;\r
+import com.liferay.portal.kernel.util.Digester;\r
+import com.liferay.portal.kernel.util.DigesterUtil;\r
+import com.liferay.portal.kernel.util.GetterUtil;\r
+import com.liferay.portal.kernel.util.KeyValuePair;\r
+import com.liferay.portal.kernel.util.OrderByComparator;\r
+import com.liferay.portal.kernel.util.ParamUtil;\r
+import com.liferay.portal.kernel.util.PropsKeys;\r
+import com.liferay.portal.kernel.util.StringPool;\r
+import com.liferay.portal.kernel.util.StringUtil;\r
+import com.liferay.portal.kernel.util.Validator;\r
+import com.liferay.portal.kernel.workflow.WorkflowConstants;\r
+import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;\r
+import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;\r
+import com.liferay.portal.model.Account;\r
+import com.liferay.portal.model.Company;\r
+import com.liferay.portal.model.CompanyConstants;\r
+import com.liferay.portal.model.Contact;\r
+import com.liferay.portal.model.ContactConstants;\r
+import com.liferay.portal.model.Group;\r
+import com.liferay.portal.model.GroupConstants;\r
+import com.liferay.portal.model.Layout;\r
+import com.liferay.portal.model.Organization;\r
+import com.liferay.portal.model.PasswordPolicy;\r
+import com.liferay.portal.model.ResourceConstants;\r
+import com.liferay.portal.model.Role;\r
+import com.liferay.portal.model.RoleConstants;\r
+import com.liferay.portal.model.Team;\r
+import com.liferay.portal.model.Ticket;\r
+import com.liferay.portal.model.TicketConstants;\r
+import com.liferay.portal.model.User;\r
+import com.liferay.portal.model.UserGroup;\r
+import com.liferay.portal.model.UserGroupRole;\r
+import com.liferay.portal.model.impl.LayoutImpl;\r
+import com.liferay.portal.security.auth.AuthPipeline;\r
+import com.liferay.portal.security.auth.Authenticator;\r
+import com.liferay.portal.security.auth.EmailAddressGenerator;\r
+import com.liferay.portal.security.auth.EmailAddressGeneratorFactory;\r
+import com.liferay.portal.security.auth.EmailAddressValidator;\r
+import com.liferay.portal.security.auth.EmailAddressValidatorFactory;\r
+import com.liferay.portal.security.auth.FullNameGenerator;\r
+import com.liferay.portal.security.auth.FullNameGeneratorFactory;\r
+import com.liferay.portal.security.auth.FullNameValidator;\r
+import com.liferay.portal.security.auth.FullNameValidatorFactory;\r
+import com.liferay.portal.security.auth.PrincipalException;\r
+import com.liferay.portal.security.auth.ScreenNameGenerator;\r
+import com.liferay.portal.security.auth.ScreenNameGeneratorFactory;\r
+import com.liferay.portal.security.auth.ScreenNameValidator;\r
+import com.liferay.portal.security.auth.ScreenNameValidatorFactory;\r
+import com.liferay.portal.security.ldap.LDAPSettingsUtil;\r
+import com.liferay.portal.security.permission.PermissionCacheUtil;\r
+import com.liferay.portal.security.pwd.PwdAuthenticator;\r
+import com.liferay.portal.security.pwd.PwdEncryptor;\r
+import com.liferay.portal.security.pwd.PwdToolkitUtil;\r
+import com.liferay.portal.service.BaseServiceImpl;\r
+import com.liferay.portal.service.ExtUserLocalService;\r
+import com.liferay.portal.service.ServiceContext;\r
+import com.liferay.portal.service.base.UserLocalServiceBaseImpl;\r
+import com.liferay.portal.spring.transaction.TransactionCommitCallbackUtil;\r
+import com.liferay.portal.util.PortalUtil;\r
+import com.liferay.portal.util.PrefsPropsUtil;\r
+import com.liferay.portal.util.PropsValues;\r
+import com.liferay.portal.util.SubscriptionSender;\r
+import com.liferay.portlet.documentlibrary.ImageSizeException;\r
+import com.liferay.portlet.messageboards.model.MBMessage;\r
+import com.liferay.portlet.usersadmin.util.UsersAdminUtil;\r
+import com.liferay.util.Encryptor;\r
+import com.liferay.util.EncryptorException;\r
+\r
+import java.awt.image.RenderedImage;\r
+\r
+import java.io.IOException;\r
+import java.io.Serializable;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.Calendar;\r
+import java.util.Date;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.LinkedHashMap;\r
+import java.util.List;\r
+import java.util.Locale;\r
+import java.util.Map;\r
+import java.util.Set;\r
+import java.util.concurrent.Callable;\r
+import java.util.concurrent.ConcurrentHashMap;\r
+\r
+/**\r
+ * The implementation of the user local service.\r
+ *\r
+ * @author Brian Wing Shun Chan\r
+ * @author Scott Lee\r
+ * @author Raymond Augé\r
+ * @author Jorge Ferrer\r
+ * @author Julio Camarero\r
+ * @author Wesley Gong\r
+ * @author Zsigmond Rab\r
+ */\r
+public class ExtUserLocalServiceImpl extends UserLocalServiceBaseImpl implements ExtUserLocalService {\r
+\r
+ public User addDefaultAdminUser(\r
+ long companyId, String screenName, String emailAddress,\r
+ Locale locale, String firstName, String middleName, String lastName)\r
+ throws PortalException, SystemException {\r
+\r
+ long creatorUserId = 0;\r
+ boolean autoPassword = false;\r
+ String password1 = PropsValues.DEFAULT_ADMIN_PASSWORD;\r
+ String password2 = password1;\r
+ boolean autoScreenName = false;\r
+\r
+ screenName = getScreenName(screenName);\r
+\r
+ for (int i = 1;; i++) {\r
+ User screenNameUser = userPersistence.fetchByC_SN(\r
+ companyId, screenName);\r
+\r
+ if (screenNameUser == null) {\r
+ break;\r
+ }\r
+\r
+ screenName = screenName + i;\r
+ }\r
+\r
+ long facebookId = 0;\r
+ String openId = StringPool.BLANK;\r
+ int prefixId = 0;\r
+ int suffixId = 0;\r
+ boolean male = true;\r
+ int birthdayMonth = Calendar.JANUARY;\r
+ int birthdayDay = 1;\r
+ int birthdayYear = 1970;\r
+ String jobTitle = StringPool.BLANK;\r
+\r
+ Group guestGroup = groupLocalService.getGroup(\r
+ companyId, GroupConstants.GUEST);\r
+\r
+ long[] groupIds = {guestGroup.getGroupId()};\r
+\r
+ long[] organizationIds = null;\r
+\r
+ Role adminRole = roleLocalService.getRole(\r
+ companyId, RoleConstants.ADMINISTRATOR);\r
+\r
+ Role powerUserRole = roleLocalService.getRole(\r
+ companyId, RoleConstants.POWER_USER);\r
+\r
+ long[] roleIds = {adminRole.getRoleId(), powerUserRole.getRoleId()};\r
+\r
+ long[] userGroupIds = null;\r
+ boolean sendEmail = false;\r
+ ServiceContext serviceContext = new ServiceContext();\r
+\r
+ User defaultAdminUser = addUser(\r
+ creatorUserId, companyId, autoPassword, password1, password2,\r
+ autoScreenName, screenName, emailAddress, facebookId, openId,\r
+ locale, firstName, middleName, lastName, prefixId, suffixId, male,\r
+ birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,\r
+ organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);\r
+\r
+ updateEmailAddressVerified(defaultAdminUser.getUserId(), true);\r
+\r
+ updateLastLogin(\r
+ defaultAdminUser.getUserId(), defaultAdminUser.getLoginIP());\r
+\r
+ updatePasswordReset(defaultAdminUser.getUserId(), false);\r
+\r
+ return defaultAdminUser;\r
+ }\r
+\r
+ /**\r
+ * Adds the user to the default groups, unless the user is already in these\r
+ * groups. The default groups can be specified in\r
+ * <code>portal.properties</code> with the key\r
+ * <code>admin.default.group.names</code>.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void addDefaultGroups(long userId)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ Set<Long> groupIdsSet = new HashSet<Long>();\r
+\r
+ String[] defaultGroupNames = PrefsPropsUtil.getStringArray(\r
+ user.getCompanyId(), PropsKeys.ADMIN_DEFAULT_GROUP_NAMES,\r
+ StringPool.NEW_LINE, PropsValues.ADMIN_DEFAULT_GROUP_NAMES);\r
+\r
+ for (String defaultGroupName : defaultGroupNames) {\r
+ Company company = companyPersistence.findByPrimaryKey(\r
+ user.getCompanyId());\r
+\r
+ Account account = company.getAccount();\r
+\r
+ if (defaultGroupName.equalsIgnoreCase(account.getName())) {\r
+ defaultGroupName = GroupConstants.GUEST;\r
+ }\r
+\r
+ try {\r
+ Group group = groupPersistence.findByC_N(\r
+ user.getCompanyId(), defaultGroupName);\r
+\r
+ if (!userPersistence.containsGroup(\r
+ userId, group.getGroupId())) {\r
+\r
+ groupIdsSet.add(group.getGroupId());\r
+ }\r
+ }\r
+ catch (NoSuchGroupException nsge) {\r
+ }\r
+ }\r
+\r
+ long[] groupIds = ArrayUtil.toArray(\r
+ groupIdsSet.toArray(new Long[groupIdsSet.size()]));\r
+\r
+ groupLocalService.addUserGroups(userId, groupIds);\r
+ }\r
+\r
+ /**\r
+ * Adds the user to the default roles, unless the user already has these\r
+ * roles. The default roles can be specified in\r
+ * <code>portal.properties</code> with the key\r
+ * <code>admin.default.role.names</code>.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void addDefaultRoles(long userId)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ Set<Long> roleIdSet = new HashSet<Long>();\r
+\r
+ String[] defaultRoleNames = PrefsPropsUtil.getStringArray(\r
+ user.getCompanyId(), PropsKeys.ADMIN_DEFAULT_ROLE_NAMES,\r
+ StringPool.NEW_LINE, PropsValues.ADMIN_DEFAULT_ROLE_NAMES);\r
+\r
+ for (String defaultRoleName : defaultRoleNames) {\r
+ try {\r
+ Role role = rolePersistence.findByC_N(\r
+ user.getCompanyId(), defaultRoleName);\r
+\r
+ if (!userPersistence.containsRole(userId, role.getRoleId())) {\r
+ roleIdSet.add(role.getRoleId());\r
+ }\r
+ }\r
+ catch (NoSuchRoleException nsre) {\r
+ }\r
+ }\r
+\r
+ long[] roleIds = ArrayUtil.toArray(\r
+ roleIdSet.toArray(new Long[roleIdSet.size()]));\r
+\r
+ roleIds = UsersAdminUtil.addRequiredRoles(user, roleIds);\r
+\r
+ userPersistence.addRoles(userId, roleIds);\r
+ }\r
+\r
+ /**\r
+ * Adds the user to the default user groups, unless the user is already in\r
+ * these user groups. The default user groups can be specified in\r
+ * <code>portal.properties</code> with the property\r
+ * <code>admin.default.user.group.names</code>.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ @SuppressWarnings("deprecation")\r
+ public void addDefaultUserGroups(long userId)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ Set<Long> userGroupIdSet = new HashSet<Long>();\r
+\r
+ String[] defaultUserGroupNames = PrefsPropsUtil.getStringArray(\r
+ user.getCompanyId(), PropsKeys.ADMIN_DEFAULT_USER_GROUP_NAMES,\r
+ StringPool.NEW_LINE, PropsValues.ADMIN_DEFAULT_USER_GROUP_NAMES);\r
+\r
+ for (String defaultUserGroupName : defaultUserGroupNames) {\r
+ try {\r
+ UserGroup userGroup = userGroupPersistence.findByC_N(\r
+ user.getCompanyId(), defaultUserGroupName);\r
+\r
+ if (!userPersistence.containsUserGroup(\r
+ userId, userGroup.getUserGroupId())) {\r
+\r
+ userGroupIdSet.add(userGroup.getUserGroupId());\r
+ }\r
+ }\r
+ catch (NoSuchUserGroupException nsuge) {\r
+ }\r
+ }\r
+\r
+ long[] userGroupIds = ArrayUtil.toArray(\r
+ userGroupIdSet.toArray(new Long[userGroupIdSet.size()]));\r
+\r
+ if (PropsValues.USER_GROUPS_COPY_LAYOUTS_TO_USER_PERSONAL_SITE) {\r
+ for (long userGroupId : userGroupIds) {\r
+ userGroupLocalService.copyUserGroupLayouts(userGroupId, userId);\r
+ }\r
+ }\r
+\r
+ userPersistence.addUserGroups(userId, userGroupIds);\r
+ }\r
+\r
+ /**\r
+ * Adds the users to the group.\r
+ *\r
+ * @param groupId the primary key of the group\r
+ * @param userIds the primary keys of the users\r
+ * @throws PortalException if a group or user with the primary key could not\r
+ * be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void addGroupUsers(long groupId, long[] userIds)\r
+ throws PortalException, SystemException {\r
+\r
+ groupPersistence.addUsers(groupId, userIds);\r
+\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);\r
+\r
+ indexer.reindex(userIds);\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ /**\r
+ * Adds the users to the organization.\r
+ *\r
+ * @param organizationId the primary key of the organization\r
+ * @param userIds the primary keys of the users\r
+ * @throws PortalException if an organization or user with the primary key\r
+ * could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void addOrganizationUsers(long organizationId, long[] userIds)\r
+ throws PortalException, SystemException {\r
+\r
+ organizationPersistence.addUsers(organizationId, userIds);\r
+\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);\r
+\r
+ indexer.reindex(userIds);\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ /**\r
+ * Assigns the password policy to the users, removing any other currently\r
+ * assigned password policies.\r
+ *\r
+ * @param passwordPolicyId the primary key of the password policy\r
+ * @param userIds the primary keys of the users\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void addPasswordPolicyUsers(long passwordPolicyId, long[] userIds)\r
+ throws SystemException {\r
+\r
+ passwordPolicyRelLocalService.addPasswordPolicyRels(\r
+ passwordPolicyId, User.class.getName(), userIds);\r
+ }\r
+\r
+ /**\r
+ * Adds the users to the role.\r
+ *\r
+ * @param roleId the primary key of the role\r
+ * @param userIds the primary keys of the users\r
+ * @throws PortalException if a role or user with the primary key could not\r
+ * be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void addRoleUsers(long roleId, long[] userIds)\r
+ throws PortalException, SystemException {\r
+\r
+ rolePersistence.addUsers(roleId, userIds);\r
+\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);\r
+\r
+ indexer.reindex(userIds);\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ /**\r
+ * Adds the users to the team.\r
+ *\r
+ * @param teamId the primary key of the team\r
+ * @param userIds the primary keys of the users\r
+ * @throws PortalException if a team or user with the primary key could not\r
+ * be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void addTeamUsers(long teamId, long[] userIds)\r
+ throws PortalException, SystemException {\r
+\r
+ teamPersistence.addUsers(teamId, userIds);\r
+\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);\r
+\r
+ indexer.reindex(userIds);\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ /**\r
+ * Adds a user.\r
+ *\r
+ * <p>\r
+ * This method handles the creation and bookkeeping of the user including\r
+ * its resources, metadata, and internal data structures. It is not\r
+ * necessary to make subsequent calls to any methods to setup default\r
+ * groups, resources, etc.\r
+ * </p>\r
+ *\r
+ * @param creatorUserId the primary key of the creator\r
+ * @param companyId the primary key of the user's company\r
+ * @param autoPassword whether a password should be automatically generated\r
+ * for the user\r
+ * @param password1 the user's password\r
+ * @param password2 the user's password confirmation\r
+ * @param autoScreenName whether a screen name should be automatically\r
+ * generated for the user\r
+ * @param screenName the user's screen name\r
+ * @param emailAddress the user's email address\r
+ * @param facebookId the user's facebook ID\r
+ * @param openId the user's OpenID\r
+ * @param locale the user's locale\r
+ * @param firstName the user's first name\r
+ * @param middleName the user's middle name\r
+ * @param lastName the user's last name\r
+ * @param prefixId the user's name prefix ID\r
+ * @param suffixId the user's name suffix ID\r
+ * @param male whether the user is male\r
+ * @param birthdayMonth the user's birthday month (0-based, meaning 0 for\r
+ * January)\r
+ * @param birthdayDay the user's birthday day\r
+ * @param birthdayYear the user's birthday year\r
+ * @param jobTitle the user's job title\r
+ * @param groupIds the primary keys of the user's groups\r
+ * @param organizationIds the primary keys of the user's organizations\r
+ * @param roleIds the primary keys of the roles this user possesses\r
+ * @param userGroupIds the primary keys of the user's user groups\r
+ * @param sendEmail whether to send the user an email notification about\r
+ * their new account\r
+ * @param serviceContext the user's service context (optionally\r
+ * <code>null</code>). Can set the universally unique identifier\r
+ * (with the <code>uuid</code> attribute), asset category IDs, asset\r
+ * tag names, and expando bridge attributes for the user.\r
+ * @return the new user\r
+ * @throws PortalException if the user's information was invalid\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User addUser(\r
+ long creatorUserId, long companyId, boolean autoPassword,\r
+ String password1, String password2, boolean autoScreenName,\r
+ String screenName, String emailAddress, long facebookId,\r
+ String openId, Locale locale, String firstName, String middleName,\r
+ String lastName, int prefixId, int suffixId, boolean male,\r
+ int birthdayMonth, int birthdayDay, int birthdayYear,\r
+ String jobTitle, long[] groupIds, long[] organizationIds,\r
+ long[] roleIds, long[] userGroupIds, boolean sendEmail,\r
+ ServiceContext serviceContext)\r
+ throws PortalException, SystemException {\r
+\r
+ boolean workflowEnabled = WorkflowThreadLocal.isEnabled();\r
+\r
+ try {\r
+ WorkflowThreadLocal.setEnabled(false);\r
+\r
+ return addUserWithWorkflow(\r
+ creatorUserId, companyId, autoPassword, password1, password2,\r
+ autoScreenName, screenName, emailAddress, facebookId, openId,\r
+ locale, firstName, middleName, lastName, prefixId, suffixId,\r
+ male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,\r
+ groupIds, organizationIds, roleIds, userGroupIds, sendEmail,\r
+ serviceContext);\r
+ }\r
+ finally {\r
+ WorkflowThreadLocal.setEnabled(workflowEnabled);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Adds the users to the user group.\r
+ *\r
+ * @param userGroupId the primary key of the user group\r
+ * @param userIds the primary keys of the users\r
+ * @throws PortalException if a user group or user with the primary could\r
+ * could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ @SuppressWarnings("deprecation")\r
+ public void addUserGroupUsers(long userGroupId, long[] userIds)\r
+ throws PortalException, SystemException {\r
+\r
+ if (PropsValues.USER_GROUPS_COPY_LAYOUTS_TO_USER_PERSONAL_SITE) {\r
+ userGroupLocalService.copyUserGroupLayouts(userGroupId, userIds);\r
+ }\r
+\r
+ userGroupPersistence.addUsers(userGroupId, userIds);\r
+\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);\r
+\r
+ indexer.reindex(userIds);\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ /**\r
+ * Adds a user with workflow.\r
+ *\r
+ * <p>\r
+ * This method handles the creation and bookkeeping of the user including\r
+ * its resources, metadata, and internal data structures. It is not\r
+ * necessary to make subsequent calls to any methods to setup default\r
+ * groups, resources, etc.\r
+ * </p>\r
+ *\r
+ * @param creatorUserId the primary key of the creator\r
+ * @param companyId the primary key of the user's company\r
+ * @param autoPassword whether a password should be automatically generated\r
+ * for the user\r
+ * @param password1 the user's password\r
+ * @param password2 the user's password confirmation\r
+ * @param autoScreenName whether a screen name should be automatically\r
+ * generated for the user\r
+ * @param screenName the user's screen name\r
+ * @param emailAddress the user's email address\r
+ * @param facebookId the user's facebook ID\r
+ * @param openId the user's OpenID\r
+ * @param locale the user's locale\r
+ * @param firstName the user's first name\r
+ * @param middleName the user's middle name\r
+ * @param lastName the user's last name\r
+ * @param prefixId the user's name prefix ID\r
+ * @param suffixId the user's name suffix ID\r
+ * @param male whether the user is male\r
+ * @param birthdayMonth the user's birthday month (0-based, meaning 0 for\r
+ * January)\r
+ * @param birthdayDay the user's birthday day\r
+ * @param birthdayYear the user's birthday year\r
+ * @param jobTitle the user's job title\r
+ * @param groupIds the primary keys of the user's groups\r
+ * @param organizationIds the primary keys of the user's organizations\r
+ * @param roleIds the primary keys of the roles this user possesses\r
+ * @param userGroupIds the primary keys of the user's user groups\r
+ * @param sendEmail whether to send the user an email notification about\r
+ * their new account\r
+ * @param serviceContext the user's service context (optionally\r
+ * <code>null</code>). Can set the universally unique identifier\r
+ * (with the <code>uuid</code> attribute), asset category IDs, asset\r
+ * tag names, and expando bridge attributes for the user.\r
+ * @return the new user\r
+ * @throws PortalException if the user's information was invalid\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ @SuppressWarnings("deprecation")\r
+ public User addUserWithWorkflow(\r
+ long creatorUserId, long companyId, boolean autoPassword,\r
+ String password1, String password2, boolean autoScreenName,\r
+ String screenName, String emailAddress, long facebookId,\r
+ String openId, Locale locale, String firstName, String middleName,\r
+ String lastName, int prefixId, int suffixId, boolean male,\r
+ int birthdayMonth, int birthdayDay, int birthdayYear,\r
+ String jobTitle, long[] groupIds, long[] organizationIds,\r
+ long[] roleIds, long[] userGroupIds, boolean sendEmail,\r
+ ServiceContext serviceContext)\r
+ throws PortalException, SystemException {\r
+\r
+ // User\r
+\r
+ Company company = companyPersistence.findByPrimaryKey(companyId);\r
+ screenName = getScreenName(screenName);\r
+ emailAddress = emailAddress.trim().toLowerCase();\r
+ openId = openId.trim();\r
+ Date now = new Date();\r
+\r
+ if (PrefsPropsUtil.getBoolean(\r
+ companyId, PropsKeys.USERS_SCREEN_NAME_ALWAYS_AUTOGENERATE)) {\r
+\r
+ autoScreenName = true;\r
+ }\r
+\r
+ long userId = counterLocalService.increment();\r
+\r
+ EmailAddressGenerator emailAddressGenerator =\r
+ EmailAddressGeneratorFactory.getInstance();\r
+\r
+ if (emailAddressGenerator.isGenerated(emailAddress)) {\r
+ emailAddress = StringPool.BLANK;\r
+ }\r
+\r
+ if (!PropsValues.USERS_EMAIL_ADDRESS_REQUIRED &&\r
+ Validator.isNull(emailAddress)) {\r
+\r
+ emailAddress = emailAddressGenerator.generate(companyId, userId);\r
+ }\r
+\r
+ validate(\r
+ companyId, userId, autoPassword, password1, password2,\r
+ autoScreenName, screenName, emailAddress, firstName, middleName,\r
+ lastName, organizationIds);\r
+\r
+ if (!autoPassword) {\r
+ if (Validator.isNull(password1) || Validator.isNull(password2)) {\r
+ throw new UserPasswordException(\r
+ UserPasswordException.PASSWORD_INVALID);\r
+ }\r
+ }\r
+\r
+ if (autoScreenName) {\r
+ ScreenNameGenerator screenNameGenerator =\r
+ ScreenNameGeneratorFactory.getInstance();\r
+\r
+ try {\r
+ screenName = screenNameGenerator.generate(\r
+ companyId, userId, emailAddress);\r
+ }\r
+ catch (Exception e) {\r
+ throw new SystemException(e);\r
+ }\r
+ }\r
+\r
+ User defaultUser = getDefaultUser(companyId);\r
+\r
+ FullNameGenerator fullNameGenerator =\r
+ FullNameGeneratorFactory.getInstance();\r
+\r
+ String fullName = fullNameGenerator.getFullName(\r
+ firstName, middleName, lastName);\r
+\r
+ String greeting = LanguageUtil.format(\r
+ locale, "welcome-x", " " + fullName, false);\r
+\r
+ User user = userPersistence.create(userId);\r
+\r
+ if (serviceContext != null) {\r
+ String uuid = serviceContext.getUuid();\r
+\r
+ if (Validator.isNotNull(uuid)) {\r
+ user.setUuid(uuid);\r
+ }\r
+ }\r
+\r
+ user.setCompanyId(companyId);\r
+ user.setCreateDate(now);\r
+ user.setModifiedDate(now);\r
+ user.setDefaultUser(false);\r
+ user.setContactId(counterLocalService.increment());\r
+\r
+ if (Validator.isNotNull(password1)) {\r
+ user.setPassword(PwdEncryptor.encrypt(password1));\r
+ user.setPasswordUnencrypted(password1);\r
+ }\r
+\r
+ user.setPasswordEncrypted(true);\r
+\r
+ PasswordPolicy passwordPolicy = defaultUser.getPasswordPolicy();\r
+\r
+ if ((passwordPolicy != null) && passwordPolicy.isChangeable() &&\r
+ passwordPolicy.isChangeRequired()) {\r
+\r
+ user.setPasswordReset(true);\r
+ }\r
+ else {\r
+ user.setPasswordReset(false);\r
+ }\r
+\r
+ user.setDigest(StringPool.BLANK);\r
+ user.setScreenName(screenName);\r
+ user.setEmailAddress(emailAddress);\r
+ user.setFacebookId(facebookId);\r
+ user.setOpenId(openId);\r
+ user.setLanguageId(locale.toString());\r
+ user.setTimeZoneId(defaultUser.getTimeZoneId());\r
+ user.setGreeting(greeting);\r
+ user.setFirstName(firstName);\r
+ user.setMiddleName(middleName);\r
+ user.setLastName(lastName);\r
+ user.setJobTitle(jobTitle);\r
+ user.setStatus(WorkflowConstants.STATUS_DRAFT);\r
+ user.setExpandoBridgeAttributes(serviceContext);\r
+\r
+ userPersistence.update(user, false, serviceContext);\r
+\r
+ // Resources\r
+\r
+ String creatorUserName = StringPool.BLANK;\r
+\r
+ if (creatorUserId <= 0) {\r
+ creatorUserId = user.getUserId();\r
+\r
+ // Don't grab the full name from the User object because it doesn't\r
+ // have a corresponding Contact object yet\r
+\r
+ //creatorUserName = user.getFullName();\r
+ }\r
+ else {\r
+ User creatorUser = userPersistence.findByPrimaryKey(creatorUserId);\r
+\r
+ creatorUserName = creatorUser.getFullName();\r
+ }\r
+\r
+ resourceLocalService.addResources(\r
+ companyId, 0, creatorUserId, User.class.getName(), user.getUserId(),\r
+ false, false, false);\r
+\r
+ // Contact\r
+\r
+ Date birthday = PortalUtil.getDate(\r
+ birthdayMonth, birthdayDay, birthdayYear,\r
+ ContactBirthdayException.class);\r
+\r
+ Contact contact = contactPersistence.create(user.getContactId());\r
+\r
+ contact.setCompanyId(user.getCompanyId());\r
+ contact.setUserId(creatorUserId);\r
+ contact.setUserName(creatorUserName);\r
+ contact.setCreateDate(now);\r
+ contact.setModifiedDate(now);\r
+ contact.setAccountId(company.getAccountId());\r
+ contact.setParentContactId(ContactConstants.DEFAULT_PARENT_CONTACT_ID);\r
+ contact.setFirstName(firstName);\r
+ contact.setMiddleName(middleName);\r
+ contact.setLastName(lastName);\r
+ contact.setPrefixId(prefixId);\r
+ contact.setSuffixId(suffixId);\r
+ contact.setMale(male);\r
+ contact.setBirthday(birthday);\r
+ contact.setJobTitle(jobTitle);\r
+\r
+ contactPersistence.update(contact, false, serviceContext);\r
+\r
+ // Group\r
+\r
+ groupLocalService.addGroup(\r
+ user.getUserId(), User.class.getName(), user.getUserId(), null,\r
+ null, 0, StringPool.SLASH + screenName, false, true, null);\r
+\r
+ // Groups\r
+\r
+ if (groupIds != null) {\r
+ groupLocalService.addUserGroups(userId, groupIds);\r
+ }\r
+\r
+ addDefaultGroups(userId);\r
+\r
+ // Organizations\r
+\r
+ updateOrganizations(userId, organizationIds, false);\r
+\r
+ // Roles\r
+\r
+ if (roleIds != null) {\r
+ roleIds = UsersAdminUtil.addRequiredRoles(user, roleIds);\r
+\r
+ userPersistence.setRoles(userId, roleIds);\r
+ }\r
+\r
+ addDefaultRoles(userId);\r
+\r
+ // User groups\r
+\r
+ if (userGroupIds != null) {\r
+ if (PropsValues.USER_GROUPS_COPY_LAYOUTS_TO_USER_PERSONAL_SITE) {\r
+ for (long userGroupId : userGroupIds) {\r
+ userGroupLocalService.copyUserGroupLayouts(\r
+ userGroupId, new long[] {userId});\r
+ }\r
+ }\r
+\r
+ userPersistence.setUserGroups(userId, userGroupIds);\r
+ }\r
+\r
+ addDefaultUserGroups(userId);\r
+\r
+ // Asset\r
+\r
+ if (serviceContext != null) {\r
+ updateAsset(\r
+ creatorUserId, user, serviceContext.getAssetCategoryIds(),\r
+ serviceContext.getAssetTagNames());\r
+ }\r
+\r
+ // Indexer\r
+\r
+ if ((serviceContext == null) || serviceContext.isIndexingEnabled()) {\r
+ reindex(user);\r
+ }\r
+\r
+ // Workflow\r
+\r
+ long workflowUserId = creatorUserId;\r
+\r
+ if (workflowUserId == userId) {\r
+ workflowUserId = defaultUser.getUserId();\r
+ }\r
+\r
+ ServiceContext workflowServiceContext = serviceContext;\r
+\r
+ if (workflowServiceContext == null) {\r
+ workflowServiceContext = new ServiceContext();\r
+ }\r
+\r
+ workflowServiceContext.setAttribute("autoPassword", autoPassword);\r
+ workflowServiceContext.setAttribute("sendEmail", sendEmail);\r
+\r
+ WorkflowHandlerRegistryUtil.startWorkflowInstance(\r
+ companyId, workflowUserId, User.class.getName(), userId, user,\r
+ workflowServiceContext);\r
+\r
+ if (serviceContext != null) {\r
+ String passwordUnencrypted =\r
+ (String)serviceContext.getAttribute("passwordUnencrypted");\r
+\r
+ if (Validator.isNotNull(passwordUnencrypted)) {\r
+ user.setPasswordUnencrypted(passwordUnencrypted);\r
+ }\r
+ }\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Attempts to authenticate the user by their email address and password,\r
+ * while using the AuthPipeline.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param emailAddress the user's email address\r
+ * @param password the user's password\r
+ * @param headerMap the header map from the authentication request\r
+ * @param parameterMap the parameter map from the authentication request\r
+ * @param resultsMap the map of authentication results (may be nil). After\r
+ * a succesful authentication the user's primary key will be placed\r
+ * under the key <code>userId</code>.\r
+ * @return the authentication status. This can be {@link\r
+ * com.liferay.portal.security.auth.Authenticator#FAILURE}\r
+ * indicating that the user's credentials are invalid, {@link\r
+ * com.liferay.portal.security.auth.Authenticator#SUCCESS}\r
+ * indicating a successful login, or {@link\r
+ * com.liferay.portal.security.auth.Authenticator#DNE} indicating\r
+ * that a user with that login does not exist.\r
+ * @throws PortalException if <code>emailAddress</code> or\r
+ * <code>password</code> was <code>null</code>\r
+ * @throws SystemException if a system exception occurred\r
+ * @see com.liferay.portal.security.auth.AuthPipeline\r
+ */\r
+ public int authenticateByEmailAddress(\r
+ long companyId, String emailAddress, String password,\r
+ Map<String, String[]> headerMap, Map<String, String[]> parameterMap,\r
+ Map<String, Object> resultsMap)\r
+ throws PortalException, SystemException {\r
+\r
+ return authenticate(\r
+ companyId, emailAddress, password, CompanyConstants.AUTH_TYPE_EA,\r
+ headerMap, parameterMap, resultsMap);\r
+ }\r
+\r
+ /**\r
+ * Attempts to authenticate the user by their screen name and password,\r
+ * while using the AuthPipeline.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param screenName the user's screen name\r
+ * @param password the user's password\r
+ * @param headerMap the header map from the authentication request\r
+ * @param parameterMap the parameter map from the authentication request\r
+ * @param resultsMap the map of authentication results (may be nil). After\r
+ * a succesful authentication the user's primary key will be placed\r
+ * under the key <code>userId</code>.\r
+ * @return the authentication status. This can be {@link\r
+ * com.liferay.portal.security.auth.Authenticator#FAILURE}\r
+ * indicating that the user's credentials are invalid, {@link\r
+ * com.liferay.portal.security.auth.Authenticator#SUCCESS}\r
+ * indicating a successful login, or {@link\r
+ * com.liferay.portal.security.auth.Authenticator#DNE} indicating\r
+ * that a user with that login does not exist.\r
+ * @throws PortalException if <code>screenName</code> or\r
+ * <code>password</code> was <code>null</code>\r
+ * @throws SystemException if a system exception occurred\r
+ * @see com.liferay.portal.security.auth.AuthPipeline\r
+ */\r
+ public int authenticateByScreenName(\r
+ long companyId, String screenName, String password,\r
+ Map<String, String[]> headerMap, Map<String, String[]> parameterMap,\r
+ Map<String, Object> resultsMap)\r
+ throws PortalException, SystemException {\r
+\r
+ return authenticate(\r
+ companyId, screenName, password, CompanyConstants.AUTH_TYPE_SN,\r
+ headerMap, parameterMap, resultsMap);\r
+ }\r
+\r
+ /**\r
+ * Attempts to authenticate the user by their primary key and password,\r
+ * while using the AuthPipeline.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param userId the user's primary key\r
+ * @param password the user's password\r
+ * @param headerMap the header map from the authentication request\r
+ * @param parameterMap the parameter map from the authentication request\r
+ * @param resultsMap the map of authentication results (may be nil). After\r
+ * a succesful authentication the user's primary key will be placed\r
+ * under the key <code>userId</code>.\r
+ * @return the authentication status. This can be {@link\r
+ * com.liferay.portal.security.auth.Authenticator#FAILURE}\r
+ * indicating that the user's credentials are invalid, {@link\r
+ * com.liferay.portal.security.auth.Authenticator#SUCCESS}\r
+ * indicating a successful login, or {@link\r
+ * com.liferay.portal.security.auth.Authenticator#DNE} indicating\r
+ * that a user with that login does not exist.\r
+ * @throws PortalException if <code>userId</code> or <code>password</code>\r
+ * was <code>null</code>\r
+ * @throws SystemException if a system exception occurred\r
+ * @see com.liferay.portal.security.auth.AuthPipeline\r
+ */\r
+ public int authenticateByUserId(\r
+ long companyId, long userId, String password,\r
+ Map<String, String[]> headerMap, Map<String, String[]> parameterMap,\r
+ Map<String, Object> resultsMap)\r
+ throws PortalException, SystemException {\r
+\r
+ return authenticate(\r
+ companyId, String.valueOf(userId), password,\r
+ CompanyConstants.AUTH_TYPE_ID, headerMap, parameterMap, resultsMap);\r
+ }\r
+\r
+ /**\r
+ * Attempts to authenticate the user using HTTP basic access authentication,\r
+ * without using the AuthPipeline. Primarily used for authenticating users\r
+ * of <code>tunnel-web</code>.\r
+ *\r
+ * <p>\r
+ * Authentication type specifies what <code>login</code> contains.The valid\r
+ * values are:\r
+ * </p>\r
+ *\r
+ * <ul>\r
+ * <li>\r
+ * <code>CompanyConstants.AUTH_TYPE_EA</code> - <code>login</code> is the\r
+ * user's email address\r
+ * </li>\r
+ * <li>\r
+ * <code>CompanyConstants.AUTH_TYPE_SN</code> - <code>login</code> is the\r
+ * user's screen name\r
+ * </li>\r
+ * <li>\r
+ * <code>CompanyConstants.AUTH_TYPE_ID</code> - <code>login</code> is the\r
+ * user's primary key\r
+ * </li>\r
+ * </ul>\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param authType the type of authentication to perform\r
+ * @param login either the user's email address, screen name, or primary\r
+ * key depending on the value of <code>authType</code>\r
+ * @param password the user's password\r
+ * @return the authentication status. This can be {@link\r
+ * com.liferay.portal.security.auth.Authenticator#FAILURE}\r
+ * indicating that the user's credentials are invalid, {@link\r
+ * com.liferay.portal.security.auth.Authenticator#SUCCESS}\r
+ * indicating a successful login, or {@link\r
+ * com.liferay.portal.security.auth.Authenticator#DNE} indicating\r
+ * that a user with that login does not exist.\r
+ * @throws PortalException if a portal exception occurred\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)\r
+ public long authenticateForBasic(\r
+ long companyId, String authType, String login, String password)\r
+ throws PortalException, SystemException {\r
+\r
+ if (PropsValues.AUTH_LOGIN_DISABLED) {\r
+ return 0;\r
+ }\r
+\r
+ try {\r
+ User user = null;\r
+\r
+ if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {\r
+ user = getUserByEmailAddress(companyId, login);\r
+ }\r
+ else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {\r
+ user = getUserByScreenName(companyId, login);\r
+ }\r
+ else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {\r
+ user = getUserById(companyId, GetterUtil.getLong(login));\r
+ }\r
+\r
+ if (user.isDefaultUser()) {\r
+ if (_log.isInfoEnabled()) {\r
+ _log.info(\r
+ "Basic authentication is disabled for the default " +\r
+ "user");\r
+ }\r
+\r
+ return 0;\r
+ }\r
+ else if (!user.isActive()) {\r
+ if (_log.isInfoEnabled()) {\r
+ _log.info(\r
+ "Basic authentication is disabled for inactive user " +\r
+ user.getUserId());\r
+ }\r
+\r
+ return 0;\r
+ }\r
+\r
+ if (!PropsValues.BASIC_AUTH_PASSWORD_REQUIRED) {\r
+ return user.getUserId();\r
+ }\r
+\r
+ String userPassword = user.getPassword();\r
+\r
+ if (!user.isPasswordEncrypted()) {\r
+ userPassword = PwdEncryptor.encrypt(userPassword);\r
+ }\r
+\r
+ String encPassword = PwdEncryptor.encrypt(password);\r
+\r
+ if (userPassword.equals(password) ||\r
+ userPassword.equals(encPassword)) {\r
+\r
+ return user.getUserId();\r
+ }\r
+ }\r
+ catch (NoSuchUserException nsue) {\r
+ }\r
+\r
+ return 0;\r
+ }\r
+\r
+ /**\r
+ * Attempts to authenticate the user using HTTP digest access\r
+ * authentication, without using the AuthPipeline. Primarily used for\r
+ * authenticating users of <code>tunnel-web</code>.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param username either the user's email address, screen name, or primary\r
+ * key\r
+ * @param realm unused\r
+ * @param nonce the number used once\r
+ * @param method the request method\r
+ * @param uri the request URI\r
+ * @param response the authentication response hash\r
+ * @return the user's primary key if authentication is succesful;\r
+ * <code>0</code> otherwise\r
+ * @throws PortalException if a portal exception occurred\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)\r
+ public long authenticateForDigest(\r
+ long companyId, String username, String realm, String nonce,\r
+ String method, String uri, String response)\r
+ throws PortalException, SystemException {\r
+\r
+ if (PropsValues.AUTH_LOGIN_DISABLED) {\r
+ return 0;\r
+ }\r
+\r
+ // Get User\r
+\r
+ User user = null;\r
+\r
+ try {\r
+ user = getUserByEmailAddress(companyId, username);\r
+ }\r
+ catch (NoSuchUserException nsue) {\r
+ try {\r
+ user = getUserByScreenName(companyId, username);\r
+ }\r
+ catch (NoSuchUserException nsue2) {\r
+ try {\r
+ user = getUserById(GetterUtil.getLong(username));\r
+ }\r
+ catch (NoSuchUserException nsue3) {\r
+ return 0;\r
+ }\r
+ }\r
+ }\r
+\r
+ if (user.isDefaultUser()) {\r
+ if (_log.isInfoEnabled()) {\r
+ _log.info(\r
+ "Digest authentication is disabled for the default user");\r
+ }\r
+\r
+ return 0;\r
+ }\r
+ else if (!user.isActive()) {\r
+ if (_log.isInfoEnabled()) {\r
+ _log.info(\r
+ "Digest authentication is disabled for inactive user " +\r
+ user.getUserId());\r
+ }\r
+\r
+ return 0;\r
+ }\r
+\r
+ // Verify digest\r
+\r
+ String digest = user.getDigest();\r
+\r
+ if (Validator.isNull(digest)) {\r
+ _log.error(\r
+ "User must first login through the portal " + user.getUserId());\r
+\r
+ return 0;\r
+ }\r
+\r
+ String[] digestArray = StringUtil.split(user.getDigest());\r
+\r
+ for (String ha1 : digestArray) {\r
+ String ha2 = DigesterUtil.digestHex(Digester.MD5, method, uri);\r
+\r
+ String curResponse = DigesterUtil.digestHex(\r
+ Digester.MD5, ha1, nonce, ha2);\r
+\r
+ if (response.equals(curResponse)) {\r
+ return user.getUserId();\r
+ }\r
+ }\r
+\r
+ return 0;\r
+ }\r
+\r
+ /**\r
+ * Attempts to authenticate the user using JAAS credentials, without using\r
+ * the AuthPipeline.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param encPassword the encrypted password\r
+ * @return <code>true</code> if authentication is successful;\r
+ * <code>false</code> otherwise\r
+ */\r
+ @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)\r
+ public boolean authenticateForJAAS(long userId, String encPassword) {\r
+ if (PropsValues.AUTH_LOGIN_DISABLED) {\r
+ return false;\r
+ }\r
+\r
+ try {\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ if (user.isDefaultUser()) {\r
+ if (_log.isInfoEnabled()) {\r
+ _log.info(\r
+ "JAAS authentication is disabled for the default user");\r
+ }\r
+\r
+ return false;\r
+ }\r
+ else if (!user.isActive()) {\r
+ if (_log.isInfoEnabled()) {\r
+ _log.info(\r
+ "JAAS authentication is disabled for inactive user " +\r
+ userId);\r
+ }\r
+\r
+ return false;\r
+ }\r
+\r
+ String password = user.getPassword();\r
+\r
+ if (user.isPasswordEncrypted()) {\r
+ if (password.equals(encPassword)) {\r
+ return true;\r
+ }\r
+\r
+ if (!PropsValues.PORTAL_JAAS_STRICT_PASSWORD) {\r
+ encPassword = PwdEncryptor.encrypt(encPassword, password);\r
+\r
+ if (password.equals(encPassword)) {\r
+ return true;\r
+ }\r
+ }\r
+ }\r
+ else {\r
+ if (!PropsValues.PORTAL_JAAS_STRICT_PASSWORD) {\r
+ if (password.equals(encPassword)) {\r
+ return true;\r
+ }\r
+ }\r
+\r
+ password = PwdEncryptor.encrypt(password);\r
+\r
+ if (password.equals(encPassword)) {\r
+ return true;\r
+ }\r
+ }\r
+ }\r
+ catch (Exception e) {\r
+ _log.error(e);\r
+ }\r
+\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * Checks if the user is currently locked out based on the password policy,\r
+ * and performs maintenance on the user's lockout and failed login data.\r
+ *\r
+ * @param user the user\r
+ * @throws PortalException if the user was determined to still be locked out\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void checkLockout(User user)\r
+ throws PortalException, SystemException {\r
+\r
+ if (LDAPSettingsUtil.isPasswordPolicyEnabled(user.getCompanyId())) {\r
+ return;\r
+ }\r
+\r
+ PasswordPolicy passwordPolicy = user.getPasswordPolicy();\r
+\r
+ if (passwordPolicy.isLockout()) {\r
+\r
+ // Reset failure count\r
+\r
+ Date now = new Date();\r
+ int failedLoginAttempts = user.getFailedLoginAttempts();\r
+\r
+ if (failedLoginAttempts > 0) {\r
+ long failedLoginTime = user.getLastFailedLoginDate().getTime();\r
+ long elapsedTime = now.getTime() - failedLoginTime;\r
+ long requiredElapsedTime =\r
+ passwordPolicy.getResetFailureCount() * 1000;\r
+\r
+ if ((requiredElapsedTime != 0) &&\r
+ (elapsedTime > requiredElapsedTime)) {\r
+\r
+ user.setLastFailedLoginDate(null);\r
+ user.setFailedLoginAttempts(0);\r
+\r
+ userPersistence.update(user, false);\r
+ }\r
+ }\r
+\r
+ // Reset lockout\r
+\r
+ if (user.isLockout()) {\r
+ long lockoutTime = user.getLockoutDate().getTime();\r
+ long elapsedTime = now.getTime() - lockoutTime;\r
+ long requiredElapsedTime =\r
+ passwordPolicy.getLockoutDuration() * 1000;\r
+\r
+ if ((requiredElapsedTime != 0) &&\r
+ (elapsedTime > requiredElapsedTime)) {\r
+\r
+ user.setLockout(false);\r
+ user.setLockoutDate(null);\r
+\r
+ userPersistence.update(user, false);\r
+ }\r
+ }\r
+\r
+ if (user.isLockout()) {\r
+ throw new UserLockoutException();\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Adds a failed login attempt to the user and updates the user's last\r
+ * failed login date.\r
+ *\r
+ * @param user the user\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void checkLoginFailure(User user) throws SystemException {\r
+ Date now = new Date();\r
+\r
+ int failedLoginAttempts = user.getFailedLoginAttempts();\r
+\r
+ user.setLastFailedLoginDate(now);\r
+ user.setFailedLoginAttempts(++failedLoginAttempts);\r
+\r
+ userPersistence.update(user, false);\r
+ }\r
+\r
+ /**\r
+ * Adds a failed login attempt to the user with the email address and\r
+ * updates the user's last failed login date.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param emailAddress the user's email address\r
+ * @throws PortalException if a user with the email address could not be\r
+ * found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void checkLoginFailureByEmailAddress(\r
+ long companyId, String emailAddress)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = getUserByEmailAddress(companyId, emailAddress);\r
+\r
+ checkLoginFailure(user);\r
+ }\r
+\r
+ /**\r
+ * Adds a failed login attempt to the user and updates the user's last\r
+ * failed login date.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void checkLoginFailureById(long userId)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ checkLoginFailure(user);\r
+ }\r
+\r
+ /**\r
+ * Adds a failed login attempt to the user with the screen name and updates\r
+ * the user's last failed login date.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param screenName the user's screen name\r
+ * @throws PortalException if a user with the screen name could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void checkLoginFailureByScreenName(long companyId, String screenName)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = getUserByScreenName(companyId, screenName);\r
+\r
+ checkLoginFailure(user);\r
+ }\r
+\r
+ /**\r
+ * Checks if the user's password is expired based on the password policy,\r
+ * and performs maintenance on the user's grace login and password reset\r
+ * data.\r
+ *\r
+ * @param user the user\r
+ * @throws PortalException if the user's password has expired and the grace\r
+ * login limit has been exceeded\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void checkPasswordExpired(User user)\r
+ throws PortalException, SystemException {\r
+\r
+ if (LDAPSettingsUtil.isPasswordPolicyEnabled(user.getCompanyId())) {\r
+ return;\r
+ }\r
+\r
+ PasswordPolicy passwordPolicy = user.getPasswordPolicy();\r
+\r
+ // Check if password has expired\r
+\r
+ if (isPasswordExpired(user)) {\r
+ int graceLoginCount = user.getGraceLoginCount();\r
+\r
+ if (graceLoginCount < passwordPolicy.getGraceLimit()) {\r
+ user.setGraceLoginCount(++graceLoginCount);\r
+\r
+ userPersistence.update(user, false);\r
+ }\r
+ else {\r
+ user.setDigest(StringPool.BLANK);\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ throw new PasswordExpiredException();\r
+ }\r
+ }\r
+\r
+ // Check if user should be forced to change password on first login\r
+\r
+ if (passwordPolicy.isChangeable() &&\r
+ passwordPolicy.isChangeRequired()) {\r
+\r
+ if (user.getLastLoginDate() == null) {\r
+ user.setPasswordReset(true);\r
+\r
+ userPersistence.update(user, false);\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Removes all the users from the organization.\r
+ *\r
+ * @param organizationId the primary key of the organization\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void clearOrganizationUsers(long organizationId)\r
+ throws SystemException {\r
+\r
+ organizationPersistence.clearUsers(organizationId);\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ /**\r
+ * Removes all the users from the user group.\r
+ *\r
+ * @param userGroupId the primary key of the user group\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void clearUserGroupUsers(long userGroupId) throws SystemException {\r
+ userGroupPersistence.clearUsers(userGroupId);\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ /**\r
+ * Completes the user's registration by generating a password and sending\r
+ * the confirmation email.\r
+ *\r
+ * @param user the user\r
+ * @param serviceContext the user's service context. Can set whether a\r
+ * password should be generated (with the <code>autoPassword</code>\r
+ * attribute) and whether the confirmation email should be sent\r
+ * (with the <code>sendEmail</code> attribute) for the user.\r
+ * @throws PortalException if a portal exception occurred\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void completeUserRegistration(\r
+ User user, ServiceContext serviceContext)\r
+ throws PortalException, SystemException {\r
+\r
+ boolean autoPassword = ParamUtil.getBoolean(\r
+ serviceContext, "autoPassword");\r
+\r
+ String password = null;\r
+\r
+ if (autoPassword) {\r
+ PasswordPolicy passwordPolicy =\r
+ passwordPolicyLocalService.getPasswordPolicy(\r
+ user.getCompanyId(), user.getOrganizationIds());\r
+\r
+ password = PwdToolkitUtil.generate(passwordPolicy);\r
+\r
+ user.setPassword(PwdEncryptor.encrypt(password));\r
+ user.setPasswordEncrypted(true);\r
+ user.setPasswordUnencrypted(password);\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ }\r
+\r
+ if (user.hasCompanyMx()) {\r
+ String mailPassword = password;\r
+\r
+ if (Validator.isNull(mailPassword)) {\r
+ mailPassword = user.getPasswordUnencrypted();\r
+ }\r
+\r
+ mailService.addUser(\r
+ user.getCompanyId(), user.getUserId(), mailPassword,\r
+ user.getFirstName(), user.getMiddleName(), user.getLastName(),\r
+ user.getEmailAddress());\r
+ }\r
+\r
+ boolean sendEmail = ParamUtil.getBoolean(serviceContext, "sendEmail");\r
+\r
+ if (sendEmail) {\r
+ sendEmail(user, password, serviceContext);\r
+ }\r
+\r
+ Company company = companyPersistence.findByPrimaryKey(\r
+ user.getCompanyId());\r
+\r
+ if (company.isStrangersVerify() && (serviceContext.getPlid() > 0)) {\r
+ sendEmailAddressVerification(\r
+ user, user.getEmailAddress(), serviceContext);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Decrypts the user's primary key and password from their encrypted forms.\r
+ * Used for decrypting a user's credentials from the values stored in an\r
+ * automatic login cookie.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param name the encrypted primary key of the user\r
+ * @param password the encrypted password of the user\r
+ * @return the user's primary key and password\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * or if the user's password was incorrect\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public KeyValuePair decryptUserId(\r
+ long companyId, String name, String password)\r
+ throws PortalException, SystemException {\r
+\r
+ Company company = companyPersistence.findByPrimaryKey(companyId);\r
+\r
+ try {\r
+ name = Encryptor.decrypt(company.getKeyObj(), name);\r
+ }\r
+ catch (EncryptorException ee) {\r
+ throw new SystemException(ee);\r
+ }\r
+\r
+ long userId = GetterUtil.getLong(name);\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ try {\r
+ password = Encryptor.decrypt(company.getKeyObj(), password);\r
+ }\r
+ catch (EncryptorException ee) {\r
+ throw new SystemException(ee);\r
+ }\r
+\r
+ String encPassword = PwdEncryptor.encrypt(password);\r
+\r
+ if (user.getPassword().equals(encPassword)) {\r
+ if (isPasswordExpired(user)) {\r
+ user.setPasswordReset(true);\r
+\r
+ userPersistence.update(user, false);\r
+ }\r
+\r
+ return new KeyValuePair(name, password);\r
+ }\r
+ else {\r
+ throw new PrincipalException();\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Deletes the user's portrait image.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * or if the user's portrait could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void deletePortrait(long userId)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ long portraitId = user.getPortraitId();\r
+\r
+ if (portraitId > 0) {\r
+ user.setPortraitId(0);\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ imageLocalService.deleteImage(portraitId);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Removes the user from the role.\r
+ *\r
+ * @param roleId the primary key of the role\r
+ * @param userId the primary key of the user\r
+ * @throws PortalException if a role or user with the primary key could not\r
+ * be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void deleteRoleUser(long roleId, long userId)\r
+ throws PortalException, SystemException {\r
+\r
+ rolePersistence.removeUser(roleId, userId);\r
+\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);\r
+\r
+ indexer.reindex(userId);\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ /**\r
+ * Deletes the user.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ @Override\r
+ public User deleteUser(long userId)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ return deleteUser(user);\r
+ }\r
+\r
+ /**\r
+ * Deletes the user.\r
+ *\r
+ * @param user the user\r
+ * @throws PortalException if a portal exception occurred\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ @Override\r
+ public User deleteUser(User user) throws PortalException, SystemException {\r
+ if (!PropsValues.USERS_DELETE) {\r
+ throw new RequiredUserException();\r
+ }\r
+\r
+ // Browser tracker\r
+\r
+ browserTrackerLocalService.deleteUserBrowserTracker(user.getUserId());\r
+\r
+ // Group\r
+\r
+ Group group = user.getGroup();\r
+\r
+ if (group != null) {\r
+ groupLocalService.deleteGroup(group);\r
+ }\r
+\r
+ // Portrait\r
+\r
+ imageLocalService.deleteImage(user.getPortraitId());\r
+\r
+ // Password policy relation\r
+\r
+ passwordPolicyRelLocalService.deletePasswordPolicyRel(\r
+ User.class.getName(), user.getUserId());\r
+\r
+ // Old passwords\r
+\r
+ passwordTrackerLocalService.deletePasswordTrackers(user.getUserId());\r
+\r
+ // Subscriptions\r
+\r
+ subscriptionLocalService.deleteSubscriptions(user.getUserId());\r
+\r
+ // External user ids\r
+\r
+ userIdMapperLocalService.deleteUserIdMappers(user.getUserId());\r
+\r
+ // Announcements\r
+\r
+ announcementsDeliveryLocalService.deleteDeliveries(user.getUserId());\r
+\r
+ // Asset\r
+\r
+ assetEntryLocalService.deleteEntry(\r
+ User.class.getName(), user.getUserId());\r
+\r
+ // Blogs\r
+\r
+ blogsStatsUserLocalService.deleteStatsUserByUserId(user.getUserId());\r
+\r
+ // Document library\r
+\r
+ dlFileRankLocalService.deleteFileRanksByUserId(user.getUserId());\r
+\r
+ // Expando\r
+\r
+ expandoValueLocalService.deleteValues(\r
+ User.class.getName(), user.getUserId());\r
+\r
+ // Message boards\r
+\r
+ mbBanLocalService.deleteBansByBanUserId(user.getUserId());\r
+ mbStatsUserLocalService.deleteStatsUsersByUserId(user.getUserId());\r
+ mbThreadFlagLocalService.deleteThreadFlagsByUserId(user.getUserId());\r
+\r
+ // Membership requests\r
+\r
+ membershipRequestLocalService.deleteMembershipRequestsByUserId(\r
+ user.getUserId());\r
+\r
+ // Shopping cart\r
+\r
+ shoppingCartLocalService.deleteUserCarts(user.getUserId());\r
+\r
+ // Social\r
+\r
+ socialActivityLocalService.deleteUserActivities(user.getUserId());\r
+ socialRequestLocalService.deleteReceiverUserRequests(user.getUserId());\r
+ socialRequestLocalService.deleteUserRequests(user.getUserId());\r
+\r
+ // Mail\r
+\r
+ mailService.deleteUser(user.getCompanyId(), user.getUserId());\r
+\r
+ // Contact\r
+\r
+ try {\r
+ contactLocalService.deleteContact(user.getContactId());\r
+ }\r
+ catch (NoSuchContactException nsce) {\r
+ }\r
+\r
+ // Resources\r
+\r
+ resourceLocalService.deleteResource(\r
+ user.getCompanyId(), User.class.getName(),\r
+ ResourceConstants.SCOPE_INDIVIDUAL, user.getUserId());\r
+\r
+ // Group roles\r
+\r
+ userGroupRoleLocalService.deleteUserGroupRolesByUserId(\r
+ user.getUserId());\r
+\r
+ // User\r
+\r
+ userPersistence.remove(user);\r
+\r
+ // Permission cache\r
+\r
+ PermissionCacheUtil.clearCache();\r
+\r
+ // Workflow\r
+\r
+ workflowInstanceLinkLocalService.deleteWorkflowInstanceLinks(\r
+ user.getCompanyId(), 0, User.class.getName(), user.getUserId());\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Removes the user from the user group.\r
+ *\r
+ * @param userGroupId the primary key of the user group\r
+ * @param userId the primary key of the user\r
+ * @throws PortalException if a portal exception occurred\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void deleteUserGroupUser(long userGroupId, long userId)\r
+ throws PortalException, SystemException {\r
+\r
+ userGroupPersistence.removeUser(userGroupId, userId);\r
+\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);\r
+\r
+ indexer.reindex(userId);\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ /**\r
+ * Encrypts the primary key of the user. Used when encrypting the user's\r
+ * credentials for storage in an automatic login cookie.\r
+ *\r
+ * @param name the primary key of the user\r
+ * @return the user's encrypted primary key\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)\r
+ public String encryptUserId(String name)\r
+ throws PortalException, SystemException {\r
+\r
+ long userId = GetterUtil.getLong(name);\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ Company company = companyPersistence.findByPrimaryKey(\r
+ user.getCompanyId());\r
+\r
+ try {\r
+ return Encryptor.encrypt(company.getKeyObj(), name);\r
+ }\r
+ catch (EncryptorException ee) {\r
+ throw new SystemException(ee);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Returns the user with the email address.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param emailAddress the user's email address\r
+ * @return the user with the email address, or <code>null</code> if a user\r
+ * with the email address could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User fetchUserByEmailAddress(long companyId, String emailAddress)\r
+ throws SystemException {\r
+\r
+ return userPersistence.fetchByC_EA(companyId, emailAddress);\r
+ }\r
+\r
+ /**\r
+ * Returns the user with the primary key.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @return the user with the primary key, or <code>null</code> if a user\r
+ * with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User fetchUserById(long userId) throws SystemException {\r
+ return userPersistence.fetchByPrimaryKey(userId);\r
+ }\r
+\r
+ /**\r
+ * Returns the user with the screen name.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param screenName the user's screen name\r
+ * @return the user with the screen name, or <code>null</code> if a user\r
+ * with the screen name could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User fetchUserByScreenName(long companyId, String screenName)\r
+ throws SystemException {\r
+\r
+ screenName = getScreenName(screenName);\r
+\r
+ return userPersistence.fetchByC_SN(companyId, screenName);\r
+ }\r
+\r
+ /**\r
+ * Returns the users with the given email\r
+ */\r
+ public List<User> findUsersByEmail(String email) throws SystemException {\r
+ return userPersistence.findByEmailAddress(email);\r
+ }\r
+\r
+\r
+ /**\r
+ * Returns a range of all the users belonging to the company.\r
+ *\r
+ * <p>\r
+ * Useful when paginating results. Returns a maximum of <code>end -\r
+ * start</code> instances. <code>start</code> and <code>end</code> are not\r
+ * primary keys, they are indexes in the result set. Thus, <code>0</code>\r
+ * refers to the first result in the set. Setting both <code>start</code>\r
+ * and <code>end</code> to {@link\r
+ * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full\r
+ * result set.\r
+ * </p>\r
+ *\r
+ * @param companyId the primary key of the company\r
+ * @param start the lower bound of the range of users\r
+ * @param end the upper bound of the range of users (not inclusive)\r
+ * @return the range of users belonging to the company\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public List<User> getCompanyUsers(long companyId, int start, int end)\r
+ throws SystemException {\r
+\r
+ return userPersistence.findByCompanyId(companyId, start, end);\r
+ }\r
+\r
+ /**\r
+ * Returns the number of users belonging to the company.\r
+ *\r
+ * @param companyId the primary key of the company\r
+ * @return the number of users belonging to the company\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public int getCompanyUsersCount(long companyId) throws SystemException {\r
+ return userPersistence.countByCompanyId(companyId);\r
+ }\r
+\r
+ /**\r
+ * Returns the default user for the company.\r
+ *\r
+ * @param companyId the primary key of the company\r
+ * @return the default user for the company\r
+ * @throws PortalException if a default user for the company could not be\r
+ * found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ @Skip\r
+ public User getDefaultUser(long companyId)\r
+ throws PortalException, SystemException {\r
+\r
+ User userModel = _defaultUsers.get(companyId);\r
+\r
+ if (userModel == null) {\r
+ userModel = userLocalService.loadGetDefaultUser(companyId);\r
+\r
+ _defaultUsers.put(companyId, userModel);\r
+ }\r
+\r
+ return userModel;\r
+ }\r
+\r
+ /**\r
+ * Returns the primary key of the default user for the company.\r
+ *\r
+ * @param companyId the primary key of the company\r
+ * @return the primary key of the default user for the company\r
+ * @throws PortalException if a default user for the company could not be\r
+ * found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ @Skip\r
+ public long getDefaultUserId(long companyId)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = getDefaultUser(companyId);\r
+\r
+ return user.getUserId();\r
+ }\r
+\r
+ /**\r
+ * Returns the primary keys of all the users belonging to the group.\r
+ *\r
+ * @param groupId the primary key of the group\r
+ * @return the primary keys of the users belonging to the group\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public long[] getGroupUserIds(long groupId) throws SystemException {\r
+ return getUserIds(getGroupUsers(groupId));\r
+ }\r
+\r
+ /**\r
+ * Returns all the users belonging to the group.\r
+ *\r
+ * @param groupId the primary key of the group\r
+ * @return the users belonging to the group\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public List<User> getGroupUsers(long groupId) throws SystemException {\r
+ return groupPersistence.getUsers(groupId);\r
+ }\r
+\r
+ /**\r
+ * Returns the number of users belonging to the group.\r
+ *\r
+ * @param groupId the primary key of the group\r
+ * @return the number of users belonging to the group\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public int getGroupUsersCount(long groupId) throws SystemException {\r
+ return groupPersistence.getUsersSize(groupId);\r
+ }\r
+\r
+ /**\r
+ * Returns the number of users with the status belonging to the group.\r
+ *\r
+ * @param groupId the primary key of the group\r
+ * @param status the workflow status\r
+ * @return the number of users with the status belonging to the group\r
+ * @throws PortalException if a group with the primary key could not be\r
+ * found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public int getGroupUsersCount(long groupId, int status)\r
+ throws PortalException, SystemException {\r
+\r
+ Group group = groupPersistence.findByPrimaryKey(groupId);\r
+\r
+ LinkedHashMap<String, Object> params =\r
+ new LinkedHashMap<String, Object>();\r
+\r
+ params.put("usersGroups", new Long(groupId));\r
+\r
+ return searchCount(group.getCompanyId(), null, status, params);\r
+ }\r
+\r
+ /**\r
+ * Returns all the users who have not had any announcements of the type\r
+ * delivered, excluding the default user.\r
+ *\r
+ * @param type the type of announcement\r
+ * @return the users who have not had any annoucements of the type delivered\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public List<User> getNoAnnouncementsDeliveries(String type)\r
+ throws SystemException {\r
+\r
+ return userFinder.findByNoAnnouncementsDeliveries(type);\r
+ }\r
+\r
+ /**\r
+ * Returns all the users who do not have any contacts.\r
+ *\r
+ * @return the users who do not have any contacts\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public List<User> getNoContacts() throws SystemException {\r
+ return userFinder.findByNoContacts();\r
+ }\r
+\r
+ /**\r
+ * Returns all the users who do not belong to any groups, excluding the\r
+ * default user.\r
+ *\r
+ * @return the users who do not belong to any groups\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public List<User> getNoGroups() throws SystemException {\r
+ return userFinder.findByNoGroups();\r
+ }\r
+\r
+ /**\r
+ * Returns the primary keys of all the users belonging to the organization.\r
+ *\r
+ * @param organizationId the primary key of the organization\r
+ * @return the primary keys of the users belonging to the organization\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public long[] getOrganizationUserIds(long organizationId)\r
+ throws SystemException {\r
+\r
+ return getUserIds(getOrganizationUsers(organizationId));\r
+ }\r
+\r
+ /**\r
+ * Returns all the users belonging to the organization.\r
+ *\r
+ * @param organizationId the primary key of the organization\r
+ * @return the users belonging to the organization\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public List<User> getOrganizationUsers(long organizationId)\r
+ throws SystemException {\r
+\r
+ return organizationPersistence.getUsers(organizationId);\r
+ }\r
+ \r
+\r
+ public List<User> getOrganizationUsers(long organizationId,\r
+ int start, int stop) throws SystemException {\r
+ return organizationPersistence.getUsers(organizationId, start, stop);\r
+ }\r
+ \r
+ public List<User> getOrganizationUsers(long organizationId,\r
+ int start, int stop, OrderByComparator orderComp) throws SystemException {\r
+ return organizationPersistence.getUsers(organizationId, start, stop, orderComp);\r
+ }\r
+\r
+ /**\r
+ * Returns the number of users belonging to the organization.\r
+ *\r
+ * @param organizationId the primary key of the organization\r
+ * @return the number of users belonging to the organization\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public int getOrganizationUsersCount(long organizationId)\r
+ throws SystemException {\r
+\r
+ return organizationPersistence.getUsersSize(organizationId);\r
+ }\r
+\r
+ /**\r
+ * Returns the number of users with the status belonging to the\r
+ * organization.\r
+ *\r
+ * @param organizationId the primary key of the organization\r
+ * @param status the workflow status\r
+ * @return the number of users with the status belonging to the organization\r
+ * @throws PortalException if an organization with the primary key could not\r
+ * be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public int getOrganizationUsersCount(long organizationId, int status)\r
+ throws PortalException, SystemException {\r
+\r
+ Organization organization = organizationPersistence.findByPrimaryKey(\r
+ organizationId);\r
+\r
+ LinkedHashMap<String, Object> params =\r
+ new LinkedHashMap<String, Object>();\r
+\r
+ params.put("usersOrgs", new Long(organizationId));\r
+\r
+ return searchCount(organization.getCompanyId(), null, status, params);\r
+ }\r
+\r
+ /**\r
+ * Returns the primary keys of all the users belonging to the role.\r
+ *\r
+ * @param roleId the primary key of the role\r
+ * @return the primary keys of the users belonging to the role\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public long[] getRoleUserIds(long roleId) throws SystemException {\r
+ return getUserIds(getRoleUsers(roleId));\r
+ }\r
+\r
+ /**\r
+ * Returns all the users belonging to the role.\r
+ *\r
+ * @param roleId the primary key of the role\r
+ * @return the users belonging to the role\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public List<User> getRoleUsers(long roleId) throws SystemException {\r
+ return rolePersistence.getUsers(roleId);\r
+ }\r
+\r
+ /**\r
+ * Returns a range of all the users belonging to the role.\r
+ *\r
+ * <p>\r
+ * Useful when paginating results. Returns a maximum of <code>end -\r
+ * start</code> instances. <code>start</code> and <code>end</code> are not\r
+ * primary keys, they are indexes in the result set. Thus, <code>0</code>\r
+ * refers to the first result in the set. Setting both <code>start</code>\r
+ * and <code>end</code> to {@link\r
+ * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full\r
+ * result set.\r
+ * </p>\r
+ *\r
+ * @param roleId the primary key of the role\r
+ * @param start the lower bound of the range of users\r
+ * @param end the upper bound of the range of users (not inclusive)\r
+ * @return the range of users belonging to the role\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public List<User> getRoleUsers(long roleId, int start, int end)\r
+ throws SystemException {\r
+\r
+ return rolePersistence.getUsers(roleId, start, end);\r
+ }\r
+\r
+ /**\r
+ * Returns the number of users belonging to the role.\r
+ *\r
+ * @param roleId the primary key of the role\r
+ * @return the number of users belonging to the role\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public int getRoleUsersCount(long roleId) throws SystemException {\r
+ return rolePersistence.getUsersSize(roleId);\r
+ }\r
+\r
+ /**\r
+ * Returns the number of users with the status belonging to the role.\r
+ *\r
+ * @param roleId the primary key of the role\r
+ * @param status the workflow status\r
+ * @return the number of users with the status belonging to the role\r
+ * @throws PortalException if an role with the primary key could not be\r
+ * found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public int getRoleUsersCount(long roleId, int status)\r
+ throws PortalException, SystemException {\r
+\r
+ Role role = rolePersistence.findByPrimaryKey(roleId);\r
+\r
+ LinkedHashMap<String, Object> params =\r
+ new LinkedHashMap<String, Object>();\r
+\r
+ params.put("usersRoles", new Long(roleId));\r
+\r
+ return searchCount(role.getCompanyId(), null, status, params);\r
+ }\r
+\r
+ /**\r
+ * Returns an ordered range of all the users with a social relation of the\r
+ * type with the user.\r
+ *\r
+ * <p>\r
+ * Useful when paginating results. Returns a maximum of <code>end -\r
+ * start</code> instances. <code>start</code> and <code>end</code> are not\r
+ * primary keys, they are indexes in the result set. Thus, <code>0</code>\r
+ * refers to the first result in the set. Setting both <code>start</code>\r
+ * and <code>end</code> to {@link\r
+ * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full\r
+ * result set.\r
+ * </p>\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param type the type of social relation. The possible types can be found\r
+ * in {@link\r
+ * com.liferay.portlet.social.model.SocialRelationConstants}.\r
+ * @param start the lower bound of the range of users\r
+ * @param end the upper bound of the range of users (not inclusive)\r
+ * @param obc the comparator to order the users by (optionally\r
+ * <code>null</code>)\r
+ * @return the ordered range of users with a social relation of the type\r
+ * with the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public List<User> getSocialUsers(\r
+ long userId, int type, int start, int end, OrderByComparator obc)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ LinkedHashMap<String, Object> params =\r
+ new LinkedHashMap<String, Object>();\r
+\r
+ params.put("socialRelationType", new Long[] {userId, new Long(type)});\r
+\r
+ return search(\r
+ user.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,\r
+ params, start, end, obc);\r
+ }\r
+\r
+ /**\r
+ * Returns an ordered range of all the users with a social relation with the\r
+ * user.\r
+ *\r
+ * <p>\r
+ * Useful when paginating results. Returns a maximum of <code>end -\r
+ * start</code> instances. <code>start</code> and <code>end</code> are not\r
+ * primary keys, they are indexes in the result set. Thus, <code>0</code>\r
+ * refers to the first result in the set. Setting both <code>start</code>\r
+ * and <code>end</code> to {@link\r
+ * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full\r
+ * result set.\r
+ * </p>\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param start the lower bound of the range of users\r
+ * @param end the upper bound of the range of users (not inclusive)\r
+ * @param obc the comparator to order the users by (optionally\r
+ * <code>null</code>)\r
+ * @return the ordered range of users with a social relation with the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public List<User> getSocialUsers(\r
+ long userId, int start, int end, OrderByComparator obc)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ LinkedHashMap<String, Object> params =\r
+ new LinkedHashMap<String, Object>();\r
+\r
+ params.put("socialRelation", new Long[] {userId});\r
+\r
+ return search(\r
+ user.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,\r
+ params, start, end, obc);\r
+ }\r
+\r
+ /**\r
+ * Returns an ordered range of all the users with a mutual social relation\r
+ * of the type with both of the given users.\r
+ *\r
+ * <p>\r
+ * Useful when paginating results. Returns a maximum of <code>end -\r
+ * start</code> instances. <code>start</code> and <code>end</code> are not\r
+ * primary keys, they are indexes in the result set. Thus, <code>0</code>\r
+ * refers to the first result in the set. Setting both <code>start</code>\r
+ * and <code>end</code> to {@link\r
+ * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full\r
+ * result set.\r
+ * </p>\r
+ *\r
+ * @param userId1 the primary key of the first user\r
+ * @param userId2 the primary key of the second user\r
+ * @param type the type of social relation. The possible types can be found\r
+ * in {@link\r
+ * com.liferay.portlet.social.model.SocialRelationConstants}.\r
+ * @param start the lower bound of the range of users\r
+ * @param end the upper bound of the range of users (not inclusive)\r
+ * @param obc the comparator to order the users by (optionally\r
+ * <code>null</code>)\r
+ * @return the ordered range of users with a mutual social relation of the\r
+ * type with the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public List<User> getSocialUsers(\r
+ long userId1, long userId2, int type, int start, int end,\r
+ OrderByComparator obc)\r
+ throws PortalException, SystemException {\r
+\r
+ User user1 = userPersistence.findByPrimaryKey(userId1);\r
+\r
+ LinkedHashMap<String, Object> params =\r
+ new LinkedHashMap<String, Object>();\r
+\r
+ params.put(\r
+ "socialMutualRelationType",\r
+ new Long[] {userId1, new Long(type), userId2, new Long(type)});\r
+\r
+ return search(\r
+ user1.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,\r
+ params, start, end, obc);\r
+ }\r
+\r
+ /**\r
+ * Returns an ordered range of all the users with a mutual social relation\r
+ * with both of the given users.\r
+ *\r
+ * <p>\r
+ * Useful when paginating results. Returns a maximum of <code>end -\r
+ * start</code> instances. <code>start</code> and <code>end</code> are not\r
+ * primary keys, they are indexes in the result set. Thus, <code>0</code>\r
+ * refers to the first result in the set. Setting both <code>start</code>\r
+ * and <code>end</code> to {@link\r
+ * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full\r
+ * result set.\r
+ * </p>\r
+ *\r
+ * @param userId1 the primary key of the first user\r
+ * @param userId2 the primary key of the second user\r
+ * @param start the lower bound of the range of users\r
+ * @param end the upper bound of the range of users (not inclusive)\r
+ * @param obc the comparator to order the users by (optionally\r
+ * <code>null</code>)\r
+ * @return the ordered range of users with a mutual social relation with the\r
+ * user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public List<User> getSocialUsers(\r
+ long userId1, long userId2, int start, int end,\r
+ OrderByComparator obc)\r
+ throws PortalException, SystemException {\r
+\r
+ User user1 = userPersistence.findByPrimaryKey(userId1);\r
+\r
+ LinkedHashMap<String, Object> params =\r
+ new LinkedHashMap<String, Object>();\r
+\r
+ params.put("socialMutualRelation", new Long[] {userId1, userId2});\r
+\r
+ return search(\r
+ user1.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,\r
+ params, start, end, obc);\r
+ }\r
+\r
+ /**\r
+ * Returns the number of users with a social relation with the user.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @return the number of users with a social relation with the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public int getSocialUsersCount(long userId)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ LinkedHashMap<String, Object> params =\r
+ new LinkedHashMap<String, Object>();\r
+\r
+ params.put("socialRelation", new Long[] {userId});\r
+\r
+ return searchCount(\r
+ user.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,\r
+ params);\r
+ }\r
+\r
+ /**\r
+ * Returns the number of users with a social relation of the type with the\r
+ * user.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param type the type of social relation. The possible types can be found\r
+ * in {@link\r
+ * com.liferay.portlet.social.model.SocialRelationConstants}.\r
+ * @return the number of users with a social relation of the type with the\r
+ * user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public int getSocialUsersCount(long userId, int type)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ LinkedHashMap<String, Object> params =\r
+ new LinkedHashMap<String, Object>();\r
+\r
+ params.put("socialRelationType", new Long[] {userId, new Long(type)});\r
+\r
+ return searchCount(\r
+ user.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,\r
+ params);\r
+ }\r
+\r
+ /**\r
+ * Returns the number of users with a mutual social relation with both of\r
+ * the given users.\r
+ *\r
+ * @param userId1 the primary key of the first user\r
+ * @param userId2 the primary key of the second user\r
+ * @return the number of users with a mutual social relation with the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public int getSocialUsersCount(long userId1, long userId2)\r
+ throws PortalException, SystemException {\r
+\r
+ User user1 = userPersistence.findByPrimaryKey(userId1);\r
+\r
+ LinkedHashMap<String, Object> params =\r
+ new LinkedHashMap<String, Object>();\r
+\r
+ params.put("socialMutualRelation", new Long[] {userId1, userId2});\r
+\r
+ return searchCount(\r
+ user1.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,\r
+ params);\r
+ }\r
+\r
+ /**\r
+ * Returns the number of users with a mutual social relation of the type\r
+ * with both of the given users.\r
+ *\r
+ * @param userId1 the primary key of the first user\r
+ * @param userId2 the primary key of the second user\r
+ * @param type the type of social relation. The possible types can be found\r
+ * in {@link\r
+ * com.liferay.portlet.social.model.SocialRelationConstants}.\r
+ * @return the number of users with a mutual social relation of the type\r
+ * with the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public int getSocialUsersCount(long userId1, long userId2, int type)\r
+ throws PortalException, SystemException {\r
+\r
+ User user1 = userPersistence.findByPrimaryKey(userId1);\r
+\r
+ LinkedHashMap<String, Object> params =\r
+ new LinkedHashMap<String, Object>();\r
+\r
+ params.put(\r
+ "socialMutualRelationType",\r
+ new Long[] {userId1, new Long(type), userId2, new Long(type)});\r
+\r
+ return searchCount(\r
+ user1.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED,\r
+ params);\r
+ }\r
+\r
+ /**\r
+ * Returns the user with the contact ID.\r
+ *\r
+ * @param contactId the user's contact ID\r
+ * @return the user with the contact ID\r
+ * @throws PortalException if a user with the contact ID could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User getUserByContactId(long contactId)\r
+ throws PortalException, SystemException {\r
+\r
+ return userPersistence.findByContactId(contactId);\r
+ }\r
+\r
+ /**\r
+ * Returns the user with the email address.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param emailAddress the user's email address\r
+ * @return the user with the email address\r
+ * @throws PortalException if a user with the email address could not be\r
+ * found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User getUserByEmailAddress(long companyId, String emailAddress)\r
+ throws PortalException, SystemException {\r
+\r
+ emailAddress = emailAddress.trim().toLowerCase();\r
+\r
+ return userPersistence.findByC_EA(companyId, emailAddress);\r
+ }\r
+\r
+ /**\r
+ * Returns the user with the Facebook ID.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param facebookId the user's Facebook ID\r
+ * @return the user with the Facebook ID\r
+ * @throws PortalException if a user with the Facebook ID could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User getUserByFacebookId(long companyId, long facebookId)\r
+ throws PortalException, SystemException {\r
+\r
+ return userPersistence.findByC_FID(companyId, facebookId);\r
+ }\r
+\r
+ /**\r
+ * Returns the user with the primary key.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @return the user with the primary key\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User getUserById(long userId)\r
+ throws PortalException, SystemException {\r
+\r
+ return userPersistence.findByPrimaryKey(userId);\r
+ }\r
+\r
+ /**\r
+ * Returns the user with the primary key from the company.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param userId the primary key of the user\r
+ * @return the user with the primary key\r
+ * @throws PortalException if a user with the primary key from the company\r
+ * could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User getUserById(long companyId, long userId)\r
+ throws PortalException, SystemException {\r
+\r
+ return userPersistence.findByC_U(companyId, userId);\r
+ }\r
+\r
+ /**\r
+ * Returns the user with the OpenID.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param openId the user's OpenID\r
+ * @return the user with the OpenID\r
+ * @throws PortalException if a user with the OpenID could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User getUserByOpenId(long companyId, String openId)\r
+ throws PortalException, SystemException {\r
+\r
+ return userPersistence.findByC_O(companyId, openId);\r
+ }\r
+\r
+ /**\r
+ * Returns the user with the portrait ID.\r
+ *\r
+ * @param portraitId the user's portrait ID\r
+ * @return the user with the portrait ID\r
+ * @throws PortalException if a user with the portrait ID could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User getUserByPortraitId(long portraitId)\r
+ throws PortalException, SystemException {\r
+\r
+ return userPersistence.findByPortraitId(portraitId);\r
+ }\r
+\r
+ /**\r
+ * Returns the user with the screen name.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param screenName the user's screen name\r
+ * @return the user with the screen name\r
+ * @throws PortalException if a user with the screen name could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User getUserByScreenName(long companyId, String screenName)\r
+ throws PortalException, SystemException {\r
+\r
+ screenName = getScreenName(screenName);\r
+\r
+ return userPersistence.findByC_SN(companyId, screenName);\r
+ }\r
+\r
+ /**\r
+ * Returns the user with the universally unique identifier.\r
+ *\r
+ * @param uuid the user's universally unique identifier\r
+ * @return the user with the universally unique identifier\r
+ * @throws PortalException if a user with the universally unique identifier\r
+ * could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User getUserByUuid(String uuid)\r
+ throws PortalException, SystemException {\r
+\r
+ List<User> users = userPersistence.findByUuid(uuid);\r
+\r
+ if (users.isEmpty()) {\r
+ throw new NoSuchUserException();\r
+ }\r
+ else {\r
+ return users.get(0);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Returns all the users belonging to the user group.\r
+ *\r
+ * @param userGroupId the primary key of the user group\r
+ * @return the users belonging to the user group\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public List<User> getUserGroupUsers(long userGroupId)\r
+ throws SystemException {\r
+\r
+ return userGroupPersistence.getUsers(userGroupId);\r
+ }\r
+\r
+ /**\r
+ * Returns the number of users belonging to the user group.\r
+ *\r
+ * @param userGroupId the primary key of the user group\r
+ * @return the number of users belonging to the user group\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public int getUserGroupUsersCount(long userGroupId) throws SystemException {\r
+ return userGroupPersistence.getUsersSize(userGroupId);\r
+ }\r
+\r
+ /**\r
+ * Returns the number of users with the status belonging to the user group.\r
+ *\r
+ * @param userGroupId the primary key of the user group\r
+ * @param status the workflow status\r
+ * @return the number of users with the status belonging to the user group\r
+ * @throws PortalException if a user group with the primary key could not be\r
+ * found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public int getUserGroupUsersCount(long userGroupId, int status)\r
+ throws PortalException, SystemException {\r
+\r
+ UserGroup userGroup = userGroupPersistence.findByPrimaryKey(\r
+ userGroupId);\r
+\r
+ LinkedHashMap<String, Object> params =\r
+ new LinkedHashMap<String, Object>();\r
+\r
+ params.put("usersUserGroups", new Long(userGroupId));\r
+\r
+ return searchCount(userGroup.getCompanyId(), null, status, params);\r
+ }\r
+\r
+ /**\r
+ * Returns the primary key of the user with the email address.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param emailAddress the user's email address\r
+ * @return the primary key of the user with the email address\r
+ * @throws PortalException if a user with the email address could not be\r
+ * found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public long getUserIdByEmailAddress(long companyId, String emailAddress)\r
+ throws PortalException, SystemException {\r
+\r
+ emailAddress = emailAddress.trim().toLowerCase();\r
+\r
+ User user = userPersistence.findByC_EA(companyId, emailAddress);\r
+\r
+ return user.getUserId();\r
+ }\r
+\r
+ /**\r
+ * Returns the primary key of the user with the screen name.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param screenName the user's screen name\r
+ * @return the primary key of the user with the screen name\r
+ * @throws PortalException if a user with the screen name could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public long getUserIdByScreenName(long companyId, String screenName)\r
+ throws PortalException, SystemException {\r
+\r
+ screenName = getScreenName(screenName);\r
+\r
+ User user = userPersistence.findByC_SN(companyId, screenName);\r
+\r
+ return user.getUserId();\r
+ }\r
+\r
+ /**\r
+ * Returns <code>true</code> if the user is a member of the group.\r
+ *\r
+ * @param groupId the primary key of the group\r
+ * @param userId the primary key of the user\r
+ * @return <code>true</code> if the user is a member of the group;\r
+ * <code>false</code> otherwise\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public boolean hasGroupUser(long groupId, long userId)\r
+ throws SystemException {\r
+\r
+ return groupPersistence.containsUser(groupId, userId);\r
+ }\r
+\r
+ /**\r
+ * Returns <code>true</code> if the user is a member of the organization.\r
+ *\r
+ * @param organizationId the primary key of the organization\r
+ * @param userId the primary key of the user\r
+ * @return <code>true</code> if the user is a member of the organization;\r
+ * <code>false</code> otherwise\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public boolean hasOrganizationUser(long organizationId, long userId)\r
+ throws SystemException {\r
+\r
+ return organizationPersistence.containsUser(organizationId, userId);\r
+ }\r
+\r
+ /**\r
+ * Returns <code>true</code> if the password policy has been assigned to the\r
+ * user.\r
+ *\r
+ * @param passwordPolicyId the primary key of the password policy\r
+ * @param userId the primary key of the user\r
+ * @return <code>true</code> if the password policy is assigned to the user;\r
+ * <code>false</code> otherwise\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public boolean hasPasswordPolicyUser(long passwordPolicyId, long userId)\r
+ throws SystemException {\r
+\r
+ return passwordPolicyRelLocalService.hasPasswordPolicyRel(\r
+ passwordPolicyId, User.class.getName(), userId);\r
+ }\r
+\r
+ /**\r
+ * Returns <code>true</code> if the user is a member of the role.\r
+ *\r
+ * @param roleId the primary key of the role\r
+ * @param userId the primary key of the user\r
+ * @return <code>true</code> if the user is a member of the role;\r
+ * <code>false</code> otherwise\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public boolean hasRoleUser(long roleId, long userId)\r
+ throws SystemException {\r
+\r
+ return rolePersistence.containsUser(roleId, userId);\r
+ }\r
+\r
+ /**\r
+ * Returns <code>true</code> if the user has the role with the name,\r
+ * optionally through inheritance.\r
+ *\r
+ * @param companyId the primary key of the role's company\r
+ * @param name the name of the role (must be a regular role, not an\r
+ * organization, site or provider role)\r
+ * @param userId the primary key of the user\r
+ * @param inherited whether to include roles inherited from organizations,\r
+ * sites, etc.\r
+ * @return <code>true</code> if the user has the role; <code>false</code>\r
+ * otherwise\r
+ * @throws PortalException if a role with the name could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public boolean hasRoleUser(\r
+ long companyId, String name, long userId, boolean inherited)\r
+ throws PortalException, SystemException {\r
+\r
+ return roleLocalService.hasUserRole(userId, companyId, name, inherited);\r
+ }\r
+\r
+ /**\r
+ * Returns <code>true</code> if the user is a member of the team.\r
+ *\r
+ * @param teamId the primary key of the team\r
+ * @param userId the primary key of the user\r
+ * @return <code>true</code> if the user is a member of the team;\r
+ * <code>false</code> otherwise\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public boolean hasTeamUser(long teamId, long userId)\r
+ throws SystemException {\r
+\r
+ return teamPersistence.containsUser(teamId, userId);\r
+ }\r
+\r
+ /**\r
+ * Returns <code>true</code> if the user is a member of the user group.\r
+ *\r
+ * @param userGroupId the primary key of the user group\r
+ * @param userId the primary key of the user\r
+ * @return <code>true</code> if the user is a member of the user group;\r
+ * <code>false</code> otherwise\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public boolean hasUserGroupUser(long userGroupId, long userId)\r
+ throws SystemException {\r
+\r
+ return userGroupPersistence.containsUser(userGroupId, userId);\r
+ }\r
+\r
+ /**\r
+ * Returns <code>true</code> if the user's password is expired.\r
+ *\r
+ * @param user the user\r
+ * @return <code>true</code> if the user's password is expired;\r
+ * <code>false</code> otherwise\r
+ * @throws PortalException if the password policy for the user could not be\r
+ * found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public boolean isPasswordExpired(User user)\r
+ throws PortalException, SystemException {\r
+\r
+ PasswordPolicy passwordPolicy = user.getPasswordPolicy();\r
+\r
+ if (passwordPolicy.getExpireable()) {\r
+ Date now = new Date();\r
+\r
+ if (user.getPasswordModifiedDate() == null) {\r
+ user.setPasswordModifiedDate(now);\r
+\r
+ userLocalService.updateUser(user, false);\r
+ }\r
+\r
+ long passwordStartTime = user.getPasswordModifiedDate().getTime();\r
+ long elapsedTime = now.getTime() - passwordStartTime;\r
+\r
+ if (elapsedTime > (passwordPolicy.getMaxAge() * 1000)) {\r
+ return true;\r
+ }\r
+ else {\r
+ return false;\r
+ }\r
+ }\r
+\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * Returns <code>true</code> if the user's password is expiring soon.\r
+ *\r
+ * @param user the user\r
+ * @return <code>true</code> if the user's password is expiring soon;\r
+ * <code>false</code> otherwise\r
+ * @throws PortalException if the password policy for the user could not be\r
+ * found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public boolean isPasswordExpiringSoon(User user)\r
+ throws PortalException, SystemException {\r
+\r
+ PasswordPolicy passwordPolicy = user.getPasswordPolicy();\r
+\r
+ if (passwordPolicy.isExpireable()) {\r
+ Date now = new Date();\r
+\r
+ if (user.getPasswordModifiedDate() == null) {\r
+ user.setPasswordModifiedDate(now);\r
+\r
+ userLocalService.updateUser(user, false);\r
+ }\r
+\r
+ long timeModified = user.getPasswordModifiedDate().getTime();\r
+ long passwordExpiresOn =\r
+ (passwordPolicy.getMaxAge() * 1000) + timeModified;\r
+\r
+ long timeStartWarning =\r
+ passwordExpiresOn - (passwordPolicy.getWarningTime() * 1000);\r
+\r
+ if (now.getTime() > timeStartWarning) {\r
+ return true;\r
+ }\r
+ else {\r
+ return false;\r
+ }\r
+ }\r
+\r
+ return false;\r
+ }\r
+\r
+ public User loadGetDefaultUser(long companyId)\r
+ throws PortalException, SystemException {\r
+\r
+ return userPersistence.findByC_DU(companyId, true);\r
+ }\r
+\r
+ /**\r
+ * Returns an ordered range of all the users who match the keywords and\r
+ * status, without using the indexer. It is preferable to use the indexed\r
+ * version {@link #search(long, String, int, LinkedHashMap, int, int, Sort)}\r
+ * instead of this method wherever possible for performance reasons.\r
+ *\r
+ * <p>\r
+ * Useful when paginating results. Returns a maximum of <code>end -\r
+ * start</code> instances. <code>start</code> and <code>end</code> are not\r
+ * primary keys, they are indexes in the result set. Thus, <code>0</code>\r
+ * refers to the first result in the set. Setting both <code>start</code>\r
+ * and <code>end</code> to {@link\r
+ * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full\r
+ * result set.\r
+ * </p>\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param keywords the keywords (space separated), which may occur in the\r
+ * user's first name, middle name, last name, screen name, or email\r
+ * address\r
+ * @param status the workflow status\r
+ * @param params the finder parameters (optionally <code>null</code>). For\r
+ * more information see {@link\r
+ * com.liferay.portal.service.persistence.UserFinder}.\r
+ * @param start the lower bound of the range of users\r
+ * @param end the upper bound of the range of users (not inclusive)\r
+ * @param obc the comparator to order the users by (optionally\r
+ * <code>null</code>)\r
+ * @return the matching users\r
+ * @throws SystemException if a system exception occurred\r
+ * @see com.liferay.portal.service.persistence.UserFinder\r
+ */\r
+ public List<User> search(\r
+ long companyId, String keywords, int status,\r
+ LinkedHashMap<String, Object> params, int start, int end,\r
+ OrderByComparator obc)\r
+ throws SystemException {\r
+\r
+ return userFinder.findByKeywords(\r
+ companyId, keywords, status, params, start, end, obc);\r
+ }\r
+\r
+ /**\r
+ * Returns an ordered range of all the users who match the keywords and\r
+ * status, using the indexer. It is preferable to use this method instead of\r
+ * the non-indexed version whenever possible for performance reasons.\r
+ *\r
+ * <p>\r
+ * Useful when paginating results. Returns a maximum of <code>end -\r
+ * start</code> instances. <code>start</code> and <code>end</code> are not\r
+ * primary keys, they are indexes in the result set. Thus, <code>0</code>\r
+ * refers to the first result in the set. Setting both <code>start</code>\r
+ * and <code>end</code> to {@link\r
+ * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full\r
+ * result set.\r
+ * </p>\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param keywords the keywords (space separated), which may occur in the\r
+ * user's first name, middle name, last name, screen name, or email\r
+ * address\r
+ * @param status the workflow status\r
+ * @param params the indexer parameters (optionally <code>null</code>). For\r
+ * more information see {@link\r
+ * com.liferay.portlet.usersadmin.util.UserIndexer}.\r
+ * @param start the lower bound of the range of users\r
+ * @param end the upper bound of the range of users (not inclusive)\r
+ * @param sort the field and direction to sort by (optionally\r
+ * <code>null</code>)\r
+ * @return the matching users\r
+ * @throws SystemException if a system exception occurred\r
+ * @see com.liferay.portlet.usersadmin.util.UserIndexer\r
+ */\r
+ public Hits search(\r
+ long companyId, String keywords, int status,\r
+ LinkedHashMap<String, Object> params, int start, int end, Sort sort)\r
+ throws SystemException {\r
+\r
+ String firstName = null;\r
+ String middleName = null;\r
+ String lastName = null;\r
+ String fullName = null;\r
+ String screenName = null;\r
+ String emailAddress = null;\r
+ String street = null;\r
+ String city = null;\r
+ String zip = null;\r
+ String region = null;\r
+ String country = null;\r
+ boolean andOperator = false;\r
+\r
+ if (Validator.isNotNull(keywords)) {\r
+ firstName = keywords;\r
+ middleName = keywords;\r
+ lastName = keywords;\r
+ fullName = keywords;\r
+ screenName = keywords;\r
+ emailAddress = keywords;\r
+ street = keywords;\r
+ city = keywords;\r
+ zip = keywords;\r
+ region = keywords;\r
+ country = keywords;\r
+ }\r
+ else {\r
+ andOperator = true;\r
+ }\r
+\r
+ if (params != null) {\r
+ params.put("keywords", keywords);\r
+ }\r
+\r
+ return search(\r
+ companyId, firstName, middleName, lastName, fullName, screenName,\r
+ emailAddress, street, city, zip, region, country, status, params,\r
+ andOperator, start, end, sort);\r
+ }\r
+\r
+ /**\r
+ * Returns an ordered range of all the users with the status, and whose\r
+ * first name, middle name, last name, screen name, and email address match\r
+ * the keywords specified for them, without using the indexer. It is\r
+ * preferable to use the indexed version {@link #search(long, String,\r
+ * String, String, String, String, int, LinkedHashMap, boolean, int, int,\r
+ * Sort)} instead of this method wherever possible for performance reasons.\r
+ *\r
+ * <p>\r
+ * Useful when paginating results. Returns a maximum of <code>end -\r
+ * start</code> instances. <code>start</code> and <code>end</code> are not\r
+ * primary keys, they are indexes in the result set. Thus, <code>0</code>\r
+ * refers to the first result in the set. Setting both <code>start</code>\r
+ * and <code>end</code> to {@link\r
+ * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full\r
+ * result set.\r
+ * </p>\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param firstName the first name keywords (space separated)\r
+ * @param middleName the middle name keywords\r
+ * @param lastName the last name keywords\r
+ * @param screenName the screen name keywords\r
+ * @param emailAddress the email address keywords\r
+ * @param status the workflow status\r
+ * @param params the finder parameters (optionally <code>null</code>). For\r
+ * more information see {@link\r
+ * com.liferay.portal.service.persistence.UserFinder}.\r
+ * @param andSearch whether every field must match its keywords, or just\r
+ * one field. For example, "users with the first name 'bob' and\r
+ * last name 'smith'" vs "users with the first name 'bob'\r
+ * or the last name 'smith'".\r
+ * @param start the lower bound of the range of users\r
+ * @param end the upper bound of the range of users (not inclusive)\r
+ * @param obc the comparator to order the users by (optionally\r
+ * <code>null</code>)\r
+ * @return the matching users\r
+ * @throws SystemException if a system exception occurred\r
+ * @see com.liferay.portal.service.persistence.UserFinder\r
+ */\r
+ public List<User> search(\r
+ long companyId, String firstName, String middleName,\r
+ String lastName, String screenName, String emailAddress, int status,\r
+ LinkedHashMap<String, Object> params, boolean andSearch, int start,\r
+ int end, OrderByComparator obc)\r
+ throws SystemException {\r
+\r
+ return userFinder.findByC_FN_MN_LN_SN_EA_S(\r
+ companyId, firstName, middleName, lastName, screenName,\r
+ emailAddress, status, params, andSearch, start, end, obc);\r
+ }\r
+\r
+ /**\r
+ * Returns an ordered range of all the users with the status, and whose\r
+ * first name, middle name, last name, screen name, and email address match\r
+ * the keywords specified for them, using the indexer. It is preferable to\r
+ * use this method instead of the non-indexed version whenever possible for\r
+ * performance reasons.\r
+ *\r
+ * <p>\r
+ * Useful when paginating results. Returns a maximum of <code>end -\r
+ * start</code> instances. <code>start</code> and <code>end</code> are not\r
+ * primary keys, they are indexes in the result set. Thus, <code>0</code>\r
+ * refers to the first result in the set. Setting both <code>start</code>\r
+ * and <code>end</code> to {@link\r
+ * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full\r
+ * result set.\r
+ * </p>\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param firstName the first name keywords (space separated)\r
+ * @param middleName the middle name keywords\r
+ * @param lastName the last name keywords\r
+ * @param screenName the screen name keywords\r
+ * @param emailAddress the email address keywords\r
+ * @param status the workflow status\r
+ * @param params the indexer parameters (optionally <code>null</code>). For\r
+ * more information see {@link\r
+ * com.liferay.portlet.usersadmin.util.UserIndexer}.\r
+ * @param andSearch whether every field must match its keywords, or just\r
+ * one field. For example, "users with the first name 'bob' and\r
+ * last name 'smith'" vs "users with the first name 'bob'\r
+ * or the last name 'smith'".\r
+ * @param start the lower bound of the range of users\r
+ * @param end the upper bound of the range of users (not inclusive)\r
+ * @param sort the field and direction to sort by (optionally\r
+ * <code>null</code>)\r
+ * @return the matching users\r
+ * @throws SystemException if a system exception occurred\r
+ * @see com.liferay.portlet.usersadmin.util.UserIndexer\r
+ */\r
+ public Hits search(\r
+ long companyId, String firstName, String middleName,\r
+ String lastName, String screenName, String emailAddress, int status,\r
+ LinkedHashMap<String, Object> params, boolean andSearch, int start,\r
+ int end, Sort sort)\r
+ throws SystemException {\r
+\r
+ return search(\r
+ companyId, firstName, middleName, lastName, null, screenName,\r
+ emailAddress, null, null, null, null, null, status, params,\r
+ andSearch, start, end, sort);\r
+ }\r
+\r
+ /**\r
+ * Returns the number of users who match the keywords and status.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param keywords the keywords (space separated), which may occur in the\r
+ * user's first name, middle name, last name, screen name, or email\r
+ * address\r
+ * @param status the workflow status\r
+ * @param params the finder parameters (optionally <code>null</code>). For\r
+ * more information see {@link\r
+ * com.liferay.portal.service.persistence.UserFinder}.\r
+ * @return the number matching users\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public int searchCount(\r
+ long companyId, String keywords, int status,\r
+ LinkedHashMap<String, Object> params)\r
+ throws SystemException {\r
+\r
+ return userFinder.countByKeywords(companyId, keywords, status, params);\r
+ }\r
+\r
+ /**\r
+ * Returns the number of users with the status, and whose first name, middle\r
+ * name, last name, screen name, and email address match the keywords\r
+ * specified for them.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param firstName the first name keywords (space separated)\r
+ * @param middleName the middle name keywords\r
+ * @param lastName the last name keywords\r
+ * @param screenName the screen name keywords\r
+ * @param emailAddress the email address keywords\r
+ * @param status the workflow status\r
+ * @param params the finder parameters (optionally <code>null</code>). For\r
+ * more information see {@link\r
+ * com.liferay.portal.service.persistence.UserFinder}.\r
+ * @param andSearch whether every field must match its keywords, or just\r
+ * one field. For example, "users with the first name 'bob' and\r
+ * last name 'smith'" vs "users with the first name 'bob'\r
+ * or the last name 'smith'".\r
+ * @return the number of matching users\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public int searchCount(\r
+ long companyId, String firstName, String middleName,\r
+ String lastName, String screenName, String emailAddress, int status,\r
+ LinkedHashMap<String, Object> params, boolean andSearch)\r
+ throws SystemException {\r
+\r
+ return userFinder.countByC_FN_MN_LN_SN_EA_S(\r
+ companyId, firstName, middleName, lastName, screenName,\r
+ emailAddress, status, params, andSearch);\r
+ }\r
+\r
+ /**\r
+ * Sends an email address verification to the user.\r
+ *\r
+ * @param user the verification email recipient\r
+ * @param emailAddress the recipient's email address\r
+ * @param serviceContext the service context. Must set the portal URL, main\r
+ * path, primary key of the layout, remote address, remote host, and\r
+ * agent for the user.\r
+ * @throws PortalException if a portal exception occurred\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void sendEmailAddressVerification(\r
+ User user, String emailAddress, ServiceContext serviceContext)\r
+ throws PortalException, SystemException {\r
+\r
+ if (user.isEmailAddressVerified() &&\r
+ emailAddress.equalsIgnoreCase(user.getEmailAddress())) {\r
+\r
+ return;\r
+ }\r
+\r
+ Ticket ticket = ticketLocalService.addTicket(\r
+ user.getCompanyId(), User.class.getName(), user.getUserId(),\r
+ TicketConstants.TYPE_EMAIL_ADDRESS, emailAddress, null,\r
+ serviceContext);\r
+\r
+ String verifyEmailAddressURL =\r
+ serviceContext.getPortalURL() + serviceContext.getPathMain() +\r
+ "/portal/verify_email_address?ticketKey=" + ticket.getKey();\r
+\r
+ Layout layout = layoutLocalService.getLayout(serviceContext.getPlid());\r
+\r
+ Group group = layout.getGroup();\r
+\r
+ if (!layout.isPrivateLayout() && !group.isUser()) {\r
+ verifyEmailAddressURL += "&p_l_id=" + serviceContext.getPlid();\r
+ }\r
+\r
+ String fromName = PrefsPropsUtil.getString(\r
+ user.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_NAME);\r
+ String fromAddress = PrefsPropsUtil.getString(\r
+ user.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);\r
+\r
+ String toName = user.getFullName();\r
+ String toAddress = emailAddress;\r
+\r
+ String subject = PrefsPropsUtil.getContent(\r
+ user.getCompanyId(), PropsKeys.ADMIN_EMAIL_VERIFICATION_SUBJECT);\r
+\r
+ String body = PrefsPropsUtil.getContent(\r
+ user.getCompanyId(), PropsKeys.ADMIN_EMAIL_VERIFICATION_BODY);\r
+\r
+ SubscriptionSender subscriptionSender = new SubscriptionSender();\r
+\r
+ subscriptionSender.setBody(body);\r
+ subscriptionSender.setCompanyId(user.getCompanyId());\r
+ subscriptionSender.setContextAttributes(\r
+ "[$EMAIL_VERIFICATION_CODE$]", ticket.getKey(),\r
+ "[$EMAIL_VERIFICATION_URL$]", verifyEmailAddressURL,\r
+ "[$REMOTE_ADDRESS$]", serviceContext.getRemoteAddr(),\r
+ "[$REMOTE_HOST$]", serviceContext.getRemoteHost(), "[$USER_ID$]",\r
+ user.getUserId(), "[$USER_SCREENNAME$]", user.getScreenName());\r
+ subscriptionSender.setFrom(fromAddress, fromName);\r
+ subscriptionSender.setHtmlFormat(true);\r
+ subscriptionSender.setMailId("user", user.getUserId());\r
+ subscriptionSender.setServiceContext(serviceContext);\r
+ subscriptionSender.setSubject(subject);\r
+ subscriptionSender.setUserId(user.getUserId());\r
+\r
+ subscriptionSender.addRuntimeSubscribers(toAddress, toName);\r
+\r
+ subscriptionSender.flushNotificationsAsync();\r
+ }\r
+\r
+ /**\r
+ * Sends the password email to the user with the email address. The content\r
+ * of this email can be specified in <code>portal.properties</code> with the\r
+ * <code>admin.email.password</code> keys.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param emailAddress the user's email address\r
+ * @param fromName the name of the individual that the email should be from\r
+ * @param fromAddress the address of the individual that the email should\r
+ * be from\r
+ * @param subject the email subject. If <code>null</code>, the subject\r
+ * specified in <code>portal.properties</code> will be used.\r
+ * @param body the email body. If <code>null</code>, the body specified in\r
+ * <code>portal.properties</code> will be used.\r
+ * @param serviceContext the user's service context\r
+ * @throws PortalException if a user with the email address could not be\r
+ * found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void sendPassword(\r
+ long companyId, String emailAddress, String fromName,\r
+ String fromAddress, String subject, String body,\r
+ ServiceContext serviceContext)\r
+ throws PortalException, SystemException {\r
+\r
+ Company company = companyPersistence.findByPrimaryKey(companyId);\r
+\r
+ if (!company.isSendPassword() && !company.isSendPasswordResetLink()) {\r
+ return;\r
+ }\r
+\r
+ emailAddress = emailAddress.trim().toLowerCase();\r
+\r
+ if (Validator.isNull(emailAddress)) {\r
+ throw new UserEmailAddressException();\r
+ }\r
+\r
+ User user = userPersistence.findByC_EA(companyId, emailAddress);\r
+\r
+ PasswordPolicy passwordPolicy = user.getPasswordPolicy();\r
+\r
+ String newPassword = StringPool.BLANK;\r
+ String passwordResetURL = StringPool.BLANK;\r
+\r
+ if (company.isSendPasswordResetLink()) {\r
+ Date expirationDate = null;\r
+\r
+ if (passwordPolicy.getResetTicketMaxAge() > 0) {\r
+ expirationDate = new Date(\r
+ System.currentTimeMillis() +\r
+ (passwordPolicy.getResetTicketMaxAge() * 1000));\r
+ }\r
+\r
+ Ticket ticket = ticketLocalService.addTicket(\r
+ companyId, User.class.getName(), user.getUserId(),\r
+ TicketConstants.TYPE_PASSWORD, null, expirationDate,\r
+ serviceContext);\r
+\r
+ passwordResetURL =\r
+ serviceContext.getPortalURL() + serviceContext.getPathMain() +\r
+ "/portal/update_password?p_l_id="+\r
+ serviceContext.getPlid() +\r
+ "&ticketKey=" + ticket.getKey();\r
+ }\r
+ else {\r
+ if (!PwdEncryptor.PASSWORDS_ENCRYPTION_ALGORITHM.equals(\r
+ PwdEncryptor.TYPE_NONE)) {\r
+\r
+ newPassword = PwdToolkitUtil.generate(passwordPolicy);\r
+\r
+ boolean passwordReset = false;\r
+\r
+ if (passwordPolicy.getChangeable() &&\r
+ passwordPolicy.getChangeRequired()) {\r
+\r
+ passwordReset = true;\r
+ }\r
+\r
+ user.setPassword(PwdEncryptor.encrypt(newPassword));\r
+ user.setPasswordUnencrypted(newPassword);\r
+ user.setPasswordEncrypted(true);\r
+ user.setPasswordReset(passwordReset);\r
+ user.setPasswordModified(true);\r
+ user.setPasswordModifiedDate(new Date());\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ user.setPasswordModified(false);\r
+ }\r
+ else {\r
+ newPassword = user.getPassword();\r
+ }\r
+ }\r
+\r
+ if (Validator.isNull(fromName)) {\r
+ fromName = PrefsPropsUtil.getString(\r
+ companyId, PropsKeys.ADMIN_EMAIL_FROM_NAME);\r
+ }\r
+\r
+ if (Validator.isNull(fromAddress)) {\r
+ fromAddress = PrefsPropsUtil.getString(\r
+ companyId, PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);\r
+ }\r
+\r
+ String toName = user.getFullName();\r
+ String toAddress = user.getEmailAddress();\r
+\r
+ if (Validator.isNull(subject)) {\r
+ if (company.isSendPasswordResetLink()) {\r
+ subject = PrefsPropsUtil.getContent(\r
+ companyId, PropsKeys.ADMIN_EMAIL_PASSWORD_RESET_SUBJECT);\r
+ }\r
+ else {\r
+ subject = PrefsPropsUtil.getContent(\r
+ companyId, PropsKeys.ADMIN_EMAIL_PASSWORD_SENT_SUBJECT);\r
+ }\r
+ }\r
+\r
+ if (Validator.isNull(body)) {\r
+ if (company.isSendPasswordResetLink()) {\r
+ body = PrefsPropsUtil.getContent(\r
+ companyId, PropsKeys.ADMIN_EMAIL_PASSWORD_RESET_BODY);\r
+ }\r
+ else {\r
+ body = PrefsPropsUtil.getContent(\r
+ companyId, PropsKeys.ADMIN_EMAIL_PASSWORD_SENT_BODY);\r
+ }\r
+ }\r
+\r
+ SubscriptionSender subscriptionSender = new SubscriptionSender();\r
+\r
+ subscriptionSender.setBody(body);\r
+ subscriptionSender.setCompanyId(companyId);\r
+ subscriptionSender.setContextAttributes(\r
+ "[$PASSWORD_RESET_URL$]", passwordResetURL, "[$REMOTE_ADDRESS$]",\r
+ serviceContext.getRemoteAddr(), "[$REMOTE_HOST$]",\r
+ serviceContext.getRemoteHost(), "[$USER_ID$]", user.getUserId(),\r
+ "[$USER_PASSWORD$]", newPassword, "[$USER_SCREENNAME$]",\r
+ user.getScreenName());\r
+ subscriptionSender.setFrom(fromAddress, fromName);\r
+ subscriptionSender.setHtmlFormat(true);\r
+ subscriptionSender.setMailId("user", user.getUserId());\r
+ subscriptionSender.setServiceContext(serviceContext);\r
+ subscriptionSender.setSubject(subject);\r
+ subscriptionSender.setUserId(user.getUserId());\r
+\r
+ subscriptionSender.addRuntimeSubscribers(toAddress, toName);\r
+\r
+ subscriptionSender.flushNotificationsAsync();\r
+ }\r
+\r
+ /**\r
+ * Sets the users in the role, removing and adding users to the role as\r
+ * necessary.\r
+ *\r
+ * @param roleId the primary key of the role\r
+ * @param userIds the primary keys of the users\r
+ * @throws PortalException if a portal exception occurred\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void setRoleUsers(long roleId, long[] userIds)\r
+ throws PortalException, SystemException {\r
+\r
+ rolePersistence.setUsers(roleId, userIds);\r
+\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);\r
+\r
+ indexer.reindex(userIds);\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ /**\r
+ * Sets the users in the user group, removing and adding users to the user\r
+ * group as necessary.\r
+ *\r
+ * @param userGroupId the primary key of the user group\r
+ * @param userIds the primary keys of the users\r
+ * @throws PortalException if a portal exception occurred\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ @SuppressWarnings("deprecation")\r
+ public void setUserGroupUsers(long userGroupId, long[] userIds)\r
+ throws PortalException, SystemException {\r
+\r
+ if (PropsValues.USER_GROUPS_COPY_LAYOUTS_TO_USER_PERSONAL_SITE) {\r
+ userGroupLocalService.copyUserGroupLayouts(userGroupId, userIds);\r
+ }\r
+\r
+ userGroupPersistence.setUsers(userGroupId, userIds);\r
+\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);\r
+\r
+ indexer.reindex(userIds);\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ /**\r
+ * Removes the users from the teams of a group.\r
+ *\r
+ * @param groupId the primary key of the group\r
+ * @param userIds the primary keys of the users\r
+ * @throws PortalException if a portal exception occurred\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void unsetGroupTeamsUsers(long groupId, long[] userIds)\r
+ throws PortalException, SystemException {\r
+\r
+ List<Team> teams = teamPersistence.findByGroupId(groupId);\r
+\r
+ for (Team team : teams) {\r
+ unsetTeamUsers(team.getTeamId(), userIds);\r
+ }\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ /**\r
+ * Removes the users from the group.\r
+ *\r
+ * @param groupId the primary key of the group\r
+ * @param userIds the primary keys of the users\r
+ * @throws PortalException if a portal exception occurred\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void unsetGroupUsers(\r
+ long groupId, long[] userIds, ServiceContext serviceContext)\r
+ throws PortalException, SystemException {\r
+\r
+ userGroupRoleLocalService.deleteUserGroupRoles(userIds, groupId);\r
+\r
+ userLocalService.unsetGroupTeamsUsers(groupId, userIds);\r
+\r
+ groupPersistence.removeUsers(groupId, userIds);\r
+\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);\r
+\r
+ indexer.reindex(userIds);\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ /**\r
+ * Removes the users from the organization.\r
+ *\r
+ * @param organizationId the primary key of the organization\r
+ * @param userIds the primary keys of the users\r
+ * @throws PortalException if a portal exception occurred\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void unsetOrganizationUsers(long organizationId, long[] userIds)\r
+ throws PortalException, SystemException {\r
+\r
+ Organization organization = organizationPersistence.findByPrimaryKey(\r
+ organizationId);\r
+\r
+ Group group = organization.getGroup();\r
+\r
+ long groupId = group.getGroupId();\r
+\r
+ userGroupRoleLocalService.deleteUserGroupRoles(userIds, groupId);\r
+\r
+ organizationPersistence.removeUsers(organizationId, userIds);\r
+\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);\r
+\r
+ indexer.reindex(userIds);\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ /**\r
+ * Removes the users from the password policy.\r
+ *\r
+ * @param passwordPolicyId the primary key of the password policy\r
+ * @param userIds the primary keys of the users\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void unsetPasswordPolicyUsers(long passwordPolicyId, long[] userIds)\r
+ throws SystemException {\r
+\r
+ passwordPolicyRelLocalService.deletePasswordPolicyRels(\r
+ passwordPolicyId, User.class.getName(), userIds);\r
+ }\r
+\r
+ /**\r
+ * Removes the users from the role.\r
+ *\r
+ * @param roleId the primary key of the role\r
+ * @param users the users\r
+ * @throws PortalException if a portal exception occurred\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void unsetRoleUsers(long roleId, List<User> users)\r
+ throws PortalException, SystemException {\r
+\r
+ Role role = rolePersistence.findByPrimaryKey(roleId);\r
+\r
+ if (role.getName().equals(RoleConstants.USER)) {\r
+ return;\r
+ }\r
+\r
+ rolePersistence.removeUsers(roleId, users);\r
+\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);\r
+\r
+ indexer.reindex(users);\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ /**\r
+ * Removes the users from the role.\r
+ *\r
+ * @param roleId the primary key of the role\r
+ * @param userIds the primary keys of the users\r
+ * @throws PortalException if a portal exception occurred\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void unsetRoleUsers(long roleId, long[] userIds)\r
+ throws PortalException, SystemException {\r
+\r
+ Role role = rolePersistence.findByPrimaryKey(roleId);\r
+\r
+ if (role.getName().equals(RoleConstants.USER)) {\r
+ return;\r
+ }\r
+\r
+ rolePersistence.removeUsers(roleId, userIds);\r
+\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);\r
+\r
+ indexer.reindex(userIds);\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ /**\r
+ * Removes the users from the team.\r
+ *\r
+ * @param teamId the primary key of the team\r
+ * @param userIds the primary keys of the users\r
+ * @throws PortalException if a portal exception occurred\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void unsetTeamUsers(long teamId, long[] userIds)\r
+ throws PortalException, SystemException {\r
+\r
+ teamPersistence.removeUsers(teamId, userIds);\r
+\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);\r
+\r
+ indexer.reindex(userIds);\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ /**\r
+ * Removes the users from the user group.\r
+ *\r
+ * @param userGroupId the primary key of the user group\r
+ * @param userIds the primary keys of the users\r
+ * @throws PortalException if a portal exception occurred\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void unsetUserGroupUsers(long userGroupId, long[] userIds)\r
+ throws PortalException, SystemException {\r
+\r
+ userGroupPersistence.removeUsers(userGroupId, userIds);\r
+\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);\r
+\r
+ indexer.reindex(userIds);\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ /**\r
+ * Updates whether the user has agreed to the terms of use.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param agreedToTermsOfUse whether the user has agreet to the terms of\r
+ * use\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updateAgreedToTermsOfUse(\r
+ long userId, boolean agreedToTermsOfUse)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ user.setAgreedToTermsOfUse(agreedToTermsOfUse);\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Updates the user's asset with the new asset categories and tag names,\r
+ * removing and adding asset categories and tag names as necessary.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param user ID the primary key of the user\r
+ * @param assetCategoryIds the primary key's of the new asset categories\r
+ * @param assetTagNames the new asset tag names\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void updateAsset(\r
+ long userId, User user, long[] assetCategoryIds,\r
+ String[] assetTagNames)\r
+ throws PortalException, SystemException {\r
+\r
+ User owner = userPersistence.findByPrimaryKey(userId);\r
+\r
+ Company company = companyPersistence.findByPrimaryKey(\r
+ owner.getCompanyId());\r
+\r
+ Group companyGroup = company.getGroup();\r
+\r
+ assetEntryLocalService.updateEntry(\r
+ userId, companyGroup.getGroupId(), User.class.getName(),\r
+ user.getUserId(), user.getUuid(), 0, assetCategoryIds,\r
+ assetTagNames, false, null, null, null, null, null,\r
+ user.getFullName(), null, null, null, null, 0, 0, null, false);\r
+ }\r
+\r
+ /**\r
+ * Updates the user's creation date.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param createDate the new creation date\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updateCreateDate(long userId, Date createDate)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ user.setCreateDate(createDate);\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Updates the user's email address.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param password the user's password\r
+ * @param emailAddress1 the user's new email address\r
+ * @param emailAddress2 the user's new email address confirmation\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updateEmailAddress(\r
+ long userId, String password, String emailAddress1,\r
+ String emailAddress2)\r
+ throws PortalException, SystemException {\r
+\r
+ emailAddress1 = emailAddress1.trim().toLowerCase();\r
+ emailAddress2 = emailAddress2.trim().toLowerCase();\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ validateEmailAddress(user, emailAddress1, emailAddress2);\r
+\r
+ setEmailAddress(\r
+ user, password, user.getFirstName(), user.getMiddleName(),\r
+ user.getLastName(), emailAddress1);\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Updates the user's email address or sends verification email.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param password the user's password\r
+ * @param emailAddress1 the user's new email address\r
+ * @param emailAddress2 the user's new email address confirmation\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updateEmailAddress(\r
+ long userId, String password, String emailAddress1,\r
+ String emailAddress2, ServiceContext serviceContext)\r
+ throws PortalException, SystemException {\r
+\r
+ emailAddress1 = emailAddress1.trim().toLowerCase();\r
+ emailAddress2 = emailAddress2.trim().toLowerCase();\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ validateEmailAddress(user, emailAddress1, emailAddress2);\r
+\r
+ Company company = companyPersistence.findByPrimaryKey(\r
+ user.getCompanyId());\r
+\r
+ if (!company.isStrangersVerify()) {\r
+ setEmailAddress(\r
+ user, password, user.getFirstName(), user.getMiddleName(),\r
+ user.getLastName(), emailAddress1);\r
+\r
+ userPersistence.update(user, false);\r
+ }\r
+ else {\r
+ sendEmailAddressVerification(user, emailAddress1, serviceContext);\r
+ }\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Updates whether the user has verified email address.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param emailAddressVerified whether the user has verified email address\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updateEmailAddressVerified(\r
+ long userId, boolean emailAddressVerified)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ user.setEmailAddressVerified(emailAddressVerified);\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Updates the user's Facebook ID.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param facebookId the user's new Facebook ID\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updateFacebookId(long userId, long facebookId)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ user.setFacebookId(facebookId);\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Sets the groups the user is in, removing and adding groups as necessary.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param newGroupIds the primary keys of the groups\r
+ * @throws PortalException if a portal exception occurred\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void updateGroups(\r
+ long userId, long[] newGroupIds, ServiceContext serviceContext)\r
+ throws PortalException, SystemException {\r
+\r
+ updateGroups(\r
+ userId, newGroupIds, serviceContext,\r
+ serviceContext.isIndexingEnabled());\r
+ }\r
+\r
+ /**\r
+ * Updates a user account that was automatically created when a guest user\r
+ * participated in an action (e.g. posting a comment) and only provided his\r
+ * name and email address.\r
+ *\r
+ * @param creatorUserId the primary key of the creator\r
+ * @param companyId the primary key of the user's company\r
+ * @param autoPassword whether a password should be automatically generated\r
+ * for the user\r
+ * @param password1 the user's password\r
+ * @param password2 the user's password confirmation\r
+ * @param autoScreenName whether a screen name should be automatically\r
+ * generated for the user\r
+ * @param screenName the user's screen name\r
+ * @param emailAddress the user's email address\r
+ * @param facebookId the user's facebook ID\r
+ * @param openId the user's OpenID\r
+ * @param locale the user's locale\r
+ * @param firstName the user's first name\r
+ * @param middleName the user's middle name\r
+ * @param lastName the user's last name\r
+ * @param prefixId the user's name prefix ID\r
+ * @param suffixId the user's name suffix ID\r
+ * @param male whether the user is male\r
+ * @param birthdayMonth the user's birthday month (0-based, meaning 0 for\r
+ * January)\r
+ * @param birthdayDay the user's birthday day\r
+ * @param birthdayYear the user's birthday year\r
+ * @param jobTitle the user's job title\r
+ * @param updateUserInformation whether to update the user's information\r
+ * @param sendEmail whether to send the user an email notification about\r
+ * their new account\r
+ * @param serviceContext the user's service context (optionally\r
+ * <code>null</code>). Can set expando bridge attributes for the\r
+ * user.\r
+ * @return the user\r
+ * @throws PortalException if the user's information was invalid\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updateIncompleteUser(\r
+ long creatorUserId, long companyId, boolean autoPassword,\r
+ String password1, String password2, boolean autoScreenName,\r
+ String screenName, String emailAddress, long facebookId,\r
+ String openId, Locale locale, String firstName, String middleName,\r
+ String lastName, int prefixId, int suffixId, boolean male,\r
+ int birthdayMonth, int birthdayDay, int birthdayYear,\r
+ String jobTitle, boolean updateUserInformation, boolean sendEmail,\r
+ ServiceContext serviceContext)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = getUserByEmailAddress(companyId, emailAddress);\r
+\r
+ if (user.getStatus() != WorkflowConstants.STATUS_INCOMPLETE) {\r
+ throw new PortalException("Invalid user status");\r
+ }\r
+\r
+ User defaultUser = getDefaultUser(companyId);\r
+\r
+ if (updateUserInformation) {\r
+ autoScreenName = false;\r
+\r
+ if (PrefsPropsUtil.getBoolean(\r
+ companyId,\r
+ PropsKeys.USERS_SCREEN_NAME_ALWAYS_AUTOGENERATE)) {\r
+\r
+ autoScreenName = true;\r
+ }\r
+\r
+ validate(\r
+ companyId, user.getUserId(), autoPassword, password1, password2,\r
+ autoScreenName, screenName, emailAddress, firstName, middleName,\r
+ lastName, null);\r
+\r
+ if (!autoPassword) {\r
+ if (Validator.isNull(password1) ||\r
+ Validator.isNull(password2)) {\r
+ throw new UserPasswordException(\r
+ UserPasswordException.PASSWORD_INVALID);\r
+ }\r
+ }\r
+\r
+ if (autoScreenName) {\r
+ ScreenNameGenerator screenNameGenerator =\r
+ ScreenNameGeneratorFactory.getInstance();\r
+\r
+ try {\r
+ screenName = screenNameGenerator.generate(\r
+ companyId, user.getUserId(), emailAddress);\r
+ }\r
+ catch (Exception e) {\r
+ throw new SystemException(e);\r
+ }\r
+ }\r
+\r
+ FullNameGenerator fullNameGenerator =\r
+ FullNameGeneratorFactory.getInstance();\r
+\r
+ String fullName = fullNameGenerator.getFullName(\r
+ firstName, middleName, lastName);\r
+\r
+ String greeting = LanguageUtil.format(\r
+ locale, "welcome-x", " " + fullName, false);\r
+\r
+ if (Validator.isNotNull(password1)) {\r
+ user.setPassword(PwdEncryptor.encrypt(password1));\r
+ user.setPasswordUnencrypted(password1);\r
+ }\r
+\r
+ user.setPasswordEncrypted(true);\r
+\r
+ PasswordPolicy passwordPolicy = defaultUser.getPasswordPolicy();\r
+\r
+ if (passwordPolicy.isChangeable() &&\r
+ passwordPolicy.isChangeRequired()) {\r
+\r
+ user.setPasswordReset(true);\r
+ }\r
+ else {\r
+ user.setPasswordReset(false);\r
+ }\r
+\r
+ user.setScreenName(screenName);\r
+ user.setFacebookId(facebookId);\r
+ user.setOpenId(openId);\r
+ user.setLanguageId(locale.toString());\r
+ user.setTimeZoneId(defaultUser.getTimeZoneId());\r
+ user.setGreeting(greeting);\r
+ user.setFirstName(firstName);\r
+ user.setMiddleName(middleName);\r
+ user.setLastName(lastName);\r
+ user.setJobTitle(jobTitle);\r
+ user.setExpandoBridgeAttributes(serviceContext);\r
+\r
+ Date birthday = PortalUtil.getDate(\r
+ birthdayMonth, birthdayDay, birthdayYear,\r
+ ContactBirthdayException.class);\r
+\r
+ Contact contact = user.getContact();\r
+\r
+ contact.setFirstName(firstName);\r
+ contact.setMiddleName(middleName);\r
+ contact.setLastName(lastName);\r
+ contact.setPrefixId(prefixId);\r
+ contact.setSuffixId(suffixId);\r
+ contact.setMale(male);\r
+ contact.setBirthday(birthday);\r
+ contact.setJobTitle(jobTitle);\r
+\r
+ contactPersistence.update(contact, false, serviceContext);\r
+\r
+ // Indexer\r
+\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(\r
+ User.class);\r
+\r
+ indexer.reindex(user);\r
+ }\r
+\r
+ user.setStatus(WorkflowConstants.STATUS_DRAFT);\r
+\r
+ userPersistence.update(user, false, serviceContext);\r
+\r
+ // Workflow\r
+\r
+ long workflowUserId = creatorUserId;\r
+\r
+ if (workflowUserId == user.getUserId()) {\r
+ workflowUserId = defaultUser.getUserId();\r
+ }\r
+\r
+ ServiceContext workflowServiceContext = serviceContext;\r
+\r
+ if (workflowServiceContext == null) {\r
+ workflowServiceContext = new ServiceContext();\r
+ }\r
+\r
+ workflowServiceContext.setAttribute("autoPassword", autoPassword);\r
+ workflowServiceContext.setAttribute("sendEmail", sendEmail);\r
+\r
+ WorkflowHandlerRegistryUtil.startWorkflowInstance(\r
+ companyId, workflowUserId, User.class.getName(), user.getUserId(),\r
+ user, workflowServiceContext);\r
+\r
+ return getUserByEmailAddress(companyId, emailAddress);\r
+ }\r
+\r
+ /**\r
+ * Updates the user's job title.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param jobTitle the user's job title\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * or if a contact could not be found matching the user's contact ID\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updateJobTitle(long userId, String jobTitle)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ user.setJobTitle(jobTitle);\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ Contact contact = contactPersistence.findByPrimaryKey(\r
+ user.getContactId());\r
+\r
+ contact.setJobTitle(jobTitle);\r
+\r
+ contactPersistence.update(contact, false);\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Updates the user's last login with the current time and the IP address.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param loginIP the IP address the user logged in from\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updateLastLogin(long userId, String loginIP)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ Date lastLoginDate = user.getLoginDate();\r
+\r
+ if (lastLoginDate == null) {\r
+ lastLoginDate = new Date();\r
+ }\r
+\r
+ user.setLoginDate(new Date());\r
+ user.setLoginIP(loginIP);\r
+ user.setLastLoginDate(lastLoginDate);\r
+ user.setLastLoginIP(user.getLoginIP());\r
+ user.setLastFailedLoginDate(null);\r
+ user.setFailedLoginAttempts(0);\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Updates whether the user is locked out from logging in.\r
+ *\r
+ * @param user the user\r
+ * @param lockout whether the user is locked out\r
+ * @return the user\r
+ * @throws PortalException if a portal exception occurred\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updateLockout(User user, boolean lockout)\r
+ throws PortalException, SystemException {\r
+\r
+ PasswordPolicy passwordPolicy = user.getPasswordPolicy();\r
+\r
+ if ((passwordPolicy == null) || !passwordPolicy.isLockout()) {\r
+ return user;\r
+ }\r
+\r
+ Date lockoutDate = null;\r
+\r
+ if (lockout) {\r
+ lockoutDate = new Date();\r
+ }\r
+\r
+ user.setLockout(lockout);\r
+ user.setLockoutDate(lockoutDate);\r
+\r
+ if (!lockout) {\r
+ user.setLastFailedLoginDate(lockoutDate);\r
+ user.setFailedLoginAttempts(0);\r
+ }\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Updates whether the user is locked out from logging in.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param emailAddress the user's email address\r
+ * @param lockout whether the user is locked out\r
+ * @return the user\r
+ * @throws PortalException if a user with the email address could not be\r
+ * found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updateLockoutByEmailAddress(\r
+ long companyId, String emailAddress, boolean lockout)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = getUserByEmailAddress(companyId, emailAddress);\r
+\r
+ return updateLockout(user, lockout);\r
+ }\r
+\r
+ /**\r
+ * Updates whether the user is locked out from logging in.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param lockout whether the user is locked out\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updateLockoutById(long userId, boolean lockout)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ return updateLockout(user, lockout);\r
+ }\r
+\r
+ /**\r
+ * Updates whether the user is locked out from logging in.\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param screenName the user's screen name\r
+ * @param lockout whether the user is locked out\r
+ * @return the user\r
+ * @throws PortalException if a user with the screen name could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updateLockoutByScreenName(\r
+ long companyId, String screenName, boolean lockout)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = getUserByScreenName(companyId, screenName);\r
+\r
+ return updateLockout(user, lockout);\r
+ }\r
+\r
+ /**\r
+ * Updates the user's modified date.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param modifiedDate the new modified date\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updateModifiedDate(long userId, Date modifiedDate)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ user.setModifiedDate(modifiedDate);\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Updates the user's OpenID.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param openId the new OpenID\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updateOpenId(long userId, String openId)\r
+ throws PortalException, SystemException {\r
+\r
+ openId = openId.trim();\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ user.setOpenId(openId);\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Sets the organizations that the user is in, removing and adding\r
+ * organizations as necessary.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param newOrganizationIds the primary keys of the organizations\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void updateOrganizations(\r
+ long userId, long[] newOrganizationIds,\r
+ ServiceContext serviceContext)\r
+ throws PortalException, SystemException {\r
+\r
+ updateOrganizations(\r
+ userId, newOrganizationIds, serviceContext.isIndexingEnabled());\r
+ }\r
+\r
+ /**\r
+ * Updates the user's password without tracking or validation of the change.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param password1 the user's new password\r
+ * @param password2 the user's new password confirmation\r
+ * @param passwordReset whether the user should be asked to reset their\r
+ * password the next time they log in\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updatePassword(\r
+ long userId, String password1, String password2,\r
+ boolean passwordReset)\r
+ throws PortalException, SystemException {\r
+\r
+ return updatePassword(\r
+ userId, password1, password2, passwordReset, false);\r
+ }\r
+\r
+ /**\r
+ * Updates the user's password, optionally with tracking and validation of\r
+ * the change.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param password1 the user's new password\r
+ * @param password2 the user's new password confirmation\r
+ * @param passwordReset whether the user should be asked to reset their\r
+ * password the next time they login\r
+ * @param silentUpdate whether the password should be updated without being\r
+ * tracked, or validated. Primarily used for password imports.\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updatePassword(\r
+ long userId, String password1, String password2,\r
+ boolean passwordReset, boolean silentUpdate)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ if (!silentUpdate) {\r
+ validatePassword(user.getCompanyId(), userId, password1, password2);\r
+ }\r
+\r
+ String oldEncPwd = user.getPassword();\r
+\r
+ if (!user.isPasswordEncrypted()) {\r
+ oldEncPwd = PwdEncryptor.encrypt(user.getPassword());\r
+ }\r
+\r
+ String newEncPwd = PwdEncryptor.encrypt(password1);\r
+\r
+ if (user.hasCompanyMx()) {\r
+ mailService.updatePassword(user.getCompanyId(), userId, password1);\r
+ }\r
+\r
+ user.setPassword(newEncPwd);\r
+ user.setPasswordUnencrypted(password1);\r
+ user.setPasswordEncrypted(true);\r
+ user.setPasswordReset(passwordReset);\r
+ user.setPasswordModifiedDate(new Date());\r
+ user.setDigest(StringPool.BLANK);\r
+ user.setGraceLoginCount(0);\r
+\r
+ if (!silentUpdate) {\r
+ user.setPasswordModified(true);\r
+ }\r
+\r
+ try {\r
+ userPersistence.update(user, false);\r
+ }\r
+ catch (ModelListenerException mle) {\r
+ String msg = GetterUtil.getString(mle.getCause().getMessage());\r
+\r
+ if (LDAPSettingsUtil.isPasswordPolicyEnabled(user.getCompanyId())) {\r
+ String passwordHistory = PrefsPropsUtil.getString(\r
+ user.getCompanyId(), PropsKeys.LDAP_ERROR_PASSWORD_HISTORY);\r
+\r
+ if (msg.indexOf(passwordHistory) != -1) {\r
+ throw new UserPasswordException(\r
+ UserPasswordException.PASSWORD_ALREADY_USED);\r
+ }\r
+ }\r
+\r
+ throw new UserPasswordException(\r
+ UserPasswordException.PASSWORD_INVALID);\r
+ }\r
+\r
+ if (!silentUpdate) {\r
+ user.setPasswordModified(false);\r
+\r
+ passwordTrackerLocalService.trackPassword(userId, oldEncPwd);\r
+ }\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Updates the user's password with manually input information. This method\r
+ * should only be used when performing maintenance.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param password the user's new password\r
+ * @param passwordEncrypted the user's new encrypted password\r
+ * @param passwordReset whether the user should be asked to reset their\r
+ * password the next time they login\r
+ * @param passwordModifiedDate the new password modified date\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updatePasswordManually(\r
+ long userId, String password, boolean passwordEncrypted,\r
+ boolean passwordReset, Date passwordModifiedDate)\r
+ throws PortalException, SystemException {\r
+\r
+ // This method should only be used to manually massage data\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ user.setPassword(password);\r
+ user.setPasswordEncrypted(passwordEncrypted);\r
+ user.setPasswordReset(passwordReset);\r
+ user.setPasswordModifiedDate(passwordModifiedDate);\r
+ user.setDigest(StringPool.BLANK);\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Updates whether the user should be asked to reset their password the next\r
+ * time they login.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param passwordReset whether the user should be asked to reset their\r
+ * password the next time they login\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updatePasswordReset(long userId, boolean passwordReset)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ user.setPasswordReset(passwordReset);\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Updates the user's portrait image.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param bytes the new portrait image data\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * or if the new portrait was invalid\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updatePortrait(long userId, byte[] bytes)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ long imageMaxSize = PrefsPropsUtil.getLong(\r
+ PropsKeys.USERS_IMAGE_MAX_SIZE);\r
+\r
+ if ((imageMaxSize > 0) &&\r
+ ((bytes == null) || (bytes.length > imageMaxSize))) {\r
+\r
+ throw new UserPortraitSizeException();\r
+ }\r
+\r
+ long portraitId = user.getPortraitId();\r
+\r
+ if (portraitId <= 0) {\r
+ portraitId = counterLocalService.increment();\r
+\r
+ user.setPortraitId(portraitId);\r
+ }\r
+\r
+ try {\r
+ ImageBag imageBag = ImageToolUtil.read(bytes);\r
+\r
+ RenderedImage renderedImage = imageBag.getRenderedImage();\r
+\r
+ if (renderedImage == null) {\r
+ throw new UserPortraitTypeException();\r
+ }\r
+\r
+ renderedImage = ImageToolUtil.scale(\r
+ renderedImage, PropsValues.USERS_IMAGE_MAX_HEIGHT,\r
+ PropsValues.USERS_IMAGE_MAX_WIDTH);\r
+\r
+ String contentType = imageBag.getType();\r
+\r
+ imageLocalService.updateImage(\r
+ portraitId,\r
+ ImageToolUtil.getBytes(renderedImage, contentType));\r
+ }\r
+ catch (IOException ioe) {\r
+ throw new ImageSizeException(ioe);\r
+ }\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Updates the user's password reset question and answer.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param question the user's new password reset question\r
+ * @param answer the user's new password reset answer\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * or if the new question or answer were invalid\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updateReminderQuery(long userId, String question, String answer)\r
+ throws PortalException, SystemException {\r
+\r
+ validateReminderQuery(question, answer) ;\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ user.setReminderQueryQuestion(question);\r
+ user.setReminderQueryAnswer(answer);\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Updates the user's screen name.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param screenName the user's new screen name\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * or if the new screen name was invalid\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updateScreenName(long userId, String screenName)\r
+ throws PortalException, SystemException {\r
+\r
+ // User\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ screenName = getScreenName(screenName);\r
+\r
+ validateScreenName(user.getCompanyId(), userId, screenName);\r
+\r
+ if (!user.getScreenName().equalsIgnoreCase(screenName)) {\r
+ user.setDigest(StringPool.BLANK);\r
+ }\r
+\r
+ user.setScreenName(screenName);\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ // Group\r
+\r
+ Group group = groupLocalService.getUserGroup(\r
+ user.getCompanyId(), userId);\r
+\r
+ group.setFriendlyURL(StringPool.SLASH + screenName);\r
+\r
+ groupPersistence.update(group, false);\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Updates the user's workflow status.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param status the user's new workflow status\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public User updateStatus(long userId, int status)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ user.setStatus(status);\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ reindex(user);\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Updates the user.\r
+ *\r
+ * @param userId the primary key of the user\r
+ * @param oldPassword the user's old password\r
+ * @param newPassword1 the user's new password (optionally\r
+ * <code>null</code>)\r
+ * @param newPassword2 the user's new password confirmation (optionally\r
+ * <code>null</code>)\r
+ * @param passwordReset whether the user should be asked to reset their\r
+ * password the next time they login\r
+ * @param reminderQueryQuestion the user's new password reset question\r
+ * @param reminderQueryAnswer the user's new password reset answer\r
+ * @param screenName the user's new screen name\r
+ * @param emailAddress the user's new email address\r
+ * @param facebookId the user's new Facebook ID\r
+ * @param openId the user's new OpenID\r
+ * @param languageId the user's new language ID\r
+ * @param timeZoneId the user's new time zone ID\r
+ * @param greeting the user's new greeting\r
+ * @param comments the user's new comments\r
+ * @param firstName the user's new first name\r
+ * @param middleName the user's new middle name\r
+ * @param lastName the user's new last name\r
+ * @param prefixId the user's new name prefix ID\r
+ * @param suffixId the user's new name suffix ID\r
+ * @param male whether user is male\r
+ * @param birthdayMonth the user's new birthday month (0-based, meaning 0\r
+ * for January)\r
+ * @param birthdayDay the user's new birthday day\r
+ * @param birthdayYear the user's birthday year\r
+ * @param smsSn the user's new SMS screen name\r
+ * @param aimSn the user's new AIM screen name\r
+ * @param facebookSn the user's new Facebook screen name\r
+ * @param icqSn the user's new ICQ screen name\r
+ * @param jabberSn the user's new Jabber screen name\r
+ * @param msnSn the user's new MSN screen name\r
+ * @param mySpaceSn the user's new MySpace screen name\r
+ * @param skypeSn the user's new Skype screen name\r
+ * @param twitterSn the user's new Twitter screen name\r
+ * @param ymSn the user's new Yahoo! Messenger screen name\r
+ * @param jobTitle the user's new job title\r
+ * @param groupIds the primary keys of the user's groups\r
+ * @param organizationIds the primary keys of the user's organizations\r
+ * @param roleIds the primary keys of the user's roles\r
+ * @param userGroupRoles the user user's group roles\r
+ * @param userGroupIds the primary keys of the user's user groups\r
+ * @param serviceContext the user's service context (optionally\r
+ * <code>null</code>). Can set the universally unique identifier\r
+ * (with the <code>uuid</code> attribute), asset category IDs, asset\r
+ * tag names, and expando bridge attributes for the user.\r
+ * @return the user\r
+ * @throws PortalException if a user with the primary key could not be found\r
+ * or if the new information was invalid\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ @SuppressWarnings("deprecation")\r
+ public User updateUser(\r
+ long userId, String oldPassword, String newPassword1,\r
+ String newPassword2, boolean passwordReset,\r
+ String reminderQueryQuestion, String reminderQueryAnswer,\r
+ String screenName, String emailAddress, long facebookId,\r
+ String openId, String languageId, String timeZoneId,\r
+ String greeting, String comments, String firstName,\r
+ String middleName, String lastName, int prefixId, int suffixId,\r
+ boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,\r
+ String smsSn, String aimSn, String facebookSn, String icqSn,\r
+ String jabberSn, String msnSn, String mySpaceSn, String skypeSn,\r
+ String twitterSn, String ymSn, String jobTitle, long[] groupIds,\r
+ long[] organizationIds, long[] roleIds,\r
+ List<UserGroupRole> userGroupRoles, long[] userGroupIds,\r
+ ServiceContext serviceContext)\r
+ throws PortalException, SystemException {\r
+\r
+ // User\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+ Company company = companyPersistence.findByPrimaryKey(\r
+ user.getCompanyId());\r
+ String password = oldPassword;\r
+ screenName = getScreenName(screenName);\r
+ emailAddress = emailAddress.trim().toLowerCase();\r
+ openId = openId.trim();\r
+ String oldFullName = user.getFullName();\r
+ aimSn = aimSn.trim().toLowerCase();\r
+ facebookSn = facebookSn.trim().toLowerCase();\r
+ icqSn = icqSn.trim().toLowerCase();\r
+ jabberSn = jabberSn.trim().toLowerCase();\r
+ msnSn = msnSn.trim().toLowerCase();\r
+ mySpaceSn = mySpaceSn.trim().toLowerCase();\r
+ skypeSn = skypeSn.trim().toLowerCase();\r
+ twitterSn = twitterSn.trim().toLowerCase();\r
+ ymSn = ymSn.trim().toLowerCase();\r
+ Date now = new Date();\r
+\r
+ EmailAddressGenerator emailAddressGenerator =\r
+ EmailAddressGeneratorFactory.getInstance();\r
+\r
+ if (emailAddressGenerator.isGenerated(emailAddress)) {\r
+ emailAddress = StringPool.BLANK;\r
+ }\r
+\r
+ if (!PropsValues.USERS_EMAIL_ADDRESS_REQUIRED &&\r
+ Validator.isNull(emailAddress)) {\r
+\r
+ emailAddress = emailAddressGenerator.generate(\r
+ user.getCompanyId(), userId);\r
+ }\r
+\r
+ validate(\r
+ userId, screenName, emailAddress, firstName, middleName, lastName,\r
+ smsSn);\r
+\r
+ if (Validator.isNotNull(newPassword1) ||\r
+ Validator.isNotNull(newPassword2)) {\r
+\r
+ user = updatePassword(\r
+ userId, newPassword1, newPassword2, passwordReset);\r
+\r
+ password = newPassword1;\r
+\r
+ user.setDigest(StringPool.BLANK);\r
+ }\r
+\r
+ user.setModifiedDate(now);\r
+\r
+ if (user.getContactId() <= 0) {\r
+ user.setContactId(counterLocalService.increment());\r
+ }\r
+\r
+ user.setPasswordReset(passwordReset);\r
+\r
+ if (Validator.isNotNull(reminderQueryQuestion) &&\r
+ Validator.isNotNull(reminderQueryAnswer)) {\r
+\r
+ user.setReminderQueryQuestion(reminderQueryQuestion);\r
+ user.setReminderQueryAnswer(reminderQueryAnswer);\r
+ }\r
+\r
+ if (!user.getScreenName().equalsIgnoreCase(screenName)) {\r
+ user.setScreenName(screenName);\r
+\r
+ user.setDigest(StringPool.BLANK);\r
+ }\r
+\r
+ boolean sendEmailAddressVerification = false;\r
+\r
+ if (!company.isStrangersVerify()) {\r
+ setEmailAddress(\r
+ user, password, firstName, middleName, lastName, emailAddress);\r
+ }\r
+ else {\r
+ sendEmailAddressVerification = true;\r
+ }\r
+\r
+ if (serviceContext != null) {\r
+ String uuid = serviceContext.getUuid();\r
+\r
+ if (Validator.isNotNull(uuid)) {\r
+ user.setUuid(uuid);\r
+ }\r
+ }\r
+\r
+ user.setFacebookId(facebookId);\r
+ user.setOpenId(openId);\r
+ user.setLanguageId(languageId);\r
+ user.setTimeZoneId(timeZoneId);\r
+ user.setGreeting(greeting);\r
+ user.setComments(comments);\r
+ user.setFirstName(firstName);\r
+ user.setMiddleName(middleName);\r
+ user.setLastName(lastName);\r
+ user.setJobTitle(jobTitle);\r
+ user.setExpandoBridgeAttributes(serviceContext);\r
+\r
+ userPersistence.update(user, false, serviceContext);\r
+\r
+ // Contact\r
+\r
+ Date birthday = PortalUtil.getDate(\r
+ birthdayMonth, birthdayDay, birthdayYear,\r
+ ContactBirthdayException.class);\r
+\r
+ long contactId = user.getContactId();\r
+\r
+ Contact contact = contactPersistence.fetchByPrimaryKey(contactId);\r
+\r
+ if (contact == null) {\r
+ contact = contactPersistence.create(contactId);\r
+\r
+ contact.setCompanyId(user.getCompanyId());\r
+ contact.setUserName(StringPool.BLANK);\r
+ contact.setCreateDate(now);\r
+ contact.setAccountId(company.getAccountId());\r
+ contact.setParentContactId(\r
+ ContactConstants.DEFAULT_PARENT_CONTACT_ID);\r
+ }\r
+\r
+ contact.setModifiedDate(now);\r
+ contact.setFirstName(firstName);\r
+ contact.setMiddleName(middleName);\r
+ contact.setLastName(lastName);\r
+ contact.setPrefixId(prefixId);\r
+ contact.setSuffixId(suffixId);\r
+ contact.setMale(male);\r
+ contact.setBirthday(birthday);\r
+ contact.setSmsSn(smsSn);\r
+ contact.setAimSn(aimSn);\r
+ contact.setFacebookSn(facebookSn);\r
+ contact.setIcqSn(icqSn);\r
+ contact.setJabberSn(jabberSn);\r
+ contact.setMsnSn(msnSn);\r
+ contact.setMySpaceSn(mySpaceSn);\r
+ contact.setSkypeSn(skypeSn);\r
+ contact.setTwitterSn(twitterSn);\r
+ contact.setYmSn(ymSn);\r
+ contact.setJobTitle(jobTitle);\r
+\r
+ contactPersistence.update(contact, false, serviceContext);\r
+\r
+ // Group\r
+\r
+ Group group = groupLocalService.getUserGroup(\r
+ user.getCompanyId(), userId);\r
+\r
+ group.setFriendlyURL(StringPool.SLASH + screenName);\r
+\r
+ groupPersistence.update(group, false);\r
+\r
+ // Groups and organizations\r
+\r
+ updateGroups(userId, groupIds, serviceContext, false);\r
+ updateOrganizations(userId, organizationIds, false);\r
+\r
+ // Roles\r
+\r
+ if (roleIds != null) {\r
+ roleIds = UsersAdminUtil.addRequiredRoles(user, roleIds);\r
+\r
+ userPersistence.setRoles(userId, roleIds);\r
+ }\r
+\r
+ // User group roles\r
+\r
+ updateUserGroupRoles(user, groupIds, organizationIds, userGroupRoles);\r
+\r
+ // User groups\r
+\r
+ if (userGroupIds != null) {\r
+ if (PropsValues.USER_GROUPS_COPY_LAYOUTS_TO_USER_PERSONAL_SITE) {\r
+ userGroupLocalService.copyUserGroupLayouts(\r
+ userGroupIds, userId);\r
+ }\r
+\r
+ userPersistence.setUserGroups(userId, userGroupIds);\r
+ }\r
+\r
+ // Announcements\r
+\r
+ announcementsDeliveryLocalService.getUserDeliveries(user.getUserId());\r
+\r
+ // Asset\r
+\r
+ if (serviceContext != null) {\r
+ updateAsset(\r
+ userId, user, serviceContext.getAssetCategoryIds(),\r
+ serviceContext.getAssetTagNames());\r
+ }\r
+\r
+ // Message boards\r
+\r
+ if (GetterUtil.getBoolean(\r
+ PropsKeys.USERS_UPDATE_USER_NAME + MBMessage.class.getName()) &&\r
+ !oldFullName.equals(user.getFullName())) {\r
+\r
+ mbMessageLocalService.updateUserName(userId, user.getFullName());\r
+ }\r
+\r
+ // Indexer\r
+\r
+ if ((serviceContext == null) || serviceContext.isIndexingEnabled()) {\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(\r
+ User.class);\r
+\r
+ indexer.reindex(user);\r
+ }\r
+\r
+ // Email address verification\r
+\r
+ if ((serviceContext != null) && sendEmailAddressVerification) {\r
+ sendEmailAddressVerification(user, emailAddress, serviceContext);\r
+ }\r
+\r
+ // Permission cache\r
+\r
+ PermissionCacheUtil.clearCache();\r
+\r
+ return user;\r
+ }\r
+\r
+ /**\r
+ * Verifies the email address of the ticket.\r
+ *\r
+ * @param ticketKey the ticket key\r
+ * @throws PortalException if a ticket matching the ticket key could not be\r
+ * found, if the ticket has expired, if the ticket is an email\r
+ * address ticket, or if the email address is invalid\r
+ * @throws SystemException if a system exception occurred\r
+ */\r
+ public void verifyEmailAddress(String ticketKey)\r
+ throws PortalException, SystemException {\r
+\r
+ Ticket ticket = ticketLocalService.getTicket(ticketKey);\r
+\r
+ if (ticket.isExpired() ||\r
+ (ticket.getType() != TicketConstants.TYPE_EMAIL_ADDRESS)) {\r
+\r
+ throw new NoSuchTicketException();\r
+ }\r
+\r
+ User user = userPersistence.findByPrimaryKey(ticket.getClassPK());\r
+\r
+ String emailAddress = ticket.getExtraInfo();\r
+\r
+ emailAddress = emailAddress.toLowerCase().trim();\r
+\r
+ if (!emailAddress.equals(user.getEmailAddress())) {\r
+ if (userPersistence.fetchByC_EA(\r
+ user.getCompanyId(), emailAddress) != null) {\r
+\r
+ throw new DuplicateUserEmailAddressException();\r
+ }\r
+\r
+ setEmailAddress(\r
+ user, StringPool.BLANK, user.getFirstName(),\r
+ user.getMiddleName(), user.getLastName(), emailAddress);\r
+ }\r
+\r
+ user.setEmailAddressVerified(true);\r
+\r
+ userPersistence.update(user, false);\r
+\r
+ ticketLocalService.deleteTicket(ticket);\r
+ }\r
+\r
+ /**\r
+ * Attempts to authenticate the user by their login and password, while\r
+ * using the AuthPipeline.\r
+ *\r
+ * <p>\r
+ * Authentication type specifies what <code>login</code> contains.The valid\r
+ * values are:\r
+ * </p>\r
+ *\r
+ * <ul>\r
+ * <li>\r
+ * <code>CompanyConstants.AUTH_TYPE_EA</code> - <code>login</code> is the\r
+ * user's email address\r
+ * </li>\r
+ * <li>\r
+ * <code>CompanyConstants.AUTH_TYPE_SN</code> - <code>login</code> is the\r
+ * user's screen name\r
+ * </li>\r
+ * <li>\r
+ * <code>CompanyConstants.AUTH_TYPE_ID</code> - <code>login</code> is the\r
+ * user's primary key\r
+ * </li>\r
+ * </ul>\r
+ *\r
+ * @param companyId the primary key of the user's company\r
+ * @param login either the user's email address, screen name, or primary\r
+ * key depending on the value of <code>authType</code>\r
+ * @param password the user's password\r
+ * @param authType the type of authentication to perform\r
+ * @param headerMap the header map from the authentication request\r
+ * @param parameterMap the parameter map from the authentication request\r
+ * @param resultsMap the map of authentication results (may be nil). After\r
+ * a succesful authentication the user's primary key will be placed\r
+ * under the key <code>userId</code>.\r
+ * @return the authentication status. This can be {@link\r
+ * com.liferay.portal.security.auth.Authenticator#FAILURE}\r
+ * indicating that the user's credentials are invalid, {@link\r
+ * com.liferay.portal.security.auth.Authenticator#SUCCESS}\r
+ * indicating a successful login, or {@link\r
+ * com.liferay.portal.security.auth.Authenticator#DNE} indicating\r
+ * that a user with that login does not exist.\r
+ * @throws PortalException if <code>login</code> or <code>password</code>\r
+ * was <code>null</code>\r
+ * @throws SystemException if a system exception occurred\r
+ * @see com.liferay.portal.security.auth.AuthPipeline\r
+ */\r
+ protected int authenticate(\r
+ long companyId, String login, String password, String authType,\r
+ Map<String, String[]> headerMap, Map<String, String[]> parameterMap,\r
+ Map<String, Object> resultsMap)\r
+ throws PortalException, SystemException {\r
+\r
+ if (PropsValues.AUTH_LOGIN_DISABLED) {\r
+ return Authenticator.FAILURE;\r
+ }\r
+\r
+ login = login.trim().toLowerCase();\r
+\r
+ long userId = GetterUtil.getLong(login);\r
+\r
+ // User input validation\r
+\r
+ if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {\r
+ if (Validator.isNull(login)) {\r
+ throw new UserEmailAddressException();\r
+ }\r
+ }\r
+ else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {\r
+ if (Validator.isNull(login)) {\r
+ throw new UserScreenNameException();\r
+ }\r
+ }\r
+ else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {\r
+ if (Validator.isNull(login)) {\r
+ throw new UserIdException();\r
+ }\r
+ }\r
+\r
+ if (Validator.isNull(password)) {\r
+ throw new UserPasswordException(\r
+ UserPasswordException.PASSWORD_INVALID);\r
+ }\r
+\r
+ int authResult = Authenticator.FAILURE;\r
+\r
+ // Pre-authentication pipeline\r
+\r
+ if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {\r
+ authResult = AuthPipeline.authenticateByEmailAddress(\r
+ PropsKeys.AUTH_PIPELINE_PRE, companyId, login, password,\r
+ headerMap, parameterMap);\r
+ }\r
+ else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {\r
+ authResult = AuthPipeline.authenticateByScreenName(\r
+ PropsKeys.AUTH_PIPELINE_PRE, companyId, login, password,\r
+ headerMap, parameterMap);\r
+ }\r
+ else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {\r
+ authResult = AuthPipeline.authenticateByUserId(\r
+ PropsKeys.AUTH_PIPELINE_PRE, companyId, userId, password,\r
+ headerMap, parameterMap);\r
+ }\r
+\r
+ // Get user\r
+\r
+ User user = null;\r
+\r
+ try {\r
+ if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {\r
+ user = userPersistence.findByC_EA(companyId, login);\r
+ }\r
+ else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {\r
+ user = userPersistence.findByC_SN(companyId, login);\r
+ }\r
+ else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {\r
+ user = userPersistence.findByC_U(\r
+ companyId, GetterUtil.getLong(login));\r
+ }\r
+ }\r
+ catch (NoSuchUserException nsue) {\r
+ return Authenticator.DNE;\r
+ }\r
+\r
+ if (user.isDefaultUser()) {\r
+ if (_log.isInfoEnabled()) {\r
+ _log.info("Authentication is disabled for the default user");\r
+ }\r
+\r
+ return Authenticator.DNE;\r
+ }\r
+ else if (!user.isActive()) {\r
+ if (_log.isInfoEnabled()) {\r
+ _log.info(\r
+ "Authentication is disabled for inactive user " +\r
+ user.getUserId());\r
+ }\r
+\r
+ return Authenticator.FAILURE;\r
+ }\r
+\r
+ if (!user.isPasswordEncrypted()) {\r
+ user.setPassword(PwdEncryptor.encrypt(user.getPassword()));\r
+ user.setPasswordEncrypted(true);\r
+\r
+ userPersistence.update(user, false);\r
+ }\r
+\r
+ // Check password policy to see if the is account locked out or if the\r
+ // password is expired\r
+\r
+ checkLockout(user);\r
+\r
+ checkPasswordExpired(user);\r
+\r
+ // Authenticate against the User_ table\r
+\r
+ if ((authResult == Authenticator.SUCCESS) &&\r
+ PropsValues.AUTH_PIPELINE_ENABLE_LIFERAY_CHECK) {\r
+\r
+ boolean authenticated = PwdAuthenticator.authenticate(\r
+ login, password, user.getPassword());\r
+\r
+ if (authenticated) {\r
+ authResult = Authenticator.SUCCESS;\r
+ }\r
+ else {\r
+ authResult = Authenticator.FAILURE;\r
+ }\r
+ }\r
+\r
+ // Post-authentication pipeline\r
+\r
+ if (authResult == Authenticator.SUCCESS) {\r
+ if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {\r
+ authResult = AuthPipeline.authenticateByEmailAddress(\r
+ PropsKeys.AUTH_PIPELINE_POST, companyId, login, password,\r
+ headerMap, parameterMap);\r
+ }\r
+ else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {\r
+ authResult = AuthPipeline.authenticateByScreenName(\r
+ PropsKeys.AUTH_PIPELINE_POST, companyId, login, password,\r
+ headerMap, parameterMap);\r
+ }\r
+ else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {\r
+ authResult = AuthPipeline.authenticateByUserId(\r
+ PropsKeys.AUTH_PIPELINE_POST, companyId, userId, password,\r
+ headerMap, parameterMap);\r
+ }\r
+ }\r
+\r
+ if (authResult == Authenticator.SUCCESS) {\r
+ if (resultsMap != null) {\r
+ resultsMap.put("userId", user.getUserId());\r
+ }\r
+\r
+ // Update digest\r
+\r
+ boolean updateDigest = true;\r
+\r
+ if (PropsValues.AUTH_PIPELINE_ENABLE_LIFERAY_CHECK) {\r
+ if (Validator.isNotNull(user.getDigest())) {\r
+ updateDigest = false;\r
+ }\r
+ }\r
+\r
+ if (updateDigest) {\r
+ String digest = user.getDigest(password);\r
+\r
+ user.setDigest(digest);\r
+\r
+ userPersistence.update(user, false);\r
+ }\r
+ }\r
+\r
+ // Execute code triggered by authentication failure\r
+\r
+ if (authResult == Authenticator.FAILURE) {\r
+ try {\r
+ if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {\r
+ AuthPipeline.onFailureByEmailAddress(\r
+ PropsKeys.AUTH_FAILURE, companyId, login, headerMap,\r
+ parameterMap);\r
+ }\r
+ else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {\r
+ AuthPipeline.onFailureByScreenName(\r
+ PropsKeys.AUTH_FAILURE, companyId, login, headerMap,\r
+ parameterMap);\r
+ }\r
+ else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {\r
+ AuthPipeline.onFailureByUserId(\r
+ PropsKeys.AUTH_FAILURE, companyId, userId, headerMap,\r
+ parameterMap);\r
+ }\r
+\r
+ try {\r
+ user = userPersistence.findByPrimaryKey(user.getUserId());\r
+ }\r
+ catch (NoSuchUserException nsue) {\r
+ return Authenticator.DNE;\r
+ }\r
+\r
+ // Let LDAP handle max failure event\r
+\r
+ if (!LDAPSettingsUtil.isPasswordPolicyEnabled(\r
+ user.getCompanyId())) {\r
+\r
+ PasswordPolicy passwordPolicy = user.getPasswordPolicy();\r
+\r
+ int failedLoginAttempts = user.getFailedLoginAttempts();\r
+ int maxFailures = passwordPolicy.getMaxFailure();\r
+\r
+ if ((failedLoginAttempts >= maxFailures) &&\r
+ (maxFailures != 0)) {\r
+\r
+ if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {\r
+ AuthPipeline.onMaxFailuresByEmailAddress(\r
+ PropsKeys.AUTH_MAX_FAILURES, companyId, login,\r
+ headerMap, parameterMap);\r
+ }\r
+ else if (authType.equals(\r
+ CompanyConstants.AUTH_TYPE_SN)) {\r
+\r
+ AuthPipeline.onMaxFailuresByScreenName(\r
+ PropsKeys.AUTH_MAX_FAILURES, companyId, login,\r
+ headerMap, parameterMap);\r
+ }\r
+ else if (authType.equals(\r
+ CompanyConstants.AUTH_TYPE_ID)) {\r
+\r
+ AuthPipeline.onMaxFailuresByUserId(\r
+ PropsKeys.AUTH_MAX_FAILURES, companyId, userId,\r
+ headerMap, parameterMap);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ catch (Exception e) {\r
+ _log.error(e, e);\r
+ }\r
+ }\r
+\r
+ return authResult;\r
+ }\r
+\r
+ protected String getScreenName(String screenName) {\r
+ return StringUtil.lowerCase(StringUtil.trim(screenName));\r
+ }\r
+\r
+ protected long[] getUserIds(List<User> users) {\r
+ long[] userIds = new long[users.size()];\r
+\r
+ for (int i = 0; i < users.size(); i++) {\r
+ User user = users.get(i);\r
+\r
+ userIds[i] = user.getUserId();\r
+ }\r
+\r
+ return userIds;\r
+ }\r
+\r
+ protected void reindex(final User user) {\r
+ final Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(\r
+ User.class);\r
+\r
+ Callable<Void> callable = new Callable<Void>() {\r
+\r
+ public Void call() throws Exception {\r
+ indexer.reindex(user);\r
+\r
+ return null;\r
+ }\r
+\r
+ };\r
+\r
+ TransactionCommitCallbackUtil.registerCallback(callable);\r
+ }\r
+\r
+ protected Hits search(\r
+ long companyId, String firstName, String middleName,\r
+ String lastName, String fullName, String screenName,\r
+ String emailAddress, String street, String city, String zip,\r
+ String region, String country, int status,\r
+ LinkedHashMap<String, Object> params, boolean andSearch, int start,\r
+ int end, Sort sort)\r
+ throws SystemException {\r
+\r
+ try {\r
+ SearchContext searchContext = new SearchContext();\r
+\r
+ searchContext.setAndSearch(andSearch);\r
+\r
+ Map<String, Serializable> attributes =\r
+ new HashMap<String, Serializable>();\r
+\r
+ attributes.put("city", city);\r
+ attributes.put("country", country);\r
+ attributes.put("emailAddress", emailAddress);\r
+ attributes.put("firstName", firstName);\r
+ attributes.put("fullName", fullName);\r
+ attributes.put("lastName", lastName);\r
+ attributes.put("middleName", middleName);\r
+ attributes.put("params", params);\r
+ attributes.put("region", region);\r
+ attributes.put("screenName", screenName);\r
+ attributes.put("street", street);\r
+ attributes.put("status", status);\r
+ attributes.put("zip", zip);\r
+\r
+ searchContext.setAttributes(attributes);\r
+\r
+ searchContext.setCompanyId(companyId);\r
+ searchContext.setEnd(end);\r
+\r
+ if (params != null) {\r
+ String keywords = (String)params.remove("keywords");\r
+\r
+ if (Validator.isNotNull(keywords)) {\r
+ searchContext.setKeywords(keywords);\r
+ }\r
+ }\r
+\r
+ QueryConfig queryConfig = new QueryConfig();\r
+\r
+ queryConfig.setHighlightEnabled(false);\r
+ queryConfig.setScoreEnabled(false);\r
+\r
+ searchContext.setQueryConfig(queryConfig);\r
+\r
+ searchContext.setSorts(new Sort[] {sort});\r
+ searchContext.setStart(start);\r
+\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(\r
+ User.class);\r
+\r
+ return indexer.search(searchContext);\r
+ }\r
+ catch (Exception e) {\r
+ throw new SystemException(e);\r
+ }\r
+ }\r
+\r
+ protected void sendEmail(\r
+ User user, String password, ServiceContext serviceContext)\r
+ throws SystemException {\r
+\r
+ if (!PrefsPropsUtil.getBoolean(\r
+ user.getCompanyId(),\r
+ PropsKeys.ADMIN_EMAIL_USER_ADDED_ENABLED)) {\r
+\r
+ return;\r
+ }\r
+\r
+ String fromName = PrefsPropsUtil.getString(\r
+ user.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_NAME);\r
+ String fromAddress = PrefsPropsUtil.getString(\r
+ user.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);\r
+\r
+ String toName = user.getFullName();\r
+ String toAddress = user.getEmailAddress();\r
+\r
+ String subject = PrefsPropsUtil.getContent(\r
+ user.getCompanyId(), PropsKeys.ADMIN_EMAIL_USER_ADDED_SUBJECT);\r
+\r
+ String body = null;\r
+\r
+ if (Validator.isNotNull(password)) {\r
+ body = PrefsPropsUtil.getContent(\r
+ user.getCompanyId(), PropsKeys.ADMIN_EMAIL_USER_ADDED_BODY);\r
+ }\r
+ else {\r
+ body = PrefsPropsUtil.getContent(\r
+ user.getCompanyId(),\r
+ PropsKeys.ADMIN_EMAIL_USER_ADDED_NO_PASSWORD_BODY);\r
+ }\r
+\r
+ SubscriptionSender subscriptionSender = new SubscriptionSender();\r
+\r
+ subscriptionSender.setBody(body);\r
+ subscriptionSender.setCompanyId(user.getCompanyId());\r
+ subscriptionSender.setContextAttributes(\r
+ "[$USER_ID$]", user.getUserId(), "[$USER_PASSWORD$]", password,\r
+ "[$USER_SCREENNAME$]", user.getScreenName());\r
+ subscriptionSender.setFrom(fromAddress, fromName);\r
+ subscriptionSender.setHtmlFormat(true);\r
+ subscriptionSender.setMailId("user", user.getUserId());\r
+ subscriptionSender.setServiceContext(serviceContext);\r
+ subscriptionSender.setSubject(subject);\r
+ subscriptionSender.setUserId(user.getUserId());\r
+\r
+ subscriptionSender.addRuntimeSubscribers(toAddress, toName);\r
+\r
+ subscriptionSender.flushNotificationsAsync();\r
+ }\r
+\r
+ protected void setEmailAddress(\r
+ User user, String password, String firstName, String middleName,\r
+ String lastName, String emailAddress)\r
+ throws PortalException, SystemException {\r
+\r
+ if (emailAddress.equalsIgnoreCase(user.getEmailAddress())) {\r
+ return;\r
+ }\r
+\r
+ long userId = user.getUserId();\r
+\r
+ // test@test.com -> test@liferay.com\r
+\r
+ if (!user.hasCompanyMx() && user.hasCompanyMx(emailAddress) &&\r
+ Validator.isNotNull(password)) {\r
+\r
+ mailService.addUser(\r
+ user.getCompanyId(), userId, password, firstName, middleName,\r
+ lastName, emailAddress);\r
+ }\r
+\r
+ // test@liferay.com -> bob@liferay.com\r
+\r
+ else if (user.hasCompanyMx() && user.hasCompanyMx(emailAddress)) {\r
+ mailService.updateEmailAddress(\r
+ user.getCompanyId(), userId, emailAddress);\r
+ }\r
+\r
+ // test@liferay.com -> test@test.com\r
+\r
+ else if (user.hasCompanyMx() && !user.hasCompanyMx(emailAddress)) {\r
+ mailService.deleteEmailAddress(user.getCompanyId(), userId);\r
+ }\r
+\r
+ user.setEmailAddress(emailAddress);\r
+ user.setDigest(StringPool.BLANK);\r
+ }\r
+\r
+ protected void updateGroups(\r
+ long userId, long[] newGroupIds, ServiceContext serviceContext,\r
+ boolean indexingEnabled)\r
+ throws PortalException, SystemException {\r
+\r
+ if (newGroupIds == null) {\r
+ return;\r
+ }\r
+\r
+ List<Group> oldGroups = userPersistence.getGroups(userId);\r
+\r
+ List<Long> oldGroupIds = new ArrayList<Long>(oldGroups.size());\r
+\r
+ for (Group oldGroup : oldGroups) {\r
+ long oldGroupId = oldGroup.getGroupId();\r
+\r
+ oldGroupIds.add(oldGroupId);\r
+\r
+ if (!ArrayUtil.contains(newGroupIds, oldGroupId)) {\r
+ unsetGroupUsers(\r
+ oldGroupId, new long[] {userId}, serviceContext);\r
+ }\r
+ }\r
+\r
+ for (long newGroupId : newGroupIds) {\r
+ if (!oldGroupIds.contains(newGroupId)) {\r
+ addGroupUsers(newGroupId, new long[] {userId});\r
+ }\r
+ }\r
+\r
+ if (indexingEnabled) {\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(\r
+ User.class);\r
+\r
+ indexer.reindex(new long[] {userId});\r
+ }\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ protected void updateOrganizations(\r
+ long userId, long[] newOrganizationIds, boolean indexingEnabled)\r
+ throws PortalException, SystemException {\r
+\r
+ if (newOrganizationIds == null) {\r
+ return;\r
+ }\r
+\r
+ List<Organization> oldOrganizations = userPersistence.getOrganizations(\r
+ userId);\r
+\r
+ List<Long> oldOrganizationIds = new ArrayList<Long>(\r
+ oldOrganizations.size());\r
+\r
+ for (Organization oldOrganization : oldOrganizations) {\r
+ long oldOrganizationId = oldOrganization.getOrganizationId();\r
+\r
+ oldOrganizationIds.add(oldOrganizationId);\r
+\r
+ if (!ArrayUtil.contains(newOrganizationIds, oldOrganizationId)) {\r
+ unsetOrganizationUsers(oldOrganizationId, new long[] {userId});\r
+ }\r
+ }\r
+\r
+ for (long newOrganizationId : newOrganizationIds) {\r
+ if (!oldOrganizationIds.contains(newOrganizationId)) {\r
+ addOrganizationUsers(newOrganizationId, new long[] {userId});\r
+ }\r
+ }\r
+\r
+ if (indexingEnabled) {\r
+ Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(\r
+ User.class);\r
+\r
+ indexer.reindex(new long[] {userId});\r
+ }\r
+\r
+ PermissionCacheUtil.clearCache();\r
+ }\r
+\r
+ protected void updateUserGroupRoles(\r
+ User user, long[] groupIds, long[] organizationIds,\r
+ List<UserGroupRole> userGroupRoles)\r
+ throws PortalException, SystemException {\r
+\r
+ if (userGroupRoles == null) {\r
+ return;\r
+ }\r
+\r
+ List<UserGroupRole> previousUserGroupRoles =\r
+ userGroupRolePersistence.findByUserId(user.getUserId());\r
+\r
+ for (UserGroupRole userGroupRole : previousUserGroupRoles) {\r
+ if (userGroupRoles.contains(userGroupRole)) {\r
+ userGroupRoles.remove(userGroupRole);\r
+ }\r
+ else {\r
+ userGroupRoleLocalService.deleteUserGroupRole(userGroupRole);\r
+ }\r
+ }\r
+\r
+ long[] validGroupIds = null;\r
+\r
+ if (groupIds != null) {\r
+ validGroupIds = ArrayUtil.clone(groupIds);\r
+ }\r
+ else {\r
+ validGroupIds = user.getGroupIds();\r
+ }\r
+\r
+ if (organizationIds == null) {\r
+ organizationIds = user.getOrganizationIds();\r
+ }\r
+\r
+ long[] organizationGroupIds = new long[organizationIds.length];\r
+\r
+ for (int i = 0; i < organizationIds.length; i++) {\r
+ long organizationId = organizationIds[i];\r
+\r
+ Organization organization =\r
+ organizationPersistence.findByPrimaryKey(organizationId);\r
+\r
+ Group organizationGroup = organization.getGroup();\r
+\r
+ organizationGroupIds[i] = organizationGroup.getGroupId();\r
+ }\r
+\r
+ validGroupIds = ArrayUtil.append(validGroupIds, organizationGroupIds);\r
+\r
+ Arrays.sort(validGroupIds);\r
+\r
+ for (UserGroupRole userGroupRole : userGroupRoles) {\r
+ if (Arrays.binarySearch(\r
+ validGroupIds, userGroupRole.getGroupId()) >= 0) {\r
+\r
+ userGroupRoleLocalService.addUserGroupRole(userGroupRole);\r
+ }\r
+ }\r
+ }\r
+\r
+ protected void validate(\r
+ long companyId, long userId, boolean autoPassword, String password1,\r
+ String password2, boolean autoScreenName, String screenName,\r
+ String emailAddress, String firstName, String middleName,\r
+ String lastName, long[] organizationIds)\r
+ throws PortalException, SystemException {\r
+\r
+ Company company = companyPersistence.findByPrimaryKey(companyId);\r
+\r
+ if (company.isSystem()) {\r
+ return;\r
+ }\r
+\r
+ if ((company.getMaxUsers() > 0) &&\r
+ (company.getMaxUsers() <=\r
+ searchCount(\r
+ companyId, null, WorkflowConstants.STATUS_APPROVED,\r
+ null))) {\r
+\r
+ throw new CompanyMaxUsersException();\r
+ }\r
+\r
+ if (!autoScreenName) {\r
+ validateScreenName(companyId, userId, screenName);\r
+ }\r
+\r
+ if (!autoPassword) {\r
+ PasswordPolicy passwordPolicy =\r
+ passwordPolicyLocalService.getDefaultPasswordPolicy(companyId);\r
+\r
+ PwdToolkitUtil.validate(\r
+ companyId, 0, password1, password2, passwordPolicy);\r
+ }\r
+\r
+ validateEmailAddress(companyId, emailAddress);\r
+\r
+ if (Validator.isNotNull(emailAddress)) {\r
+ User user = userPersistence.fetchByC_EA(companyId, emailAddress);\r
+\r
+ if ((user != null) && (user.getUserId() != userId)) {\r
+ throw new DuplicateUserEmailAddressException();\r
+ }\r
+ }\r
+\r
+ validateFullName(companyId, firstName, middleName, lastName);\r
+\r
+ if (organizationIds != null) {\r
+ for (long organizationId : organizationIds) {\r
+ Organization organization =\r
+ organizationPersistence.fetchByPrimaryKey(organizationId);\r
+\r
+ if (organization == null) {\r
+ throw new NoSuchOrganizationException();\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ protected void validate(\r
+ long userId, String screenName, String emailAddress,\r
+ String firstName, String middleName, String lastName, String smsSn)\r
+ throws PortalException, SystemException {\r
+\r
+ User user = userPersistence.findByPrimaryKey(userId);\r
+\r
+ if (!user.getScreenName().equalsIgnoreCase(screenName)) {\r
+ validateScreenName(user.getCompanyId(), userId, screenName);\r
+ }\r
+\r
+ validateEmailAddress(user.getCompanyId(), emailAddress);\r
+\r
+ if (!user.isDefaultUser()) {\r
+ if (Validator.isNotNull(emailAddress) &&\r
+ !user.getEmailAddress().equalsIgnoreCase(emailAddress)) {\r
+\r
+ if (userPersistence.fetchByC_EA(\r
+ user.getCompanyId(), emailAddress) != null) {\r
+\r
+ throw new DuplicateUserEmailAddressException();\r
+ }\r
+ }\r
+\r
+ validateFullName(\r
+ user.getCompanyId(), firstName, middleName, lastName);\r
+ }\r
+\r
+ if (Validator.isNotNull(smsSn) && !Validator.isEmailAddress(smsSn)) {\r
+ throw new UserSmsException();\r
+ }\r
+ }\r
+\r
+ protected void validateEmailAddress(long companyId, String emailAddress)\r
+ throws PortalException, SystemException {\r
+\r
+ if (Validator.isNull(emailAddress) &&\r
+ !PropsValues.USERS_EMAIL_ADDRESS_REQUIRED) {\r
+\r
+ return;\r
+ }\r
+\r
+ EmailAddressValidator emailAddressValidator =\r
+ EmailAddressValidatorFactory.getInstance();\r
+\r
+ if (!emailAddressValidator.validate(companyId, emailAddress)) {\r
+ throw new UserEmailAddressException();\r
+ }\r
+\r
+ String pop3User = PrefsPropsUtil.getString(\r
+ PropsKeys.MAIL_SESSION_MAIL_POP3_USER,\r
+ PropsValues.MAIL_SESSION_MAIL_POP3_USER);\r
+\r
+ if (emailAddress.equalsIgnoreCase(pop3User)) {\r
+ throw new ReservedUserEmailAddressException();\r
+ }\r
+\r
+ String[] reservedEmailAddresses = PrefsPropsUtil.getStringArray(\r
+ companyId, PropsKeys.ADMIN_RESERVED_EMAIL_ADDRESSES,\r
+ StringPool.NEW_LINE, PropsValues.ADMIN_RESERVED_EMAIL_ADDRESSES);\r
+\r
+ for (String reservedEmailAddress : reservedEmailAddresses) {\r
+ if (emailAddress.equalsIgnoreCase(reservedEmailAddress)) {\r
+ throw new ReservedUserEmailAddressException();\r
+ }\r
+ }\r
+ }\r
+\r
+ protected void validateEmailAddress(\r
+ User user, String emailAddress1, String emailAddress2)\r
+ throws PortalException, SystemException {\r
+\r
+ if (!emailAddress1.equals(emailAddress2)) {\r
+ throw new UserEmailAddressException();\r
+ }\r
+\r
+ validateEmailAddress(user.getCompanyId(), emailAddress1);\r
+ validateEmailAddress(user.getCompanyId(), emailAddress2);\r
+\r
+ if (!emailAddress1.equalsIgnoreCase(user.getEmailAddress())) {\r
+ if (userPersistence.fetchByC_EA(\r
+ user.getCompanyId(), emailAddress1) != null) {\r
+\r
+ throw new DuplicateUserEmailAddressException();\r
+ }\r
+ }\r
+ }\r
+\r
+ protected void validateFullName(\r
+ long companyId, String firstName, String middleName,\r
+ String lastName)\r
+ throws PortalException, SystemException {\r
+\r
+ if (Validator.isNull(firstName)) {\r
+ throw new ContactFirstNameException();\r
+ }\r
+ else if (Validator.isNull(lastName) &&\r
+ PrefsPropsUtil.getBoolean(\r
+ companyId, PropsKeys.USERS_LAST_NAME_REQUIRED,\r
+ PropsValues.USERS_LAST_NAME_REQUIRED)) {\r
+\r
+ throw new ContactLastNameException();\r
+ }\r
+\r
+ FullNameValidator fullNameValidator =\r
+ FullNameValidatorFactory.getInstance();\r
+\r
+ if (!fullNameValidator.validate(\r
+ companyId, firstName, middleName, lastName)) {\r
+\r
+ throw new ContactFullNameException();\r
+ }\r
+ }\r
+\r
+ protected void validatePassword(\r
+ long companyId, long userId, String password1, String password2)\r
+ throws PortalException, SystemException {\r
+\r
+ if (Validator.isNull(password1) || Validator.isNull(password2)) {\r
+ throw new UserPasswordException(\r
+ UserPasswordException.PASSWORD_INVALID);\r
+ }\r
+\r
+ if (!password1.equals(password2)) {\r
+ throw new UserPasswordException(\r
+ UserPasswordException.PASSWORDS_DO_NOT_MATCH);\r
+ }\r
+\r
+ PasswordPolicy passwordPolicy =\r
+ passwordPolicyLocalService.getPasswordPolicyByUserId(userId);\r
+\r
+ PwdToolkitUtil.validate(\r
+ companyId, userId, password1, password2, passwordPolicy);\r
+ }\r
+\r
+ protected void validateReminderQuery(String question, String answer)\r
+ throws PortalException {\r
+\r
+ if (!PropsValues.USERS_REMINDER_QUERIES_ENABLED) {\r
+ return;\r
+ }\r
+\r
+ if (Validator.isNull(question)) {\r
+ throw new UserReminderQueryException("Question cannot be null");\r
+ }\r
+\r
+ if (Validator.isNull(answer)) {\r
+ throw new UserReminderQueryException("Answer cannot be null");\r
+ }\r
+ }\r
+\r
+ protected void validateScreenName(\r
+ long companyId, long userId, String screenName)\r
+ throws PortalException, SystemException {\r
+\r
+ if (Validator.isNull(screenName)) {\r
+ throw new UserScreenNameException();\r
+ }\r
+\r
+ ScreenNameValidator screenNameValidator =\r
+ ScreenNameValidatorFactory.getInstance();\r
+\r
+ if (!screenNameValidator.validate(companyId, screenName)) {\r
+ throw new UserScreenNameException();\r
+ }\r
+\r
+ if (Validator.isNumber(screenName)) {\r
+ if (!PropsValues.USERS_SCREEN_NAME_ALLOW_NUMERIC) {\r
+ throw new UserScreenNameException();\r
+ }\r
+\r
+ if (!screenName.equals(String.valueOf(userId))) {\r
+ Group group = groupPersistence.fetchByPrimaryKey(\r
+ GetterUtil.getLong(screenName));\r
+\r
+ if (group != null) {\r
+ throw new UserScreenNameException();\r
+ }\r
+ }\r
+ }\r
+\r
+ for (char c : screenName.toCharArray()) {\r
+ if (!Validator.isChar(c) && !Validator.isDigit(c) &&\r
+ (c != CharPool.DASH) && (c != CharPool.PERIOD) &&\r
+ (c != CharPool.UNDERLINE)) {\r
+\r
+ throw new UserScreenNameException();\r
+ }\r
+ }\r
+\r
+ String[] anonymousNames = BaseServiceImpl.ANONYMOUS_NAMES;\r
+\r
+ for (String anonymousName : anonymousNames) {\r
+ if (screenName.equalsIgnoreCase(anonymousName)) {\r
+ throw new UserScreenNameException();\r
+ }\r
+ }\r
+\r
+ User user = userPersistence.fetchByC_SN(companyId, screenName);\r
+\r
+ if ((user != null) && (user.getUserId() != userId)) {\r
+ throw new DuplicateUserScreenNameException();\r
+ }\r
+\r
+ String friendlyURL = StringPool.SLASH + screenName;\r
+\r
+ Group group = groupPersistence.fetchByC_F(companyId, friendlyURL);\r
+\r
+ if ((group != null) && (group.getClassPK() != userId)) {\r
+ throw new GroupFriendlyURLException(\r
+ GroupFriendlyURLException.DUPLICATE);\r
+ }\r
+\r
+ int exceptionType = LayoutImpl.validateFriendlyURL(friendlyURL);\r
+\r
+ if (exceptionType != -1) {\r
+ throw new UserScreenNameException(\r
+ new GroupFriendlyURLException(exceptionType));\r
+ }\r
+\r
+ String[] reservedScreenNames = PrefsPropsUtil.getStringArray(\r
+ companyId, PropsKeys.ADMIN_RESERVED_SCREEN_NAMES,\r
+ StringPool.NEW_LINE, PropsValues.ADMIN_RESERVED_SCREEN_NAMES);\r
+\r
+ for (String reservedScreenName : reservedScreenNames) {\r
+ if (screenName.equalsIgnoreCase(reservedScreenName)) {\r
+ throw new ReservedUserScreenNameException();\r
+ }\r
+ }\r
+ }\r
+\r
+ private static Log _log = LogFactoryUtil.getLog(UserLocalServiceImpl.class);\r
+\r
+ private static Map<Long, User> _defaultUsers =\r
+ new ConcurrentHashMap<Long, User>();\r
+\r
+}
\ No newline at end of file