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.DuplicatePasswordPolicyException;
024: import com.liferay.portal.NoSuchPasswordPolicyException;
025: import com.liferay.portal.NoSuchPasswordPolicyRelException;
026: import com.liferay.portal.PasswordPolicyNameException;
027: import com.liferay.portal.PortalException;
028: import com.liferay.portal.RequiredPasswordPolicyException;
029: import com.liferay.portal.SystemException;
030: import com.liferay.portal.kernel.util.OrderByComparator;
031: import com.liferay.portal.kernel.util.StringPool;
032: import com.liferay.portal.kernel.util.Validator;
033: import com.liferay.portal.model.Organization;
034: import com.liferay.portal.model.PasswordPolicy;
035: import com.liferay.portal.model.PasswordPolicyRel;
036: import com.liferay.portal.model.User;
037: import com.liferay.portal.model.impl.ResourceImpl;
038: import com.liferay.portal.security.ldap.PortalLDAPUtil;
039: import com.liferay.portal.service.base.PasswordPolicyLocalServiceBaseImpl;
040: import com.liferay.portal.util.PropsValues;
041:
042: import java.util.Date;
043: import java.util.List;
044:
045: /**
046: * <a href="PasswordPolicyLocalServiceImpl.java.html"><b><i>View Source</i></b>
047: * </a>
048: *
049: * @author Scott Lee
050: *
051: */
052: public class PasswordPolicyLocalServiceImpl extends
053: PasswordPolicyLocalServiceBaseImpl {
054:
055: public PasswordPolicy addPasswordPolicy(long userId,
056: boolean defaultPolicy, String name, String description,
057: boolean changeable, boolean changeRequired, long minAge,
058: boolean checkSyntax, boolean allowDictionaryWords,
059: int minLength, boolean history, int historyCount,
060: boolean expireable, long maxAge, long warningTime,
061: int graceLimit, boolean lockout, int maxFailure,
062: long lockoutDuration, long resetFailureCount)
063: throws PortalException, SystemException {
064:
065: // Password policy
066:
067: User user = userPersistence.findByPrimaryKey(userId);
068: Date now = new Date();
069:
070: validate(0, user.getCompanyId(), name);
071:
072: long passwordPolicyId = counterLocalService.increment();
073:
074: PasswordPolicy passwordPolicy = passwordPolicyPersistence
075: .create(passwordPolicyId);
076:
077: passwordPolicy.setUserId(userId);
078: passwordPolicy.setCompanyId(user.getCompanyId());
079: passwordPolicy.setUserName(user.getFullName());
080: passwordPolicy.setCreateDate(now);
081: passwordPolicy.setModifiedDate(now);
082: passwordPolicy.setDefaultPolicy(defaultPolicy);
083: passwordPolicy.setName(name);
084: passwordPolicy.setDescription(description);
085: passwordPolicy.setChangeable(changeable);
086: passwordPolicy.setChangeRequired(changeRequired);
087: passwordPolicy.setMinAge(minAge);
088: passwordPolicy.setCheckSyntax(checkSyntax);
089: passwordPolicy.setAllowDictionaryWords(allowDictionaryWords);
090: passwordPolicy.setMinLength(minLength);
091: passwordPolicy.setHistory(history);
092: passwordPolicy.setHistoryCount(historyCount);
093: passwordPolicy.setExpireable(expireable);
094: passwordPolicy.setMaxAge(maxAge);
095: passwordPolicy.setWarningTime(warningTime);
096: passwordPolicy.setGraceLimit(graceLimit);
097: passwordPolicy.setLockout(lockout);
098: passwordPolicy.setMaxFailure(maxFailure);
099: passwordPolicy.setLockoutDuration(lockoutDuration);
100: passwordPolicy.setRequireUnlock(lockoutDuration == 0);
101: passwordPolicy.setResetFailureCount(resetFailureCount);
102:
103: passwordPolicyPersistence.update(passwordPolicy);
104:
105: // Resources
106:
107: if (!user.isDefaultUser()) {
108: resourceLocalService.addResources(user.getCompanyId(), 0,
109: userId, PasswordPolicy.class.getName(),
110: passwordPolicy.getPasswordPolicyId(), false, false,
111: false);
112: }
113:
114: return passwordPolicy;
115: }
116:
117: public void checkDefaultPasswordPolicy(long companyId)
118: throws PortalException, SystemException {
119:
120: String defaultPasswordPolicyName = PropsValues.PASSWORDS_DEFAULT_POLICY_NAME;
121:
122: try {
123: passwordPolicyPersistence.findByC_N(companyId,
124: defaultPasswordPolicyName);
125: } catch (NoSuchPasswordPolicyException nsppe) {
126: long defaultUserId = userLocalService
127: .getDefaultUserId(companyId);
128:
129: addPasswordPolicy(defaultUserId, true,
130: defaultPasswordPolicyName,
131: defaultPasswordPolicyName, true, false, 0, false,
132: true, 6, false, 6, false, 8640000, 86400, 0, false,
133: 3, 0, 600);
134: }
135: }
136:
137: public void deletePasswordPolicy(long passwordPolicyId)
138: throws PortalException, SystemException {
139:
140: PasswordPolicy passwordPolicy = passwordPolicyPersistence
141: .findByPrimaryKey(passwordPolicyId);
142:
143: if (passwordPolicy.isDefaultPolicy()) {
144: throw new RequiredPasswordPolicyException();
145: }
146:
147: // Resources
148:
149: resourceLocalService.deleteResource(passwordPolicy
150: .getCompanyId(), PasswordPolicy.class.getName(),
151: ResourceImpl.SCOPE_INDIVIDUAL, passwordPolicy
152: .getPasswordPolicyId());
153:
154: // Password policy
155:
156: passwordPolicyPersistence.remove(passwordPolicyId);
157: }
158:
159: public PasswordPolicy getDefaultPasswordPolicy(long companyId)
160: throws PortalException, SystemException {
161:
162: if (PortalLDAPUtil.isPasswordPolicyEnabled(companyId)) {
163: return null;
164: }
165:
166: return passwordPolicyPersistence.findByC_DP(companyId, true);
167: }
168:
169: public PasswordPolicy getPasswordPolicy(long passwordPolicyId)
170: throws PortalException, SystemException {
171:
172: return passwordPolicyPersistence
173: .findByPrimaryKey(passwordPolicyId);
174: }
175:
176: /**
177: * @deprecated
178: */
179: public PasswordPolicy getPasswordPolicy(long companyId,
180: long organizationId, long locationId)
181: throws PortalException, SystemException {
182:
183: return getPasswordPolicy(companyId, new long[] {
184: organizationId, locationId });
185: }
186:
187: public PasswordPolicy getPasswordPolicy(long companyId,
188: long[] organizationIds) throws PortalException,
189: SystemException {
190:
191: if (PortalLDAPUtil.isPasswordPolicyEnabled(companyId)) {
192: return null;
193: }
194:
195: PasswordPolicyRel passwordPolicyRel = null;
196:
197: // Check for password policy specifically assigned to any of the
198: // organizations
199:
200: for (int i = 0; i < organizationIds.length; i++) {
201: long organizationId = organizationIds[i];
202:
203: try {
204: passwordPolicyRel = passwordPolicyRelLocalService
205: .getPasswordPolicyRel(Organization.class
206: .getName(), organizationId);
207:
208: return getPasswordPolicy(passwordPolicyRel
209: .getPasswordPolicyId());
210: } catch (NoSuchPasswordPolicyRelException nsppre) {
211: }
212: }
213:
214: // Get default password policy
215:
216: return getDefaultPasswordPolicy(companyId);
217: }
218:
219: public PasswordPolicy getPasswordPolicyByUserId(long userId)
220: throws PortalException, SystemException {
221:
222: User user = userPersistence.findByPrimaryKey(userId);
223:
224: if (PortalLDAPUtil.isPasswordPolicyEnabled(user.getCompanyId())) {
225: return null;
226: }
227:
228: PasswordPolicyRel passwordPolicyRel = null;
229:
230: // Check for password policy specifically assigned to this user
231:
232: try {
233: passwordPolicyRel = passwordPolicyRelLocalService
234: .getPasswordPolicyRel(User.class.getName(), userId);
235:
236: return getPasswordPolicy(passwordPolicyRel
237: .getPasswordPolicyId());
238: } catch (NoSuchPasswordPolicyRelException nsppre) {
239: }
240:
241: long[] organizationIds = user.getOrganizationIds();
242:
243: return getPasswordPolicy(user.getCompanyId(), organizationIds);
244: }
245:
246: public List search(long companyId, String name, int begin, int end,
247: OrderByComparator obc) throws SystemException {
248:
249: return passwordPolicyFinder.findByC_N(companyId, name, begin,
250: end, obc);
251: }
252:
253: public int searchCount(long companyId, String name)
254: throws SystemException {
255:
256: return passwordPolicyFinder.countByC_N(companyId, name);
257: }
258:
259: public PasswordPolicy updatePasswordPolicy(long passwordPolicyId,
260: String name, String description, boolean changeable,
261: boolean changeRequired, long minAge, boolean checkSyntax,
262: boolean allowDictionaryWords, int minLength,
263: boolean history, int historyCount, boolean expireable,
264: long maxAge, long warningTime, int graceLimit,
265: boolean lockout, int maxFailure, long lockoutDuration,
266: long resetFailureCount) throws PortalException,
267: SystemException {
268:
269: Date now = new Date();
270:
271: PasswordPolicy passwordPolicy = passwordPolicyPersistence
272: .findByPrimaryKey(passwordPolicyId);
273:
274: if (!passwordPolicy.getDefaultPolicy()) {
275: validate(passwordPolicyId, passwordPolicy.getCompanyId(),
276: name);
277:
278: passwordPolicy.setName(name);
279: }
280:
281: passwordPolicy.setModifiedDate(now);
282: passwordPolicy.setDescription(description);
283: passwordPolicy.setChangeable(changeable);
284: passwordPolicy.setChangeRequired(changeRequired);
285: passwordPolicy.setMinAge(minAge);
286: passwordPolicy.setCheckSyntax(checkSyntax);
287: passwordPolicy.setAllowDictionaryWords(allowDictionaryWords);
288: passwordPolicy.setMinLength(minLength);
289: passwordPolicy.setHistory(history);
290: passwordPolicy.setHistoryCount(historyCount);
291: passwordPolicy.setExpireable(expireable);
292: passwordPolicy.setMaxAge(maxAge);
293: passwordPolicy.setWarningTime(warningTime);
294: passwordPolicy.setGraceLimit(graceLimit);
295: passwordPolicy.setLockout(lockout);
296: passwordPolicy.setMaxFailure(maxFailure);
297: passwordPolicy.setLockoutDuration(lockoutDuration);
298: passwordPolicy.setRequireUnlock(lockoutDuration == 0);
299: passwordPolicy.setResetFailureCount(resetFailureCount);
300:
301: passwordPolicyPersistence.update(passwordPolicy);
302:
303: return passwordPolicy;
304: }
305:
306: protected void validate(long passwordPolicyId, long companyId,
307: String name) throws PortalException, SystemException {
308:
309: if ((Validator.isNull(name)) || (Validator.isNumber(name))
310: || (name.indexOf(StringPool.COMMA) != -1)
311: || (name.indexOf(StringPool.STAR) != -1)) {
312:
313: throw new PasswordPolicyNameException();
314: }
315:
316: try {
317: PasswordPolicy passwordPolicy = passwordPolicyPersistence
318: .findByC_N(companyId, name);
319:
320: if ((passwordPolicyId <= 0)
321: || (passwordPolicy.getPasswordPolicyId() != passwordPolicyId)) {
322:
323: throw new DuplicatePasswordPolicyException();
324: }
325: } catch (NoSuchPasswordPolicyException nsge) {
326: }
327: }
328:
329: }
|