001    /**
002     * Copyright (c) 2000-2013 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.model.PasswordPolicy;
021    import com.liferay.portal.security.ldap.LDAPSettingsUtil;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class PwdToolkitUtil {
027    
028            public static String generate(PasswordPolicy passwordPolicy) {
029                    return _toolkit.generate(passwordPolicy);
030            }
031    
032            public static Toolkit getToolkit() {
033                    return _toolkit;
034            }
035    
036            public static void validate(
037                            long companyId, long userId, String password1, String password2,
038                            PasswordPolicy passwordPolicy)
039                    throws PortalException, SystemException {
040    
041                    if (!password1.equals(password2)) {
042                            throw new UserPasswordException(
043                                    UserPasswordException.PASSWORDS_DO_NOT_MATCH);
044                    }
045    
046                    if (!LDAPSettingsUtil.isPasswordPolicyEnabled(companyId) &&
047                            PwdToolkitUtilThreadLocal.isValidate()) {
048    
049                            _toolkit.validate(userId, password1, password2, passwordPolicy);
050                    }
051            }
052    
053            public void setToolkit(Toolkit toolkit) {
054                    _toolkit = toolkit;
055            }
056    
057            private static Toolkit _toolkit;
058    
059    }