1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.security.pwd;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.UserPasswordException;
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  }