0001: /**
0002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
0003: *
0004: * Permission is hereby granted, free of charge, to any person obtaining a copy
0005: * of this software and associated documentation files (the "Software"), to deal
0006: * in the Software without restriction, including without limitation the rights
0007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0008: * copies of the Software, and to permit persons to whom the Software is
0009: * furnished to do so, subject to the following conditions:
0010: *
0011: * The above copyright notice and this permission notice shall be included in
0012: * all copies or substantial portions of the Software.
0013: *
0014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0020: * SOFTWARE.
0021: */package com.liferay.portal.service.impl;
0022:
0023: import com.liferay.portal.ContactBirthdayException;
0024: import com.liferay.portal.ContactFirstNameException;
0025: import com.liferay.portal.ContactLastNameException;
0026: import com.liferay.portal.DuplicateUserEmailAddressException;
0027: import com.liferay.portal.DuplicateUserScreenNameException;
0028: import com.liferay.portal.ModelListenerException;
0029: import com.liferay.portal.NoSuchContactException;
0030: import com.liferay.portal.NoSuchGroupException;
0031: import com.liferay.portal.NoSuchRoleException;
0032: import com.liferay.portal.NoSuchUserException;
0033: import com.liferay.portal.NoSuchUserGroupException;
0034: import com.liferay.portal.PasswordExpiredException;
0035: import com.liferay.portal.PortalException;
0036: import com.liferay.portal.RequiredUserException;
0037: import com.liferay.portal.ReservedUserEmailAddressException;
0038: import com.liferay.portal.ReservedUserScreenNameException;
0039: import com.liferay.portal.SystemException;
0040: import com.liferay.portal.UserEmailAddressException;
0041: import com.liferay.portal.UserIdException;
0042: import com.liferay.portal.UserLockoutException;
0043: import com.liferay.portal.UserPasswordException;
0044: import com.liferay.portal.UserPortraitException;
0045: import com.liferay.portal.UserScreenNameException;
0046: import com.liferay.portal.UserSmsException;
0047: import com.liferay.portal.kernel.language.LanguageUtil;
0048: import com.liferay.portal.kernel.mail.MailMessage;
0049: import com.liferay.portal.kernel.util.ArrayUtil;
0050: import com.liferay.portal.kernel.util.Base64;
0051: import com.liferay.portal.kernel.util.GetterUtil;
0052: import com.liferay.portal.kernel.util.InstancePool;
0053: import com.liferay.portal.kernel.util.KeyValuePair;
0054: import com.liferay.portal.kernel.util.OrderByComparator;
0055: import com.liferay.portal.kernel.util.StringPool;
0056: import com.liferay.portal.kernel.util.StringUtil;
0057: import com.liferay.portal.kernel.util.Validator;
0058: import com.liferay.portal.model.Company;
0059: import com.liferay.portal.model.Contact;
0060: import com.liferay.portal.model.Group;
0061: import com.liferay.portal.model.Organization;
0062: import com.liferay.portal.model.PasswordPolicy;
0063: import com.liferay.portal.model.Role;
0064: import com.liferay.portal.model.User;
0065: import com.liferay.portal.model.UserGroup;
0066: import com.liferay.portal.model.impl.CompanyImpl;
0067: import com.liferay.portal.model.impl.ContactImpl;
0068: import com.liferay.portal.model.impl.ResourceImpl;
0069: import com.liferay.portal.model.impl.RoleImpl;
0070: import com.liferay.portal.model.impl.UserImpl;
0071: import com.liferay.portal.security.auth.AuthPipeline;
0072: import com.liferay.portal.security.auth.Authenticator;
0073: import com.liferay.portal.security.auth.PrincipalException;
0074: import com.liferay.portal.security.auth.ScreenNameGenerator;
0075: import com.liferay.portal.security.auth.ScreenNameValidator;
0076: import com.liferay.portal.security.ldap.PortalLDAPUtil;
0077: import com.liferay.portal.security.permission.PermissionCacheUtil;
0078: import com.liferay.portal.security.pwd.PwdEncryptor;
0079: import com.liferay.portal.security.pwd.PwdToolkitUtil;
0080: import com.liferay.portal.service.base.UserLocalServiceBaseImpl;
0081: import com.liferay.portal.util.PortalUtil;
0082: import com.liferay.portal.util.PrefsPropsUtil;
0083: import com.liferay.portal.util.PropsUtil;
0084: import com.liferay.portal.util.PropsValues;
0085: import com.liferay.util.Encryptor;
0086: import com.liferay.util.EncryptorException;
0087: import com.liferay.util.Html;
0088: import com.liferay.util.Normalizer;
0089:
0090: import java.io.IOException;
0091: import java.io.UnsupportedEncodingException;
0092:
0093: import java.rmi.RemoteException;
0094:
0095: import java.security.MessageDigest;
0096: import java.security.NoSuchAlgorithmException;
0097:
0098: import java.util.ArrayList;
0099: import java.util.Date;
0100: import java.util.LinkedHashMap;
0101: import java.util.List;
0102: import java.util.Locale;
0103: import java.util.Map;
0104:
0105: import javax.mail.internet.InternetAddress;
0106:
0107: import org.apache.commons.logging.Log;
0108: import org.apache.commons.logging.LogFactory;
0109:
0110: /**
0111: * <a href="UserLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
0112: *
0113: * @author Brian Wing Shun Chan
0114: * @author Scott Lee
0115: *
0116: */
0117: public class UserLocalServiceImpl extends UserLocalServiceBaseImpl {
0118:
0119: public void addGroupUsers(long groupId, long[] userIds)
0120: throws PortalException, SystemException {
0121:
0122: groupPersistence.addUsers(groupId, userIds);
0123:
0124: Group group = groupPersistence.findByPrimaryKey(groupId);
0125:
0126: Role role = rolePersistence.findByC_N(group.getCompanyId(),
0127: RoleImpl.COMMUNITY_MEMBER);
0128:
0129: for (int i = 0; i < userIds.length; i++) {
0130: long userId = userIds[i];
0131:
0132: userGroupRoleLocalService.addUserGroupRoles(userId,
0133: groupId, new long[] { role.getRoleId() });
0134: }
0135:
0136: PermissionCacheUtil.clearCache();
0137: }
0138:
0139: public void addOrganizationUsers(long organizationId, long[] userIds)
0140: throws PortalException, SystemException {
0141:
0142: organizationPersistence.addUsers(organizationId, userIds);
0143:
0144: Organization organization = organizationPersistence
0145: .findByPrimaryKey(organizationId);
0146:
0147: Group group = organization.getGroup();
0148:
0149: long groupId = group.getGroupId();
0150:
0151: Role role = rolePersistence.findByC_N(group.getCompanyId(),
0152: RoleImpl.ORGANIZATION_MEMBER);
0153:
0154: for (int i = 0; i < userIds.length; i++) {
0155: long userId = userIds[i];
0156:
0157: userGroupRoleLocalService.addUserGroupRoles(userId,
0158: groupId, new long[] { role.getRoleId() });
0159: }
0160:
0161: PermissionCacheUtil.clearCache();
0162: }
0163:
0164: public void addPasswordPolicyUsers(long passwordPolicyId,
0165: long[] userIds) throws PortalException, SystemException {
0166:
0167: passwordPolicyRelLocalService.addPasswordPolicyRels(
0168: passwordPolicyId, User.class.getName(), userIds);
0169: }
0170:
0171: public void addRoleUsers(long roleId, long[] userIds)
0172: throws PortalException, SystemException {
0173:
0174: rolePersistence.addUsers(roleId, userIds);
0175:
0176: PermissionCacheUtil.clearCache();
0177: }
0178:
0179: public void addUserGroupUsers(long userGroupId, long[] userIds)
0180: throws PortalException, SystemException {
0181:
0182: userGroupPersistence.addUsers(userGroupId, userIds);
0183:
0184: PermissionCacheUtil.clearCache();
0185: }
0186:
0187: public User addUser(long creatorUserId, long companyId,
0188: boolean autoPassword, String password1, String password2,
0189: boolean autoScreenName, String screenName,
0190: String emailAddress, Locale locale, String firstName,
0191: String middleName, String lastName, int prefixId,
0192: int suffixId, boolean male, int birthdayMonth,
0193: int birthdayDay, int birthdayYear, String jobTitle,
0194: long[] organizationIds, boolean sendEmail)
0195: throws PortalException, SystemException {
0196:
0197: // User
0198:
0199: Company company = companyPersistence
0200: .findByPrimaryKey(companyId);
0201: screenName = getScreenName(screenName);
0202: emailAddress = emailAddress.trim().toLowerCase();
0203: Date now = new Date();
0204:
0205: if (PropsValues.USERS_SCREEN_NAME_ALWAYS_AUTOGENERATE) {
0206: autoScreenName = true;
0207: }
0208:
0209: validate(companyId, autoPassword, password1, password2,
0210: autoScreenName, screenName, emailAddress, firstName,
0211: lastName, organizationIds);
0212:
0213: if (autoPassword) {
0214: password1 = PwdToolkitUtil.generate();
0215: }
0216:
0217: long userId = counterLocalService.increment();
0218:
0219: if (autoScreenName) {
0220: ScreenNameGenerator screenNameGenerator = (ScreenNameGenerator) InstancePool
0221: .get(PropsValues.USERS_SCREEN_NAME_GENERATOR);
0222:
0223: try {
0224: screenName = screenNameGenerator.generate(companyId,
0225: userId);
0226: } catch (Exception e) {
0227: throw new SystemException(e);
0228: }
0229: }
0230:
0231: User defaultUser = getDefaultUser(companyId);
0232:
0233: String fullName = UserImpl.getFullName(firstName, middleName,
0234: lastName);
0235:
0236: String greeting = LanguageUtil
0237: .get(companyId, locale, "welcome")
0238: + ", " + fullName + "!";
0239:
0240: User user = userPersistence.create(userId);
0241:
0242: user.setCompanyId(companyId);
0243: user.setCreateDate(now);
0244: user.setModifiedDate(now);
0245: user.setDefaultUser(false);
0246: user.setContactId(counterLocalService.increment());
0247: user.setPassword(PwdEncryptor.encrypt(password1));
0248: user.setPasswordUnencrypted(password1);
0249: user.setPasswordEncrypted(true);
0250: user.setPasswordReset(false);
0251: user.setScreenName(screenName);
0252: user.setEmailAddress(emailAddress);
0253: user.setLanguageId(locale.toString());
0254: user.setTimeZoneId(defaultUser.getTimeZoneId());
0255: user.setGreeting(greeting);
0256: user.setActive(true);
0257:
0258: userPersistence.update(user);
0259:
0260: // Resources
0261:
0262: String creatorUserName = StringPool.BLANK;
0263:
0264: if (creatorUserId <= 0) {
0265: creatorUserId = user.getUserId();
0266:
0267: // Don't grab the full name from the User object because it doesn't
0268: // have a corresponding Contact object yet
0269:
0270: //creatorUserName = user.getFullName();
0271: } else {
0272: User creatorUser = userPersistence
0273: .findByPrimaryKey(creatorUserId);
0274:
0275: creatorUserName = creatorUser.getFullName();
0276: }
0277:
0278: resourceLocalService.addResources(companyId, 0, creatorUserId,
0279: User.class.getName(), user.getUserId(), false, false,
0280: false);
0281:
0282: // Mail
0283:
0284: if (user.hasCompanyMx()) {
0285: try {
0286: mailService.addUser(userId, password1, firstName,
0287: middleName, lastName, emailAddress);
0288: } catch (RemoteException re) {
0289: throw new SystemException(re);
0290: }
0291: }
0292:
0293: // Contact
0294:
0295: Date birthday = PortalUtil.getDate(birthdayMonth, birthdayDay,
0296: birthdayYear, new ContactBirthdayException());
0297:
0298: Contact contact = contactPersistence
0299: .create(user.getContactId());
0300:
0301: contact.setCompanyId(user.getCompanyId());
0302: contact.setUserId(creatorUserId);
0303: contact.setUserName(creatorUserName);
0304: contact.setCreateDate(now);
0305: contact.setModifiedDate(now);
0306: contact.setAccountId(company.getAccountId());
0307: contact
0308: .setParentContactId(ContactImpl.DEFAULT_PARENT_CONTACT_ID);
0309: contact.setFirstName(firstName);
0310: contact.setMiddleName(middleName);
0311: contact.setLastName(lastName);
0312: contact.setPrefixId(prefixId);
0313: contact.setSuffixId(suffixId);
0314: contact.setMale(male);
0315: contact.setBirthday(birthday);
0316: contact.setJobTitle(jobTitle);
0317:
0318: contactPersistence.update(contact);
0319:
0320: // Organizations
0321:
0322: updateOrganizations(userId, organizationIds);
0323:
0324: // Group
0325:
0326: groupLocalService
0327: .addGroup(user.getUserId(), User.class.getName(), user
0328: .getUserId(), null, null, 0, null, true);
0329:
0330: // Default groups
0331:
0332: String[] defaultGroupNames = PrefsPropsUtil.getStringArray(
0333: companyId, PropsUtil.ADMIN_DEFAULT_GROUP_NAMES,
0334: StringPool.NEW_LINE,
0335: PropsValues.ADMIN_DEFAULT_GROUP_NAMES);
0336:
0337: long[] groupIds = new long[defaultGroupNames.length];
0338:
0339: for (int i = 0; i < defaultGroupNames.length; i++) {
0340: try {
0341: Group group = groupFinder.findByC_N(companyId,
0342: defaultGroupNames[i]);
0343:
0344: groupIds[i] = group.getGroupId();
0345: } catch (NoSuchGroupException nsge) {
0346: }
0347: }
0348:
0349: groupLocalService.addUserGroups(userId, groupIds);
0350:
0351: // Default roles
0352:
0353: List roles = new ArrayList();
0354:
0355: String[] defaultRoleNames = PrefsPropsUtil.getStringArray(
0356: companyId, PropsUtil.ADMIN_DEFAULT_ROLE_NAMES,
0357: StringPool.NEW_LINE,
0358: PropsValues.ADMIN_DEFAULT_ROLE_NAMES);
0359:
0360: for (int i = 0; i < defaultRoleNames.length; i++) {
0361: try {
0362: Role role = roleFinder.findByC_N(companyId,
0363: defaultRoleNames[i]);
0364:
0365: roles.add(role);
0366: } catch (NoSuchRoleException nsge) {
0367: }
0368: }
0369:
0370: userPersistence.setRoles(userId, roles);
0371:
0372: // Default user groups
0373:
0374: List userGroups = new ArrayList();
0375:
0376: String[] defaultUserGroupNames = PrefsPropsUtil.getStringArray(
0377: companyId, PropsUtil.ADMIN_DEFAULT_USER_GROUP_NAMES,
0378: StringPool.NEW_LINE,
0379: PropsValues.ADMIN_DEFAULT_USER_GROUP_NAMES);
0380:
0381: for (int i = 0; i < defaultUserGroupNames.length; i++) {
0382: try {
0383: UserGroup userGroup = userGroupFinder.findByC_N(
0384: companyId, defaultUserGroupNames[i]);
0385:
0386: userGroups.add(userGroup);
0387: } catch (NoSuchUserGroupException nsuge) {
0388: }
0389: }
0390:
0391: userPersistence.setUserGroups(userId, userGroups);
0392:
0393: // Email
0394:
0395: if (sendEmail) {
0396: sendEmail(user, password1);
0397: }
0398:
0399: return user;
0400: }
0401:
0402: public int authenticateByEmailAddress(long companyId,
0403: String emailAddress, String password, Map headerMap,
0404: Map parameterMap) throws PortalException, SystemException {
0405:
0406: return authenticate(companyId, emailAddress, password,
0407: CompanyImpl.AUTH_TYPE_EA, headerMap, parameterMap);
0408: }
0409:
0410: public int authenticateByScreenName(long companyId,
0411: String screenName, String password, Map headerMap,
0412: Map parameterMap) throws PortalException, SystemException {
0413:
0414: return authenticate(companyId, screenName, password,
0415: CompanyImpl.AUTH_TYPE_SN, headerMap, parameterMap);
0416: }
0417:
0418: public int authenticateByUserId(long companyId, long userId,
0419: String password, Map headerMap, Map parameterMap)
0420: throws PortalException, SystemException {
0421:
0422: return authenticate(companyId, String.valueOf(userId),
0423: password, CompanyImpl.AUTH_TYPE_ID, headerMap,
0424: parameterMap);
0425: }
0426:
0427: public boolean authenticateForJAAS(long userId, String encPwd)
0428: throws PortalException, SystemException {
0429:
0430: try {
0431: User user = userPersistence.findByPrimaryKey(userId);
0432:
0433: if (user.isDefaultUser()) {
0434: _log
0435: .error("The default user should never be allowed to authenticate");
0436:
0437: return false;
0438: }
0439:
0440: String password = user.getPassword();
0441:
0442: if (user.isPasswordEncrypted()) {
0443: if (password.equals(encPwd)) {
0444: return true;
0445: }
0446:
0447: if (!PropsValues.PORTAL_JAAS_STRICT_PASSWORD) {
0448: encPwd = PwdEncryptor.encrypt(encPwd, password);
0449:
0450: if (password.equals(encPwd)) {
0451: return true;
0452: }
0453: }
0454: } else {
0455: if (!PropsValues.PORTAL_JAAS_STRICT_PASSWORD) {
0456: if (password.equals(encPwd)) {
0457: return true;
0458: }
0459: }
0460:
0461: password = PwdEncryptor.encrypt(password);
0462:
0463: if (password.equals(encPwd)) {
0464: return true;
0465: }
0466: }
0467: } catch (Exception e) {
0468: _log.error(e);
0469: }
0470:
0471: return false;
0472: }
0473:
0474: public void checkLockout(User user) throws PortalException,
0475: SystemException {
0476:
0477: if (PortalLDAPUtil.isPasswordPolicyEnabled(user.getCompanyId())) {
0478: return;
0479: }
0480:
0481: PasswordPolicy passwordPolicy = user.getPasswordPolicy();
0482:
0483: if (passwordPolicy.isLockout()) {
0484:
0485: // Reset failure count
0486:
0487: Date now = new Date();
0488: int failedLoginAttempts = user.getFailedLoginAttempts();
0489:
0490: if (failedLoginAttempts > 0) {
0491: long failedLoginTime = user.getLastFailedLoginDate()
0492: .getTime();
0493: long elapsedTime = now.getTime() - failedLoginTime;
0494: long requiredElapsedTime = passwordPolicy
0495: .getResetFailureCount() * 1000;
0496:
0497: if ((requiredElapsedTime != 0)
0498: && (elapsedTime > requiredElapsedTime)) {
0499:
0500: user.setLastFailedLoginDate(null);
0501: user.setFailedLoginAttempts(0);
0502: }
0503: }
0504:
0505: // Reset lockout
0506:
0507: if (user.isLockout()) {
0508: long lockoutTime = user.getLockoutDate().getTime();
0509: long elapsedTime = now.getTime() - lockoutTime;
0510: long requiredElapsedTime = passwordPolicy
0511: .getLockoutDuration() * 1000;
0512:
0513: if ((requiredElapsedTime != 0)
0514: && (elapsedTime > requiredElapsedTime)) {
0515:
0516: user.setLockout(false);
0517: user.setLockoutDate(null);
0518: }
0519: }
0520:
0521: if (user.isLockout()) {
0522: throw new UserLockoutException();
0523: }
0524: }
0525: }
0526:
0527: public void checkLoginFailure(User user) throws PortalException,
0528: SystemException {
0529:
0530: Date now = new Date();
0531:
0532: int failedLoginAttempts = user.getFailedLoginAttempts();
0533:
0534: user.setLastFailedLoginDate(now);
0535: user.setFailedLoginAttempts(++failedLoginAttempts);
0536:
0537: userPersistence.update(user);
0538: }
0539:
0540: public void checkLoginFailureByEmailAddress(long companyId,
0541: String emailAddress) throws PortalException,
0542: SystemException {
0543:
0544: User user = getUserByEmailAddress(companyId, emailAddress);
0545:
0546: checkLoginFailure(user);
0547: }
0548:
0549: public void checkLoginFailureById(long userId)
0550: throws PortalException, SystemException {
0551:
0552: User user = userPersistence.findByPrimaryKey(userId);
0553:
0554: checkLoginFailure(user);
0555: }
0556:
0557: public void checkLoginFailureByScreenName(long companyId,
0558: String screenName) throws PortalException, SystemException {
0559:
0560: User user = getUserByScreenName(companyId, screenName);
0561:
0562: checkLoginFailure(user);
0563: }
0564:
0565: public void checkPasswordExpired(User user) throws PortalException,
0566: SystemException {
0567:
0568: if (PortalLDAPUtil.isPasswordPolicyEnabled(user.getCompanyId())) {
0569: return;
0570: }
0571:
0572: PasswordPolicy passwordPolicy = user.getPasswordPolicy();
0573:
0574: // Check if password has expired
0575:
0576: if (isPasswordExpired(user)) {
0577: int graceLoginCount = user.getGraceLoginCount();
0578:
0579: if (graceLoginCount < passwordPolicy.getGraceLimit()) {
0580: user.setGraceLoginCount(++graceLoginCount);
0581:
0582: userPersistence.update(user);
0583: } else {
0584: throw new PasswordExpiredException();
0585: }
0586: }
0587:
0588: // Check if warning message should be sent
0589:
0590: if (isPasswordExpiringSoon(user)) {
0591: user.setPasswordReset(true);
0592:
0593: userPersistence.update(user);
0594: }
0595:
0596: // Check if user should be forced to change password on first login
0597:
0598: if (passwordPolicy.isChangeable()
0599: && passwordPolicy.isChangeRequired()) {
0600:
0601: if (user.getLastLoginDate() == null) {
0602: boolean passwordReset = false;
0603:
0604: if (passwordPolicy.isChangeable()
0605: && passwordPolicy.isChangeRequired()) {
0606:
0607: passwordReset = true;
0608: }
0609:
0610: user.setPasswordReset(passwordReset);
0611:
0612: userPersistence.update(user);
0613: }
0614: }
0615: }
0616:
0617: public void clearOrganizationUsers(long organizationId)
0618: throws PortalException, SystemException {
0619:
0620: organizationPersistence.clearUsers(organizationId);
0621:
0622: PermissionCacheUtil.clearCache();
0623: }
0624:
0625: public void clearUserGroupUsers(long userGroupId)
0626: throws PortalException, SystemException {
0627:
0628: userGroupPersistence.clearUsers(userGroupId);
0629:
0630: PermissionCacheUtil.clearCache();
0631: }
0632:
0633: public KeyValuePair decryptUserId(long companyId, String name,
0634: String password) throws PortalException, SystemException {
0635:
0636: Company company = companyPersistence
0637: .findByPrimaryKey(companyId);
0638:
0639: try {
0640: name = Encryptor.decrypt(company.getKeyObj(), name);
0641: } catch (EncryptorException ee) {
0642: throw new SystemException(ee);
0643: }
0644:
0645: long userId = GetterUtil.getLong(name);
0646:
0647: User user = userPersistence.findByPrimaryKey(userId);
0648:
0649: try {
0650: password = Encryptor.decrypt(company.getKeyObj(), password);
0651: } catch (EncryptorException ee) {
0652: throw new SystemException(ee);
0653: }
0654:
0655: String encPwd = PwdEncryptor.encrypt(password);
0656:
0657: if (user.getPassword().equals(encPwd)) {
0658: if (isPasswordExpired(user)) {
0659: user.setPasswordReset(true);
0660:
0661: userPersistence.update(user);
0662: }
0663:
0664: return new KeyValuePair(name, password);
0665: } else {
0666: throw new PrincipalException();
0667: }
0668: }
0669:
0670: public void deletePasswordPolicyUser(long passwordPolicyId,
0671: long userId) throws PortalException, SystemException {
0672:
0673: passwordPolicyRelLocalService.deletePasswordPolicyRel(
0674: passwordPolicyId, User.class.getName(), userId);
0675: }
0676:
0677: public void deleteRoleUser(long roleId, long userId)
0678: throws PortalException, SystemException {
0679:
0680: rolePersistence.removeUser(roleId, userId);
0681:
0682: PermissionCacheUtil.clearCache();
0683: }
0684:
0685: public void deleteUser(long userId) throws PortalException,
0686: SystemException {
0687:
0688: if (!PropsValues.USERS_DELETE) {
0689: throw new RequiredUserException();
0690: }
0691:
0692: User user = userPersistence.findByPrimaryKey(userId);
0693:
0694: // Group
0695:
0696: Group group = user.getGroup();
0697:
0698: groupLocalService.deleteGroup(group.getGroupId());
0699:
0700: // Portrait
0701:
0702: ImageLocalUtil.deleteImage(user.getPortraitId());
0703:
0704: // Password policy relation
0705:
0706: passwordPolicyRelLocalService.deletePasswordPolicyRel(
0707: User.class.getName(), userId);
0708:
0709: // Old passwords
0710:
0711: passwordTrackerLocalService.deletePasswordTrackers(userId);
0712:
0713: // Subscriptions
0714:
0715: subscriptionLocalService.deleteSubscriptions(userId);
0716:
0717: // External user ids
0718:
0719: userIdMapperLocalService.deleteUserIdMappers(userId);
0720:
0721: // Blogs
0722:
0723: blogsStatsUserLocalService.deleteStatsUserByUserId(userId);
0724:
0725: // Document library
0726:
0727: dlFileRankLocalService.deleteFileRanks(userId);
0728:
0729: // Message boards
0730:
0731: mbBanLocalService.deleteBansByBanUserId(userId);
0732: mbMessageFlagLocalService.deleteFlags(userId);
0733: mbStatsUserLocalService.deleteStatsUserByUserId(userId);
0734:
0735: // Shopping cart
0736:
0737: shoppingCartLocalService.deleteUserCarts(userId);
0738:
0739: // Mail
0740:
0741: try {
0742: mailService.deleteUser(userId);
0743: } catch (RemoteException re) {
0744: throw new SystemException(re);
0745: }
0746:
0747: // Contact
0748:
0749: contactLocalService.deleteContact(user.getContactId());
0750:
0751: // Resources
0752:
0753: resourceLocalService.deleteResource(user.getCompanyId(),
0754: User.class.getName(), ResourceImpl.SCOPE_INDIVIDUAL,
0755: user.getUserId());
0756:
0757: // Group roles
0758:
0759: userGroupRoleLocalService.deleteUserGroupRolesByUserId(userId);
0760:
0761: // User
0762:
0763: userPersistence.remove(userId);
0764:
0765: // Permission cache
0766:
0767: PermissionCacheUtil.clearCache();
0768: }
0769:
0770: public String encryptUserId(String name) throws PortalException,
0771: SystemException {
0772:
0773: long userId = GetterUtil.getLong(name);
0774:
0775: User user = userPersistence.findByPrimaryKey(userId);
0776:
0777: Company company = companyPersistence.findByPrimaryKey(user
0778: .getCompanyId());
0779:
0780: try {
0781: return Encryptor.encrypt(company.getKeyObj(), name);
0782: } catch (EncryptorException ee) {
0783: throw new SystemException(ee);
0784: }
0785: }
0786:
0787: public User getDefaultUser(long companyId) throws PortalException,
0788: SystemException {
0789:
0790: return userPersistence.findByC_DU(companyId, true);
0791: }
0792:
0793: public long getDefaultUserId(long companyId)
0794: throws PortalException, SystemException {
0795:
0796: User user = userPersistence.findByC_DU(companyId, true);
0797:
0798: return user.getUserId();
0799: }
0800:
0801: public List getGroupUsers(long groupId) throws PortalException,
0802: SystemException {
0803:
0804: return groupPersistence.getUsers(groupId);
0805: }
0806:
0807: public List getOrganizationUsers(long organizationId)
0808: throws PortalException, SystemException {
0809:
0810: return organizationPersistence.getUsers(organizationId);
0811: }
0812:
0813: public List getPermissionUsers(long companyId, long groupId,
0814: String name, String primKey, String actionId,
0815: String firstName, String middleName, String lastName,
0816: String emailAddress, boolean andOperator, int begin, int end)
0817: throws PortalException, SystemException {
0818:
0819: int orgGroupPermissionsCount = permissionUserFinder
0820: .countByOrgGroupPermissions(companyId, name, primKey,
0821: actionId);
0822:
0823: if (orgGroupPermissionsCount > 0) {
0824: return permissionUserFinder
0825: .findByUserAndOrgGroupPermission(companyId, name,
0826: primKey, actionId, firstName, middleName,
0827: lastName, emailAddress, andOperator, begin,
0828: end);
0829: } else {
0830: return permissionUserFinder.findByPermissionAndRole(
0831: companyId, groupId, name, primKey, actionId,
0832: firstName, middleName, lastName, emailAddress,
0833: andOperator, begin, end);
0834: }
0835: }
0836:
0837: public int getPermissionUsersCount(long companyId, long groupId,
0838: String name, String primKey, String actionId,
0839: String firstName, String middleName, String lastName,
0840: String emailAddress, boolean andOperator)
0841: throws PortalException, SystemException {
0842:
0843: int orgGroupPermissionsCount = permissionUserFinder
0844: .countByOrgGroupPermissions(companyId, name, primKey,
0845: actionId);
0846:
0847: if (orgGroupPermissionsCount > 0) {
0848: return permissionUserFinder
0849: .countByUserAndOrgGroupPermission(companyId, name,
0850: primKey, actionId, firstName, middleName,
0851: lastName, emailAddress, andOperator);
0852: } else {
0853: return permissionUserFinder.countByPermissionAndRole(
0854: companyId, groupId, name, primKey, actionId,
0855: firstName, middleName, lastName, emailAddress,
0856: andOperator);
0857: }
0858: }
0859:
0860: public List getRoleUsers(long roleId) throws PortalException,
0861: SystemException {
0862:
0863: return rolePersistence.getUsers(roleId);
0864: }
0865:
0866: public List getUserGroupUsers(long userGroupId)
0867: throws PortalException, SystemException {
0868:
0869: return userGroupPersistence.getUsers(userGroupId);
0870: }
0871:
0872: public User getUserByContactId(long contactId)
0873: throws PortalException, SystemException {
0874:
0875: return userPersistence.findByContactId(contactId);
0876: }
0877:
0878: public User getUserByEmailAddress(long companyId,
0879: String emailAddress) throws PortalException,
0880: SystemException {
0881:
0882: emailAddress = emailAddress.trim().toLowerCase();
0883:
0884: return userPersistence.findByC_EA(companyId, emailAddress);
0885: }
0886:
0887: public User getUserById(long userId) throws PortalException,
0888: SystemException {
0889:
0890: return userPersistence.findByPrimaryKey(userId);
0891: }
0892:
0893: public User getUserById(long companyId, long userId)
0894: throws PortalException, SystemException {
0895:
0896: return userPersistence.findByC_U(companyId, userId);
0897: }
0898:
0899: public User getUserByPortraitId(long portraitId)
0900: throws PortalException, SystemException {
0901:
0902: return userPersistence.findByPortraitId(portraitId);
0903: }
0904:
0905: public User getUserByScreenName(long companyId, String screenName)
0906: throws PortalException, SystemException {
0907:
0908: screenName = getScreenName(screenName);
0909:
0910: return userPersistence.findByC_SN(companyId, screenName);
0911: }
0912:
0913: public long getUserIdByEmailAddress(long companyId,
0914: String emailAddress) throws PortalException,
0915: SystemException {
0916:
0917: emailAddress = emailAddress.trim().toLowerCase();
0918:
0919: User user = userPersistence.findByC_EA(companyId, emailAddress);
0920:
0921: return user.getUserId();
0922: }
0923:
0924: public long getUserIdByScreenName(long companyId, String screenName)
0925: throws PortalException, SystemException {
0926:
0927: screenName = getScreenName(screenName);
0928:
0929: User user = userPersistence.findByC_SN(companyId, screenName);
0930:
0931: return user.getUserId();
0932: }
0933:
0934: public boolean hasGroupUser(long groupId, long userId)
0935: throws PortalException, SystemException {
0936:
0937: return groupPersistence.containsUser(groupId, userId);
0938: }
0939:
0940: public boolean hasOrganizationUser(long organizationId, long userId)
0941: throws PortalException, SystemException {
0942:
0943: return organizationPersistence.containsUser(organizationId,
0944: userId);
0945: }
0946:
0947: public boolean hasPasswordPolicyUser(long passwordPolicyId,
0948: long userId) throws PortalException, SystemException {
0949:
0950: return passwordPolicyRelLocalService.hasPasswordPolicyRel(
0951: passwordPolicyId, User.class.getName(), userId);
0952: }
0953:
0954: public boolean hasRoleUser(long roleId, long userId)
0955: throws PortalException, SystemException {
0956:
0957: return rolePersistence.containsUser(roleId, userId);
0958: }
0959:
0960: public boolean hasUserGroupUser(long userGroupId, long userId)
0961: throws PortalException, SystemException {
0962:
0963: return userGroupPersistence.containsUser(userGroupId, userId);
0964: }
0965:
0966: public boolean isPasswordExpired(User user) throws PortalException,
0967: SystemException {
0968:
0969: PasswordPolicy passwordPolicy = user.getPasswordPolicy();
0970:
0971: if (passwordPolicy.getExpireable()) {
0972: Date now = new Date();
0973:
0974: if (user.getPasswordModifiedDate() == null) {
0975: user.setPasswordModifiedDate(now);
0976:
0977: userPersistence.update(user);
0978: }
0979:
0980: long passwordStartTime = user.getPasswordModifiedDate()
0981: .getTime();
0982: long elapsedTime = now.getTime() - passwordStartTime;
0983:
0984: if (elapsedTime > (passwordPolicy.getMaxAge() * 1000)) {
0985: return true;
0986: } else {
0987: return false;
0988: }
0989: }
0990:
0991: return false;
0992: }
0993:
0994: public boolean isPasswordExpiringSoon(User user)
0995: throws PortalException, SystemException {
0996:
0997: PasswordPolicy passwordPolicy = user.getPasswordPolicy();
0998:
0999: if (passwordPolicy.isExpireable()) {
1000: Date now = new Date();
1001:
1002: if (user.getPasswordModifiedDate() == null) {
1003: user.setPasswordModifiedDate(now);
1004:
1005: userPersistence.update(user);
1006: }
1007:
1008: long timeModified = user.getPasswordModifiedDate()
1009: .getTime();
1010: long passwordExpiresOn = (passwordPolicy.getMaxAge() * 1000)
1011: + timeModified;
1012:
1013: long timeStartWarning = passwordExpiresOn
1014: - (passwordPolicy.getWarningTime() * 1000);
1015:
1016: if (now.getTime() > timeStartWarning) {
1017: return true;
1018: } else {
1019: return false;
1020: }
1021: }
1022:
1023: return false;
1024: }
1025:
1026: public List search(long companyId, String keywords, Boolean active,
1027: LinkedHashMap params, int begin, int end,
1028: OrderByComparator obc) throws SystemException {
1029:
1030: return userFinder.findByKeywords(companyId, keywords, active,
1031: params, begin, end, obc);
1032: }
1033:
1034: public List search(long companyId, String firstName,
1035: String middleName, String lastName, String screenName,
1036: String emailAddress, Boolean active, LinkedHashMap params,
1037: boolean andSearch, int begin, int end, OrderByComparator obc)
1038: throws SystemException {
1039:
1040: return userFinder.findByC_FN_MN_LN_SN_EA_A(companyId,
1041: firstName, middleName, lastName, screenName,
1042: emailAddress, active, params, andSearch, begin, end,
1043: obc);
1044: }
1045:
1046: public int searchCount(long companyId, String keywords,
1047: Boolean active, LinkedHashMap params)
1048: throws SystemException {
1049:
1050: return userFinder.countByKeywords(companyId, keywords, active,
1051: params);
1052: }
1053:
1054: public int searchCount(long companyId, String firstName,
1055: String middleName, String lastName, String screenName,
1056: String emailAddress, Boolean active, LinkedHashMap params,
1057: boolean andSearch) throws SystemException {
1058:
1059: return userFinder.countByC_FN_MN_LN_SN_EA_A(companyId,
1060: firstName, middleName, lastName, screenName,
1061: emailAddress, active, params, andSearch);
1062: }
1063:
1064: public void sendPassword(long companyId, String emailAddress,
1065: String remoteAddr, String remoteHost, String userAgent)
1066: throws PortalException, SystemException {
1067:
1068: if (!PrefsPropsUtil.getBoolean(companyId,
1069: PropsUtil.COMPANY_SECURITY_SEND_PASSWORD)
1070: || !PrefsPropsUtil.getBoolean(companyId,
1071: PropsUtil.ADMIN_EMAIL_PASSWORD_SENT_ENABLED)) {
1072:
1073: return;
1074: }
1075:
1076: emailAddress = emailAddress.trim().toLowerCase();
1077:
1078: if (!Validator.isEmailAddress(emailAddress)) {
1079: throw new UserEmailAddressException();
1080: }
1081:
1082: Company company = companyPersistence
1083: .findByPrimaryKey(companyId);
1084:
1085: User user = userPersistence.findByC_EA(companyId, emailAddress);
1086:
1087: PasswordPolicy passwordPolicy = user.getPasswordPolicy();
1088:
1089: /*if (user.hasCompanyMx()) {
1090: throw new SendPasswordException();
1091: }*/
1092:
1093: String newPassword = null;
1094:
1095: if (!PwdEncryptor.PASSWORDS_ENCRYPTION_ALGORITHM
1096: .equals(PwdEncryptor.TYPE_NONE)) {
1097:
1098: newPassword = PwdToolkitUtil.generate();
1099:
1100: boolean passwordReset = false;
1101:
1102: if (passwordPolicy.getChangeable()
1103: && passwordPolicy.getChangeRequired()) {
1104:
1105: passwordReset = true;
1106: }
1107:
1108: user.setPassword(PwdEncryptor.encrypt(newPassword));
1109: user.setPasswordUnencrypted(newPassword);
1110: user.setPasswordEncrypted(true);
1111: user.setPasswordReset(passwordReset);
1112:
1113: userPersistence.update(user);
1114: } else {
1115: newPassword = user.getPassword();
1116: }
1117:
1118: try {
1119: String fromName = PrefsPropsUtil.getString(companyId,
1120: PropsUtil.ADMIN_EMAIL_FROM_NAME);
1121: String fromAddress = PrefsPropsUtil.getString(companyId,
1122: PropsUtil.ADMIN_EMAIL_FROM_ADDRESS);
1123:
1124: String toName = user.getFullName();
1125: String toAddress = user.getEmailAddress();
1126:
1127: String subject = PrefsPropsUtil.getContent(companyId,
1128: PropsUtil.ADMIN_EMAIL_PASSWORD_SENT_SUBJECT);
1129: String body = PrefsPropsUtil.getContent(companyId,
1130: PropsUtil.ADMIN_EMAIL_PASSWORD_SENT_BODY);
1131:
1132: subject = StringUtil.replace(subject, new String[] {
1133: "[$FROM_ADDRESS$]", "[$FROM_NAME$]",
1134: "[$PORTAL_URL$]", "[$REMOTE_ADDRESS$]",
1135: "[$REMOTE_HOST$]", "[$TO_ADDRESS$]", "[$TO_NAME$]",
1136: "[$USER_AGENT$]", "[$USER_ID$]",
1137: "[$USER_PASSWORD$]" }, new String[] { fromAddress,
1138: fromName, company.getVirtualHost(), remoteAddr,
1139: remoteHost, toAddress, toName,
1140: Html.escape(userAgent),
1141: String.valueOf(user.getUserId()), newPassword });
1142:
1143: body = StringUtil.replace(body, new String[] {
1144: "[$FROM_ADDRESS$]", "[$FROM_NAME$]",
1145: "[$PORTAL_URL$]", "[$REMOTE_ADDRESS$]",
1146: "[$REMOTE_HOST$]", "[$TO_ADDRESS$]", "[$TO_NAME$]",
1147: "[$USER_AGENT$]", "[$USER_ID$]",
1148: "[$USER_PASSWORD$]" }, new String[] { fromAddress,
1149: fromName, company.getVirtualHost(), remoteAddr,
1150: remoteHost, toAddress, toName,
1151: Html.escape(userAgent),
1152: String.valueOf(user.getUserId()), newPassword });
1153:
1154: InternetAddress from = new InternetAddress(fromAddress,
1155: fromName);
1156:
1157: InternetAddress to = new InternetAddress(toAddress, toName);
1158:
1159: MailMessage message = new MailMessage(from, to, subject,
1160: body, true);
1161:
1162: mailService.sendEmail(message);
1163: } catch (IOException ioe) {
1164: throw new SystemException(ioe);
1165: }
1166: }
1167:
1168: public void setRoleUsers(long roleId, long[] userIds)
1169: throws PortalException, SystemException {
1170:
1171: rolePersistence.setUsers(roleId, userIds);
1172:
1173: PermissionCacheUtil.clearCache();
1174: }
1175:
1176: public void setUserGroupUsers(long userGroupId, long[] userIds)
1177: throws PortalException, SystemException {
1178:
1179: userGroupPersistence.setUsers(userGroupId, userIds);
1180:
1181: PermissionCacheUtil.clearCache();
1182: }
1183:
1184: public void unsetGroupUsers(long groupId, long[] userIds)
1185: throws PortalException, SystemException {
1186:
1187: userGroupRoleLocalService
1188: .deleteUserGroupRoles(userIds, groupId);
1189:
1190: groupPersistence.removeUsers(groupId, userIds);
1191:
1192: PermissionCacheUtil.clearCache();
1193: }
1194:
1195: public void unsetOrganizationUsers(long organizationId,
1196: long[] userIds) throws PortalException, SystemException {
1197:
1198: Organization organization = organizationPersistence
1199: .findByPrimaryKey(organizationId);
1200:
1201: Group group = organization.getGroup();
1202:
1203: long groupId = group.getGroupId();
1204:
1205: userGroupRoleLocalService
1206: .deleteUserGroupRoles(userIds, groupId);
1207:
1208: organizationPersistence.removeUsers(organizationId, userIds);
1209:
1210: PermissionCacheUtil.clearCache();
1211: }
1212:
1213: public void unsetPasswordPolicyUsers(long passwordPolicyId,
1214: long[] userIds) throws PortalException, SystemException {
1215:
1216: passwordPolicyRelLocalService.deletePasswordPolicyRels(
1217: passwordPolicyId, User.class.getName(), userIds);
1218: }
1219:
1220: public void unsetRoleUsers(long roleId, long[] userIds)
1221: throws PortalException, SystemException {
1222:
1223: rolePersistence.removeUsers(roleId, userIds);
1224:
1225: PermissionCacheUtil.clearCache();
1226: }
1227:
1228: public void unsetUserGroupUsers(long userGroupId, long[] userIds)
1229: throws PortalException, SystemException {
1230:
1231: userGroupPersistence.removeUsers(userGroupId, userIds);
1232:
1233: PermissionCacheUtil.clearCache();
1234: }
1235:
1236: public User updateActive(long userId, boolean active)
1237: throws PortalException, SystemException {
1238:
1239: User user = userPersistence.findByPrimaryKey(userId);
1240:
1241: user.setActive(active);
1242:
1243: userPersistence.update(user);
1244:
1245: return user;
1246: }
1247:
1248: public User updateAgreedToTermsOfUse(long userId,
1249: boolean agreedToTermsOfUse) throws PortalException,
1250: SystemException {
1251:
1252: User user = userPersistence.findByPrimaryKey(userId);
1253:
1254: user.setAgreedToTermsOfUse(agreedToTermsOfUse);
1255:
1256: userPersistence.update(user);
1257:
1258: return user;
1259: }
1260:
1261: public User updateCreateDate(long userId, Date createDate)
1262: throws PortalException, SystemException {
1263:
1264: User user = userPersistence.findByPrimaryKey(userId);
1265:
1266: user.setCreateDate(createDate);
1267:
1268: userPersistence.update(user);
1269:
1270: return user;
1271: }
1272:
1273: public User updateLastLogin(long userId, String loginIP)
1274: throws PortalException, SystemException {
1275:
1276: User user = userPersistence.findByPrimaryKey(userId);
1277:
1278: Date lastLoginDate = user.getLoginDate();
1279:
1280: if (lastLoginDate == null) {
1281: lastLoginDate = new Date();
1282: }
1283:
1284: user.setLoginDate(new Date());
1285: user.setLoginIP(loginIP);
1286: user.setLastLoginDate(lastLoginDate);
1287: user.setLastLoginIP(user.getLoginIP());
1288: user.setLastFailedLoginDate(null);
1289: user.setFailedLoginAttempts(0);
1290:
1291: userPersistence.update(user);
1292:
1293: return user;
1294: }
1295:
1296: public User updateLockout(User user, boolean lockout)
1297: throws PortalException, SystemException {
1298:
1299: PasswordPolicy passwordPolicy = user.getPasswordPolicy();
1300:
1301: if ((passwordPolicy == null) || !passwordPolicy.isLockout()) {
1302: return user;
1303: }
1304:
1305: Date lockoutDate = null;
1306:
1307: if (lockout) {
1308: lockoutDate = new Date();
1309: }
1310:
1311: user.setLockout(lockout);
1312: user.setLockoutDate(lockoutDate);
1313:
1314: if (!lockout) {
1315: user.setLastFailedLoginDate(lockoutDate);
1316: user.setFailedLoginAttempts(0);
1317: }
1318:
1319: userPersistence.update(user);
1320:
1321: return user;
1322: }
1323:
1324: public User updateLockoutByEmailAddress(long companyId,
1325: String emailAddress, boolean lockout)
1326: throws PortalException, SystemException {
1327:
1328: User user = getUserByEmailAddress(companyId, emailAddress);
1329:
1330: return updateLockout(user, lockout);
1331: }
1332:
1333: public User updateLockoutById(long userId, boolean lockout)
1334: throws PortalException, SystemException {
1335:
1336: User user = userPersistence.findByPrimaryKey(userId);
1337:
1338: return updateLockout(user, lockout);
1339: }
1340:
1341: public User updateLockoutByScreenName(long companyId,
1342: String screenName, boolean lockout) throws PortalException,
1343: SystemException {
1344:
1345: User user = getUserByScreenName(companyId, screenName);
1346:
1347: return updateLockout(user, lockout);
1348: }
1349:
1350: public User updateModifiedDate(long userId, Date modifiedDate)
1351: throws PortalException, SystemException {
1352:
1353: User user = userPersistence.findByPrimaryKey(userId);
1354:
1355: user.setModifiedDate(modifiedDate);
1356:
1357: userPersistence.update(user);
1358:
1359: return user;
1360: }
1361:
1362: public void updateOrganizations(long userId,
1363: long[] newOrganizationIds) throws PortalException,
1364: SystemException {
1365:
1366: List oldOrganizations = userPersistence
1367: .getOrganizations(userId);
1368:
1369: List oldOrganizationIds = new ArrayList(oldOrganizations.size());
1370:
1371: for (int i = 0; i < oldOrganizations.size(); i++) {
1372: Organization oldOrganization = (Organization) oldOrganizations
1373: .get(i);
1374:
1375: long oldOrganizationId = oldOrganization
1376: .getOrganizationId();
1377:
1378: oldOrganizationIds.add(new Long(oldOrganizationId));
1379:
1380: if (!ArrayUtil.contains(newOrganizationIds,
1381: oldOrganizationId)) {
1382: unsetOrganizationUsers(oldOrganizationId,
1383: new long[] { userId });
1384: }
1385: }
1386:
1387: for (int i = 0; i < newOrganizationIds.length; i++) {
1388: long newOrganizationId = newOrganizationIds[i];
1389:
1390: if (!oldOrganizationIds
1391: .contains(new Long(newOrganizationId))) {
1392: addOrganizationUsers(newOrganizationId,
1393: new long[] { userId });
1394: }
1395: }
1396:
1397: PermissionCacheUtil.clearCache();
1398: }
1399:
1400: public User updatePassword(long userId, String password1,
1401: String password2, boolean passwordReset)
1402: throws PortalException, SystemException {
1403:
1404: return updatePassword(userId, password1, password2,
1405: passwordReset, false);
1406: }
1407:
1408: public User updatePassword(long userId, String password1,
1409: String password2, boolean passwordReset,
1410: boolean silentUpdate) throws PortalException,
1411: SystemException {
1412:
1413: User user = userPersistence.findByPrimaryKey(userId);
1414:
1415: // Use silentUpdate so that imported user passwords are not exported
1416: // or validated
1417:
1418: if (!silentUpdate) {
1419: validatePassword(user.getCompanyId(), userId, password1,
1420: password2);
1421: }
1422:
1423: String oldEncPwd = user.getPassword();
1424:
1425: if (!user.isPasswordEncrypted()) {
1426: oldEncPwd = PwdEncryptor.encrypt(user.getPassword());
1427: }
1428:
1429: String newEncPwd = PwdEncryptor.encrypt(password1);
1430:
1431: if (user.hasCompanyMx()) {
1432: try {
1433: mailService.updatePassword(userId, password1);
1434: } catch (RemoteException re) {
1435: throw new SystemException(re);
1436: }
1437: }
1438:
1439: user.setPassword(newEncPwd);
1440: user.setPasswordUnencrypted(password1);
1441: user.setPasswordEncrypted(true);
1442: user.setPasswordReset(passwordReset);
1443: user.setPasswordModifiedDate(new Date());
1444: user.setGraceLoginCount(0);
1445:
1446: if (!silentUpdate) {
1447: user.setPasswordModified(true);
1448: }
1449:
1450: try {
1451: userPersistence.update(user);
1452: } catch (ModelListenerException mle) {
1453: String msg = GetterUtil.getString(mle.getCause()
1454: .getMessage());
1455:
1456: if (PortalLDAPUtil.isPasswordPolicyEnabled(user
1457: .getCompanyId())) {
1458: String passwordHistory = PrefsPropsUtil.getString(user
1459: .getCompanyId(),
1460: PropsUtil.LDAP_ERROR_PASSWORD_HISTORY);
1461:
1462: if (msg.indexOf(passwordHistory) != -1) {
1463: throw new UserPasswordException(
1464: UserPasswordException.PASSWORD_ALREADY_USED);
1465: }
1466: }
1467:
1468: throw new UserPasswordException(
1469: UserPasswordException.PASSWORD_INVALID);
1470: }
1471:
1472: if (!silentUpdate) {
1473: user.setPasswordModified(false);
1474: }
1475:
1476: passwordTrackerLocalService.trackPassword(userId, oldEncPwd);
1477:
1478: return user;
1479: }
1480:
1481: public User updatePasswordManually(long userId, String password,
1482: boolean passwordEncrypted, boolean passwordReset,
1483: Date passwordModifiedDate) throws PortalException,
1484: SystemException {
1485:
1486: // This method should only be used to manually massage data
1487:
1488: User user = userPersistence.findByPrimaryKey(userId);
1489:
1490: user.setPassword(password);
1491: user.setPasswordEncrypted(passwordEncrypted);
1492: user.setPasswordReset(passwordReset);
1493: user.setPasswordModifiedDate(passwordModifiedDate);
1494:
1495: userPersistence.update(user);
1496:
1497: return user;
1498: }
1499:
1500: public void updatePasswordReset(long userId, boolean passwordReset)
1501: throws PortalException, SystemException {
1502:
1503: User user = userPersistence.findByPrimaryKey(userId);
1504:
1505: user.setPasswordReset(passwordReset);
1506:
1507: userPersistence.update(user);
1508: }
1509:
1510: public void updatePortrait(long userId, byte[] bytes)
1511: throws PortalException, SystemException {
1512:
1513: User user = userPersistence.findByPrimaryKey(userId);
1514:
1515: long imageMaxSize = GetterUtil.getLong(PropsUtil
1516: .get(PropsUtil.USERS_IMAGE_MAX_SIZE));
1517:
1518: if ((imageMaxSize > 0)
1519: && ((bytes == null) || (bytes.length > imageMaxSize))) {
1520:
1521: throw new UserPortraitException();
1522: }
1523:
1524: long portraitId = user.getPortraitId();
1525:
1526: if (portraitId <= 0) {
1527: portraitId = counterLocalService.increment();
1528:
1529: user.setPortraitId(portraitId);
1530: }
1531:
1532: ImageLocalUtil.updateImage(portraitId, bytes);
1533: }
1534:
1535: public User updateUser(long userId, String oldPassword,
1536: boolean passwordReset, String screenName,
1537: String emailAddress, String languageId, String timeZoneId,
1538: String greeting, String comments, String firstName,
1539: String middleName, String lastName, int prefixId,
1540: int suffixId, boolean male, int birthdayMonth,
1541: int birthdayDay, int birthdayYear, String smsSn,
1542: String aimSn, String icqSn, String jabberSn, String msnSn,
1543: String skypeSn, String ymSn, String jobTitle,
1544: long[] organizationIds) throws PortalException,
1545: SystemException {
1546:
1547: String newPassword1 = StringPool.BLANK;
1548: String newPassword2 = StringPool.BLANK;
1549:
1550: return updateUser(userId, oldPassword, newPassword1,
1551: newPassword2, passwordReset, screenName, emailAddress,
1552: languageId, timeZoneId, greeting, comments, firstName,
1553: middleName, lastName, prefixId, suffixId, male,
1554: birthdayMonth, birthdayDay, birthdayYear, smsSn, aimSn,
1555: icqSn, jabberSn, msnSn, skypeSn, ymSn, jobTitle,
1556: organizationIds);
1557: }
1558:
1559: public User updateUser(long userId, String oldPassword,
1560: String newPassword1, String newPassword2,
1561: boolean passwordReset, String screenName,
1562: String emailAddress, String languageId, String timeZoneId,
1563: String greeting, String comments, String firstName,
1564: String middleName, String lastName, int prefixId,
1565: int suffixId, boolean male, int birthdayMonth,
1566: int birthdayDay, int birthdayYear, String smsSn,
1567: String aimSn, String icqSn, String jabberSn, String msnSn,
1568: String skypeSn, String ymSn, String jobTitle,
1569: long[] organizationIds) throws PortalException,
1570: SystemException {
1571:
1572: // User
1573:
1574: String password = oldPassword;
1575: screenName = getScreenName(screenName);
1576: emailAddress = emailAddress.trim().toLowerCase();
1577: Date now = new Date();
1578:
1579: validate(userId, screenName, emailAddress, firstName, lastName,
1580: smsSn);
1581:
1582: if (Validator.isNotNull(newPassword1)
1583: || Validator.isNotNull(newPassword2)) {
1584:
1585: updatePassword(userId, newPassword1, newPassword2,
1586: passwordReset);
1587:
1588: password = newPassword1;
1589: }
1590:
1591: User user = userPersistence.findByPrimaryKey(userId);
1592: Company company = companyPersistence.findByPrimaryKey(user
1593: .getCompanyId());
1594:
1595: user.setModifiedDate(now);
1596:
1597: if (user.getContactId() <= 0) {
1598: user.setContactId(counterLocalService.increment());
1599: }
1600:
1601: user.setPasswordReset(passwordReset);
1602: user.setScreenName(screenName);
1603:
1604: if (!emailAddress.equalsIgnoreCase(user.getEmailAddress())) {
1605:
1606: // test@test.com -> test@liferay.com
1607:
1608: try {
1609: if (!user.hasCompanyMx()
1610: && user.hasCompanyMx(emailAddress)) {
1611: mailService.addUser(userId, password, firstName,
1612: middleName, lastName, emailAddress);
1613: }
1614:
1615: // test@liferay.com -> bob@liferay.com
1616:
1617: else if (user.hasCompanyMx()
1618: && user.hasCompanyMx(emailAddress)) {
1619:
1620: mailService
1621: .updateEmailAddress(userId, emailAddress);
1622: }
1623:
1624: // test@liferay.com -> test@test.com
1625:
1626: else if (user.hasCompanyMx()
1627: && !user.hasCompanyMx(emailAddress)) {
1628:
1629: mailService.deleteEmailAddress(userId);
1630: }
1631: } catch (RemoteException re) {
1632: throw new SystemException(re);
1633: }
1634:
1635: user.setEmailAddress(emailAddress);
1636: }
1637:
1638: user.setLanguageId(languageId);
1639: user.setTimeZoneId(timeZoneId);
1640: user.setGreeting(greeting);
1641: user.setComments(comments);
1642:
1643: userPersistence.update(user);
1644:
1645: // Contact
1646:
1647: Date birthday = PortalUtil.getDate(birthdayMonth, birthdayDay,
1648: birthdayYear, new ContactBirthdayException());
1649:
1650: long contactId = user.getContactId();
1651:
1652: Contact contact = null;
1653:
1654: try {
1655: contact = contactPersistence.findByPrimaryKey(contactId);
1656: } catch (NoSuchContactException nsce) {
1657: contact = contactPersistence.create(contactId);
1658:
1659: contact.setCompanyId(user.getCompanyId());
1660: contact.setUserName(StringPool.BLANK);
1661: contact.setCreateDate(now);
1662: contact.setAccountId(company.getAccountId());
1663: contact
1664: .setParentContactId(ContactImpl.DEFAULT_PARENT_CONTACT_ID);
1665: }
1666:
1667: contact.setModifiedDate(now);
1668: contact.setFirstName(firstName);
1669: contact.setMiddleName(middleName);
1670: contact.setLastName(lastName);
1671: contact.setPrefixId(prefixId);
1672: contact.setSuffixId(suffixId);
1673: contact.setMale(male);
1674: contact.setBirthday(birthday);
1675: contact.setSmsSn(smsSn);
1676: contact.setAimSn(aimSn);
1677: contact.setIcqSn(icqSn);
1678: contact.setJabberSn(jabberSn);
1679: contact.setMsnSn(msnSn);
1680: contact.setSkypeSn(skypeSn);
1681: contact.setYmSn(ymSn);
1682: contact.setJobTitle(jobTitle);
1683:
1684: contactPersistence.update(contact);
1685:
1686: // Organizations
1687:
1688: updateOrganizations(userId, organizationIds);
1689:
1690: // Permission cache
1691:
1692: PermissionCacheUtil.clearCache();
1693:
1694: return user;
1695: }
1696:
1697: protected int authenticate(long companyId, String login,
1698: String password, String authType, Map headerMap,
1699: Map parameterMap) throws PortalException, SystemException {
1700:
1701: login = login.trim().toLowerCase();
1702:
1703: long userId = GetterUtil.getLong(login);
1704:
1705: // User input validation
1706:
1707: if (authType.equals(CompanyImpl.AUTH_TYPE_EA)) {
1708: if (!Validator.isEmailAddress(login)) {
1709: throw new UserEmailAddressException();
1710: }
1711: } else if (authType.equals(CompanyImpl.AUTH_TYPE_SN)) {
1712: if (Validator.isNull(login)) {
1713: throw new UserScreenNameException();
1714: }
1715: } else if (authType.equals(CompanyImpl.AUTH_TYPE_ID)) {
1716: if (Validator.isNull(login)) {
1717: throw new UserIdException();
1718: }
1719: }
1720:
1721: if (Validator.isNull(password)) {
1722: throw new UserPasswordException(
1723: UserPasswordException.PASSWORD_INVALID);
1724: }
1725:
1726: int authResult = Authenticator.FAILURE;
1727:
1728: // Pre-authentication pipeline
1729:
1730: String[] authPipelinePre = PropsUtil
1731: .getArray(PropsUtil.AUTH_PIPELINE_PRE);
1732:
1733: if (authType.equals(CompanyImpl.AUTH_TYPE_EA)) {
1734: authResult = AuthPipeline.authenticateByEmailAddress(
1735: authPipelinePre, companyId, login, password,
1736: headerMap, parameterMap);
1737: } else if (authType.equals(CompanyImpl.AUTH_TYPE_SN)) {
1738: authResult = AuthPipeline.authenticateByScreenName(
1739: authPipelinePre, companyId, login, password,
1740: headerMap, parameterMap);
1741: } else if (authType.equals(CompanyImpl.AUTH_TYPE_ID)) {
1742: authResult = AuthPipeline.authenticateByUserId(
1743: authPipelinePre, companyId, userId, password,
1744: headerMap, parameterMap);
1745: }
1746:
1747: // Get user
1748:
1749: User user = null;
1750:
1751: try {
1752: if (authType.equals(CompanyImpl.AUTH_TYPE_EA)) {
1753: user = userPersistence.findByC_EA(companyId, login);
1754: } else if (authType.equals(CompanyImpl.AUTH_TYPE_SN)) {
1755: user = userPersistence.findByC_SN(companyId, login);
1756: } else if (authType.equals(CompanyImpl.AUTH_TYPE_ID)) {
1757: user = userPersistence.findByC_U(companyId, GetterUtil
1758: .getLong(login));
1759: }
1760: } catch (NoSuchUserException nsue) {
1761: return Authenticator.DNE;
1762: }
1763:
1764: if (user.isDefaultUser()) {
1765: _log
1766: .error("The default user should never be allowed to authenticate");
1767:
1768: return Authenticator.DNE;
1769: }
1770:
1771: if (!user.isPasswordEncrypted()) {
1772: user.setPassword(PwdEncryptor.encrypt(user.getPassword()));
1773: user.setPasswordEncrypted(true);
1774:
1775: userPersistence.update(user);
1776: }
1777:
1778: // Check password policy to see if the is account locked out or if the
1779: // password is expired
1780:
1781: checkLockout(user);
1782:
1783: checkPasswordExpired(user);
1784:
1785: // Authenticate against the User_ table
1786:
1787: if (authResult == Authenticator.SUCCESS) {
1788: if (PropsValues.AUTH_PIPELINE_ENABLE_LIFERAY_CHECK) {
1789: String encPwd = PwdEncryptor.encrypt(password, user
1790: .getPassword());
1791:
1792: if (user.getPassword().equals(encPwd)) {
1793: authResult = Authenticator.SUCCESS;
1794: } else if (GetterUtil.getBoolean(PropsUtil
1795: .get(PropsUtil.AUTH_MAC_ALLOW))) {
1796:
1797: try {
1798: MessageDigest digester = MessageDigest
1799: .getInstance(PropsUtil
1800: .get(PropsUtil.AUTH_MAC_ALGORITHM));
1801:
1802: digester.update(login.getBytes("UTF8"));
1803:
1804: String shardKey = PropsUtil
1805: .get(PropsUtil.AUTH_MAC_SHARED_KEY);
1806:
1807: encPwd = Base64.encode(digester.digest(shardKey
1808: .getBytes("UTF8")));
1809:
1810: if (password.equals(encPwd)) {
1811: authResult = Authenticator.SUCCESS;
1812: } else {
1813: authResult = Authenticator.FAILURE;
1814: }
1815: } catch (NoSuchAlgorithmException nsae) {
1816: throw new SystemException(nsae);
1817: } catch (UnsupportedEncodingException uee) {
1818: throw new SystemException(uee);
1819: }
1820: } else {
1821: authResult = Authenticator.FAILURE;
1822: }
1823: }
1824: }
1825:
1826: // Post-authentication pipeline
1827:
1828: if (authResult == Authenticator.SUCCESS) {
1829: String[] authPipelinePost = PropsUtil
1830: .getArray(PropsUtil.AUTH_PIPELINE_POST);
1831:
1832: if (authType.equals(CompanyImpl.AUTH_TYPE_EA)) {
1833: authResult = AuthPipeline.authenticateByEmailAddress(
1834: authPipelinePost, companyId, login, password,
1835: headerMap, parameterMap);
1836: } else if (authType.equals(CompanyImpl.AUTH_TYPE_SN)) {
1837: authResult = AuthPipeline.authenticateByScreenName(
1838: authPipelinePost, companyId, login, password,
1839: headerMap, parameterMap);
1840: } else if (authType.equals(CompanyImpl.AUTH_TYPE_ID)) {
1841: authResult = AuthPipeline.authenticateByUserId(
1842: authPipelinePost, companyId, userId, password,
1843: headerMap, parameterMap);
1844: }
1845: }
1846:
1847: // Execute code triggered by authentication failure
1848:
1849: if (authResult == Authenticator.FAILURE) {
1850: try {
1851: String[] authFailure = PropsUtil
1852: .getArray(PropsUtil.AUTH_FAILURE);
1853:
1854: if (authType.equals(CompanyImpl.AUTH_TYPE_EA)) {
1855: AuthPipeline.onFailureByEmailAddress(authFailure,
1856: companyId, login, headerMap, parameterMap);
1857: } else if (authType.equals(CompanyImpl.AUTH_TYPE_SN)) {
1858: AuthPipeline.onFailureByScreenName(authFailure,
1859: companyId, login, headerMap, parameterMap);
1860: } else if (authType.equals(CompanyImpl.AUTH_TYPE_ID)) {
1861: AuthPipeline.onFailureByUserId(authFailure,
1862: companyId, userId, headerMap, parameterMap);
1863: }
1864:
1865: // Let LDAP handle max failure event
1866:
1867: if (!PortalLDAPUtil.isPasswordPolicyEnabled(user
1868: .getCompanyId())) {
1869:
1870: PasswordPolicy passwordPolicy = user
1871: .getPasswordPolicy();
1872:
1873: int failedLoginAttempts = user
1874: .getFailedLoginAttempts();
1875: int maxFailures = passwordPolicy.getMaxFailure();
1876:
1877: if ((failedLoginAttempts >= maxFailures)
1878: && (maxFailures != 0)) {
1879:
1880: String[] authMaxFailures = PropsUtil
1881: .getArray(PropsUtil.AUTH_MAX_FAILURES);
1882:
1883: if (authType.equals(CompanyImpl.AUTH_TYPE_EA)) {
1884: AuthPipeline.onMaxFailuresByEmailAddress(
1885: authMaxFailures, companyId, login,
1886: headerMap, parameterMap);
1887: } else if (authType
1888: .equals(CompanyImpl.AUTH_TYPE_SN)) {
1889: AuthPipeline.onMaxFailuresByScreenName(
1890: authMaxFailures, companyId, login,
1891: headerMap, parameterMap);
1892: } else if (authType
1893: .equals(CompanyImpl.AUTH_TYPE_ID)) {
1894: AuthPipeline.onMaxFailuresByUserId(
1895: authMaxFailures, companyId, userId,
1896: headerMap, parameterMap);
1897: }
1898: }
1899: }
1900: } catch (Exception e) {
1901: _log.error(e, e);
1902: }
1903: }
1904:
1905: return authResult;
1906: }
1907:
1908: protected String getScreenName(String screenName) {
1909: return Normalizer.normalizeToAscii(screenName.trim()
1910: .toLowerCase());
1911: }
1912:
1913: protected void sendEmail(User user, String password)
1914: throws PortalException, SystemException {
1915:
1916: if (!PrefsPropsUtil.getBoolean(user.getCompanyId(),
1917: PropsUtil.ADMIN_EMAIL_USER_ADDED_ENABLED)) {
1918:
1919: return;
1920: }
1921:
1922: try {
1923: long companyId = user.getCompanyId();
1924:
1925: Company company = companyPersistence
1926: .findByPrimaryKey(companyId);
1927:
1928: String fromName = PrefsPropsUtil.getString(companyId,
1929: PropsUtil.ADMIN_EMAIL_FROM_NAME);
1930: String fromAddress = PrefsPropsUtil.getString(companyId,
1931: PropsUtil.ADMIN_EMAIL_FROM_ADDRESS);
1932:
1933: String toName = user.getFullName();
1934: String toAddress = user.getEmailAddress();
1935:
1936: String subject = PrefsPropsUtil.getContent(companyId,
1937: PropsUtil.ADMIN_EMAIL_USER_ADDED_SUBJECT);
1938: String body = PrefsPropsUtil.getContent(companyId,
1939: PropsUtil.ADMIN_EMAIL_USER_ADDED_BODY);
1940:
1941: subject = StringUtil.replace(subject, new String[] {
1942: "[$FROM_ADDRESS$]", "[$FROM_NAME$]",
1943: "[$PORTAL_URL$]", "[$TO_ADDRESS$]", "[$TO_NAME$]",
1944: "[$USER_ID$]", "[$USER_PASSWORD$]" }, new String[] {
1945: fromAddress, fromName, company.getVirtualHost(),
1946: toAddress, toName,
1947: String.valueOf(user.getUserId()), password });
1948:
1949: body = StringUtil.replace(body, new String[] {
1950: "[$FROM_ADDRESS$]", "[$FROM_NAME$]",
1951: "[$PORTAL_URL$]", "[$TO_ADDRESS$]", "[$TO_NAME$]",
1952: "[$USER_ID$]", "[$USER_PASSWORD$]" }, new String[] {
1953: fromAddress, fromName, company.getVirtualHost(),
1954: toAddress, toName,
1955: String.valueOf(user.getUserId()), password });
1956:
1957: InternetAddress from = new InternetAddress(fromAddress,
1958: fromName);
1959:
1960: InternetAddress to = new InternetAddress(toAddress, toName);
1961:
1962: MailMessage message = new MailMessage(from, to, subject,
1963: body, true);
1964:
1965: mailService.sendEmail(message);
1966: } catch (IOException ioe) {
1967: throw new SystemException(ioe);
1968: }
1969: }
1970:
1971: protected void validate(long userId, String screenName,
1972: String emailAddress, String firstName, String lastName,
1973: String smsSn) throws PortalException, SystemException {
1974:
1975: User user = userPersistence.findByPrimaryKey(userId);
1976:
1977: if (!user.getScreenName().equalsIgnoreCase(screenName)) {
1978: validateScreenName(user.getCompanyId(), screenName);
1979: }
1980:
1981: validateEmailAddress(emailAddress);
1982:
1983: if (!user.isDefaultUser()) {
1984: try {
1985: if (!user.getEmailAddress().equalsIgnoreCase(
1986: emailAddress)) {
1987: if (userPersistence.findByC_EA(user.getCompanyId(),
1988: emailAddress) != null) {
1989:
1990: throw new DuplicateUserEmailAddressException();
1991: }
1992: }
1993: } catch (NoSuchUserException nsue) {
1994: }
1995:
1996: String[] reservedEmailAddresses = PrefsPropsUtil
1997: .getStringArray(user.getCompanyId(),
1998: PropsUtil.ADMIN_RESERVED_EMAIL_ADDRESSES,
1999: StringPool.NEW_LINE,
2000: PropsValues.ADMIN_RESERVED_EMAIL_ADDRESSES);
2001:
2002: for (int i = 0; i < reservedEmailAddresses.length; i++) {
2003: if (emailAddress
2004: .equalsIgnoreCase(reservedEmailAddresses[i])) {
2005: throw new ReservedUserEmailAddressException();
2006: }
2007: }
2008:
2009: if (Validator.isNull(firstName)) {
2010: throw new ContactFirstNameException();
2011: } else if (Validator.isNull(lastName)) {
2012: throw new ContactLastNameException();
2013: }
2014: }
2015:
2016: if (Validator.isNotNull(smsSn)
2017: && !Validator.isEmailAddress(smsSn)) {
2018: throw new UserSmsException();
2019: }
2020: }
2021:
2022: protected void validate(long companyId, boolean autoPassword,
2023: String password1, String password2, boolean autoScreenName,
2024: String screenName, String emailAddress, String firstName,
2025: String lastName, long[] organizationIds)
2026: throws PortalException, SystemException {
2027:
2028: if (!autoScreenName) {
2029: validateScreenName(companyId, screenName);
2030: }
2031:
2032: if (!autoPassword) {
2033: PasswordPolicy passwordPolicy = passwordPolicyLocalService
2034: .getDefaultPasswordPolicy(companyId);
2035:
2036: PwdToolkitUtil.validate(companyId, 0, password1, password2,
2037: passwordPolicy);
2038: }
2039:
2040: validateEmailAddress(emailAddress);
2041:
2042: try {
2043: User user = userPersistence.findByC_EA(companyId,
2044: emailAddress);
2045:
2046: if (user != null) {
2047: throw new DuplicateUserEmailAddressException();
2048: }
2049: } catch (NoSuchUserException nsue) {
2050: }
2051:
2052: String[] reservedEmailAddresses = PrefsPropsUtil
2053: .getStringArray(companyId,
2054: PropsUtil.ADMIN_RESERVED_EMAIL_ADDRESSES,
2055: StringPool.NEW_LINE,
2056: PropsValues.ADMIN_RESERVED_EMAIL_ADDRESSES);
2057:
2058: for (int i = 0; i < reservedEmailAddresses.length; i++) {
2059: if (emailAddress
2060: .equalsIgnoreCase(reservedEmailAddresses[i])) {
2061: throw new ReservedUserEmailAddressException();
2062: }
2063: }
2064:
2065: if (Validator.isNull(firstName)) {
2066: throw new ContactFirstNameException();
2067: } else if (Validator.isNull(lastName)) {
2068: throw new ContactLastNameException();
2069: }
2070: }
2071:
2072: protected void validateEmailAddress(String emailAddress)
2073: throws PortalException {
2074:
2075: if (!Validator.isEmailAddress(emailAddress)
2076: || emailAddress.startsWith("root@")
2077: || emailAddress.startsWith("postmaster@")) {
2078:
2079: throw new UserEmailAddressException();
2080: }
2081: }
2082:
2083: protected void validatePassword(long companyId, long userId,
2084: String password1, String password2) throws PortalException,
2085: SystemException {
2086:
2087: if (Validator.isNull(password1) || Validator.isNull(password2)) {
2088: throw new UserPasswordException(
2089: UserPasswordException.PASSWORD_INVALID);
2090: }
2091:
2092: if (!password1.equals(password2)) {
2093: throw new UserPasswordException(
2094: UserPasswordException.PASSWORDS_DO_NOT_MATCH);
2095: }
2096:
2097: PasswordPolicy passwordPolicy = passwordPolicyLocalService
2098: .getPasswordPolicyByUserId(userId);
2099:
2100: PwdToolkitUtil.validate(companyId, userId, password1,
2101: password2, passwordPolicy);
2102: }
2103:
2104: protected void validateScreenName(long companyId, String screenName)
2105: throws PortalException, SystemException {
2106:
2107: if (Validator.isNull(screenName)) {
2108: throw new UserScreenNameException();
2109: }
2110:
2111: ScreenNameValidator screenNameValidator = (ScreenNameValidator) InstancePool
2112: .get(PropsValues.USERS_SCREEN_NAME_VALIDATOR);
2113:
2114: if (screenNameValidator != null) {
2115: if (!screenNameValidator.validate(companyId, screenName)) {
2116: throw new UserScreenNameException();
2117: }
2118: }
2119:
2120: String[] anonymousNames = PrincipalBean.ANONYMOUS_NAMES;
2121:
2122: for (int i = 0; i < anonymousNames.length; i++) {
2123: if (screenName.equalsIgnoreCase(anonymousNames[i])) {
2124: throw new UserScreenNameException();
2125: }
2126: }
2127:
2128: User user = userPersistence.fetchByC_SN(companyId, screenName);
2129:
2130: if (user != null) {
2131: throw new DuplicateUserScreenNameException();
2132: }
2133:
2134: String friendlyURL = StringPool.SLASH + screenName;
2135:
2136: Group group = groupPersistence.fetchByC_F(companyId,
2137: friendlyURL);
2138:
2139: if (group != null) {
2140: throw new DuplicateUserScreenNameException();
2141: }
2142:
2143: String[] reservedScreenNames = PrefsPropsUtil.getStringArray(
2144: companyId, PropsUtil.ADMIN_RESERVED_SCREEN_NAMES,
2145: StringPool.NEW_LINE,
2146: PropsValues.ADMIN_RESERVED_SCREEN_NAMES);
2147:
2148: for (int i = 0; i < reservedScreenNames.length; i++) {
2149: if (screenName.equalsIgnoreCase(reservedScreenNames[i])) {
2150: throw new ReservedUserScreenNameException();
2151: }
2152: }
2153: }
2154:
2155: private static Log _log = LogFactory
2156: .getLog(UserLocalServiceImpl.class);
2157:
2158: }
|