1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.security.pwd;
16  
17  import com.liferay.portal.UserPasswordException;
18  import com.liferay.portal.kernel.exception.PortalException;
19  import com.liferay.portal.kernel.exception.SystemException;
20  import com.liferay.portal.kernel.util.InstancePool;
21  import com.liferay.portal.kernel.util.PropsKeys;
22  import com.liferay.portal.model.PasswordPolicy;
23  import com.liferay.portal.security.ldap.LDAPSettingsUtil;
24  import com.liferay.portal.util.PropsUtil;
25  
26  /**
27   * <a href="PwdToolkitUtil.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class PwdToolkitUtil {
32  
33      public static String generate() {
34          return _instance._generate();
35      }
36  
37      public static void validate(
38              long companyId, long userId, String password1, String password2,
39              PasswordPolicy passwordPolicy)
40          throws PortalException, SystemException {
41  
42          if (!password1.equals(password2)) {
43              throw new UserPasswordException(
44                  UserPasswordException.PASSWORDS_DO_NOT_MATCH);
45          }
46  
47          if (!LDAPSettingsUtil.isPasswordPolicyEnabled(companyId)) {
48              _instance._validate(userId, password1, password2, passwordPolicy);
49          }
50      }
51  
52      private PwdToolkitUtil() {
53          _toolkit = (BasicToolkit)InstancePool.get(
54              PropsUtil.get(PropsKeys.PASSWORDS_TOOLKIT));
55      }
56  
57      private String _generate() {
58          return _toolkit.generate();
59      }
60  
61      private void _validate(
62              long userId, String password1, String password2,
63              PasswordPolicy passwordPolicy)
64          throws PortalException, SystemException {
65  
66          _toolkit.validate(userId, password1, password2, passwordPolicy);
67      }
68  
69      private static PwdToolkitUtil _instance = new PwdToolkitUtil();
70  
71      private BasicToolkit _toolkit;
72  
73  }