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