001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }