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.ldap;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.log.LogUtil;
21  import com.liferay.portal.kernel.util.PropertiesUtil;
22  import com.liferay.portal.kernel.util.PropsKeys;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.security.auth.AuthSettingsUtil;
26  import com.liferay.portal.util.PrefsPropsUtil;
27  import com.liferay.portal.util.PropsValues;
28  
29  import java.util.Properties;
30  
31  /**
32   * <a href="LDAPSettingsUtil.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Edward Han
35   * @author Michael C. Han
36   * @author Brian Wing Shun Chan
37   */
38  public class LDAPSettingsUtil {
39  
40      public static String getAuthSearchFilter(
41              long ldapServerId, long companyId, String emailAddress,
42              String screenName, String userId)
43          throws SystemException {
44  
45          String postfix = getPropertyPostfix(ldapServerId);
46  
47          String filter = PrefsPropsUtil.getString(
48              companyId, PropsKeys.LDAP_AUTH_SEARCH_FILTER + postfix);
49  
50          if (_log.isDebugEnabled()) {
51              _log.debug("Search filter before transformation " + filter);
52          }
53  
54          filter = StringUtil.replace(
55              filter,
56              new String[] {
57                  "@company_id@", "@email_address@", "@screen_name@", "@user_id@"
58              },
59              new String[] {
60                  String.valueOf(companyId), emailAddress, screenName,
61                  userId
62              });
63  
64          if (_log.isDebugEnabled()) {
65              _log.debug("Search filter after transformation " + filter);
66          }
67  
68          return filter;
69      }
70  
71      public static Properties getGroupMappings(long ldapServerId, long companyId)
72          throws Exception {
73  
74          String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
75  
76          Properties groupMappings = PropertiesUtil.load(
77              PrefsPropsUtil.getString(
78                  companyId, PropsKeys.LDAP_GROUP_MAPPINGS + postfix));
79  
80          LogUtil.debug(_log, groupMappings);
81  
82          return groupMappings;
83      }
84  
85      public static String getPropertyPostfix(long ldapServerId) {
86          if (ldapServerId > 0) {
87              return StringPool.PERIOD + ldapServerId;
88          }
89  
90          return StringPool.BLANK;
91      }
92  
93      public static Properties getUserMappings(long ldapServerId, long companyId)
94              throws Exception {
95  
96          String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
97  
98          Properties userMappings = PropertiesUtil.load(
99              PrefsPropsUtil.getString(
100                 companyId, PropsKeys.LDAP_USER_MAPPINGS + postfix));
101 
102         LogUtil.debug(_log, userMappings);
103 
104         return userMappings;
105     }
106 
107     /**
108      * @deprecated {@link AuthSettingsUtil#isLDAPAuthEnabled(long)}
109      */
110     public static boolean isAuthEnabled(long companyId) throws SystemException {
111         return AuthSettingsUtil.isLDAPAuthEnabled(companyId);
112     }
113 
114     public static boolean isExportEnabled(long companyId)
115         throws SystemException {
116 
117         if (PrefsPropsUtil.getBoolean(
118                 companyId, PropsKeys.LDAP_EXPORT_ENABLED,
119                 PropsValues.LDAP_EXPORT_ENABLED)) {
120 
121             return true;
122         }
123         else {
124             return false;
125         }
126     }
127 
128     public static boolean isImportEnabled(long companyId)
129         throws SystemException {
130 
131         if (PrefsPropsUtil.getBoolean(
132                 companyId, PropsKeys.LDAP_IMPORT_ENABLED,
133                 PropsValues.LDAP_IMPORT_ENABLED)) {
134 
135             return true;
136         }
137         else {
138             return false;
139         }
140     }
141 
142     public static boolean isImportOnStartup(long companyId)
143         throws SystemException {
144 
145         if (PrefsPropsUtil.getBoolean(
146                 companyId, PropsKeys.LDAP_IMPORT_ON_STARTUP)) {
147 
148             return true;
149         }
150         else {
151             return false;
152         }
153     }
154 
155     /**
156      * @deprecated {@link AuthSettingsUtil#isNtlmEnabled(long)}
157      */
158     public static boolean isNtlmEnabled(long companyId)
159         throws SystemException {
160 
161         return AuthSettingsUtil.isNtlmEnabled(companyId);
162     }
163 
164     public static boolean isPasswordPolicyEnabled(long companyId)
165         throws SystemException {
166 
167         if (PrefsPropsUtil.getBoolean(
168                 companyId, PropsKeys.LDAP_PASSWORD_POLICY_ENABLED,
169                 PropsValues.LDAP_PASSWORD_POLICY_ENABLED)) {
170 
171             return true;
172         }
173         else {
174             return false;
175         }
176     }
177 
178     /**
179      * @deprecated {@link AuthSettingsUtil#isSiteMinderEnabled(long)}
180      */
181     public static boolean isSiteMinderEnabled(long companyId)
182         throws SystemException {
183 
184         return AuthSettingsUtil.isSiteMinderEnabled(companyId);
185     }
186 
187     private static Log _log = LogFactoryUtil.getLog(LDAPSettingsUtil.class);
188 
189 }