001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.model.PasswordPolicy;
026: import com.liferay.portal.model.PasswordTracker;
027: import com.liferay.portal.model.User;
028: import com.liferay.portal.security.pwd.PwdEncryptor;
029: import com.liferay.portal.service.base.PasswordTrackerLocalServiceBaseImpl;
030:
031: import java.util.Date;
032: import java.util.Iterator;
033:
034: /**
035: * <a href="PasswordTrackerLocalServiceImpl.java.html"><b><i>View Source</i></b>
036: * </a>
037: *
038: * @author Brian Wing Shun Chan
039: * @author Scott Lee
040: */
041: public class PasswordTrackerLocalServiceImpl extends
042: PasswordTrackerLocalServiceBaseImpl {
043:
044: public void deletePasswordTrackers(long userId)
045: throws SystemException {
046: passwordTrackerPersistence.removeByUserId(userId);
047: }
048:
049: public boolean isSameAsCurrentPassword(long userId,
050: String newClearTextPwd) throws PortalException,
051: SystemException {
052:
053: User user = userPersistence.findByPrimaryKey(userId);
054:
055: String currentPwd = user.getPassword();
056:
057: if (user.isPasswordEncrypted()) {
058: String newEncPwd = PwdEncryptor.encrypt(newClearTextPwd,
059: user.getPassword());
060:
061: if (currentPwd.equals(newEncPwd)) {
062: return true;
063: } else {
064: return false;
065: }
066: } else {
067: if (currentPwd.equals(newClearTextPwd)) {
068: return true;
069: } else {
070: return false;
071: }
072: }
073: }
074:
075: public boolean isValidPassword(long userId, String newClearTextPwd)
076: throws PortalException, SystemException {
077:
078: PasswordPolicy passwordPolicy = passwordPolicyLocalService
079: .getPasswordPolicyByUserId(userId);
080:
081: if (!passwordPolicy.getHistory()) {
082: return true;
083: }
084:
085: // Check password history
086:
087: int historyCount = 1;
088:
089: Iterator itr = passwordTrackerPersistence.findByUserId(userId)
090: .iterator();
091:
092: while (itr.hasNext()) {
093: if (historyCount > passwordPolicy.getHistoryCount()) {
094: break;
095: }
096:
097: PasswordTracker passwordTracker = (PasswordTracker) itr
098: .next();
099:
100: String oldEncPwd = passwordTracker.getPassword();
101: String newEncPwd = PwdEncryptor.encrypt(newClearTextPwd,
102: oldEncPwd);
103:
104: if (oldEncPwd.equals(newEncPwd)) {
105: return false;
106: }
107:
108: historyCount++;
109: }
110:
111: return true;
112: }
113:
114: public void trackPassword(long userId, String encPwd)
115: throws PortalException, SystemException {
116:
117: long passwordTrackerId = counterLocalService.increment();
118:
119: PasswordTracker passwordTracker = passwordTrackerPersistence
120: .create(passwordTrackerId);
121:
122: passwordTracker.setUserId(userId);
123: passwordTracker.setCreateDate(new Date());
124: passwordTracker.setPassword(encPwd);
125:
126: passwordTrackerPersistence.update(passwordTracker);
127: }
128:
129: }
|