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