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.security.pwd;
016    
017    import com.liferay.portal.UserPasswordException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.InstancePool;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.model.PasswordPolicy;
023    import com.liferay.portal.security.ldap.LDAPSettingsUtil;
024    import com.liferay.portal.util.PropsUtil;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class PwdToolkitUtil {
030    
031            public static String generate(PasswordPolicy passwordPolicy) {
032                    return _instance._generate(passwordPolicy);
033            }
034    
035            public static void validate(
036                            long companyId, long userId, String password1, String password2,
037                            PasswordPolicy passwordPolicy)
038                    throws PortalException, SystemException {
039    
040                    if (!password1.equals(password2)) {
041                            throw new UserPasswordException(
042                                    UserPasswordException.PASSWORDS_DO_NOT_MATCH);
043                    }
044    
045                    if (!LDAPSettingsUtil.isPasswordPolicyEnabled(companyId)) {
046                            _instance._validate(userId, password1, password2, passwordPolicy);
047                    }
048            }
049    
050            private PwdToolkitUtil() {
051                    _toolkit = (BasicToolkit)InstancePool.get(
052                            PropsUtil.get(PropsKeys.PASSWORDS_TOOLKIT));
053            }
054    
055            private String _generate(PasswordPolicy passwordPolicy) {
056                    return _toolkit.generate(passwordPolicy);
057            }
058    
059            private void _validate(
060                            long userId, String password1, String password2,
061                            PasswordPolicy passwordPolicy)
062                    throws PortalException, SystemException {
063    
064                    _toolkit.validate(userId, password1, password2, passwordPolicy);
065            }
066    
067            private static PwdToolkitUtil _instance = new PwdToolkitUtil();
068    
069            private BasicToolkit _toolkit;
070    
071    }