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