001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.security.ldap;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.log.LogUtil;
021    import com.liferay.portal.kernel.util.PropertiesUtil;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.util.PrefsPropsUtil;
026    import com.liferay.portal.util.PropsValues;
027    
028    import java.io.IOException;
029    
030    import java.util.Properties;
031    
032    /**
033     * @author Edward Han
034     * @author Michael C. Han
035     * @author Brian Wing Shun Chan
036     */
037    public class LDAPSettingsUtil {
038    
039            public static String getAuthSearchFilter(
040                            long ldapServerId, long companyId, String emailAddress,
041                            String screenName, String userId)
042                    throws SystemException {
043    
044                    String postfix = getPropertyPostfix(ldapServerId);
045    
046                    String filter = PrefsPropsUtil.getString(
047                            companyId, PropsKeys.LDAP_AUTH_SEARCH_FILTER + postfix);
048    
049                    if (_log.isDebugEnabled()) {
050                            _log.debug("Search filter before transformation " + filter);
051                    }
052    
053                    filter = StringUtil.replace(
054                            filter,
055                            new String[] {
056                                    "@company_id@", "@email_address@", "@screen_name@", "@user_id@"
057                            },
058                            new String[] {
059                                    String.valueOf(companyId), emailAddress, screenName,
060                                    userId
061                            });
062    
063                    if (_log.isDebugEnabled()) {
064                            _log.debug("Search filter after transformation " + filter);
065                    }
066    
067                    return filter;
068            }
069    
070            public static Properties getContactExpandoMappings(
071                            long ldapServerId, long companyId)
072                    throws Exception {
073    
074                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
075    
076                    Properties contactExpandoMappings = PropertiesUtil.load(
077                            PrefsPropsUtil.getString(
078                                    companyId, PropsKeys.LDAP_CONTACT_CUSTOM_MAPPINGS + postfix));
079    
080                    LogUtil.debug(_log, contactExpandoMappings);
081    
082                    return contactExpandoMappings;
083            }
084    
085            public static Properties getContactMappings(
086                    long ldapServerId, long companyId)
087                    throws IOException, SystemException {
088    
089                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
090    
091                    Properties contactMappings = PropertiesUtil.load(
092                            PrefsPropsUtil.getString(companyId,
093                                    PropsKeys.LDAP_CONTACT_MAPPINGS + postfix));
094    
095                    LogUtil.debug(_log, contactMappings);
096    
097                    return contactMappings;
098            }
099    
100            public static Properties getGroupMappings(long ldapServerId, long companyId)
101                    throws Exception {
102    
103                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
104    
105                    Properties groupMappings = PropertiesUtil.load(
106                            PrefsPropsUtil.getString(
107                                    companyId, PropsKeys.LDAP_GROUP_MAPPINGS + postfix));
108    
109                    LogUtil.debug(_log, groupMappings);
110    
111                    return groupMappings;
112            }
113    
114            public static String getPropertyPostfix(long ldapServerId) {
115                    if (ldapServerId > 0) {
116                            return StringPool.PERIOD + ldapServerId;
117                    }
118    
119                    return StringPool.BLANK;
120            }
121    
122            public static Properties getUserExpandoMappings(
123                            long ldapServerId, long companyId)
124                    throws Exception {
125    
126                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
127    
128                    Properties userExpandoMappings = PropertiesUtil.load(
129                            PrefsPropsUtil.getString(
130                                    companyId, PropsKeys.LDAP_USER_CUSTOM_MAPPINGS + postfix));
131    
132                    LogUtil.debug(_log, userExpandoMappings);
133    
134                    return userExpandoMappings;
135            }
136    
137            public static Properties getUserMappings(long ldapServerId, long companyId)
138                    throws Exception {
139    
140                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
141    
142                    Properties userMappings = PropertiesUtil.load(
143                            PrefsPropsUtil.getString(
144                                    companyId, PropsKeys.LDAP_USER_MAPPINGS + postfix));
145    
146                    LogUtil.debug(_log, userMappings);
147    
148                    return userMappings;
149            }
150    
151            public static boolean isAuthEnabled(long companyId) throws SystemException {
152                    if (PrefsPropsUtil.getBoolean(
153                                    companyId, PropsKeys.LDAP_AUTH_ENABLED,
154                                    PropsValues.LDAP_AUTH_ENABLED)) {
155    
156                            return true;
157                    }
158                    else {
159                            return false;
160                    }
161            }
162    
163            public static boolean isExportEnabled(long companyId)
164                    throws SystemException {
165    
166                    if (PrefsPropsUtil.getBoolean(
167                                    companyId, PropsKeys.LDAP_EXPORT_ENABLED,
168                                    PropsValues.LDAP_EXPORT_ENABLED)) {
169    
170                            return true;
171                    }
172                    else {
173                            return false;
174                    }
175            }
176    
177            public static boolean isImportEnabled(long companyId)
178                    throws SystemException {
179    
180                    if (PrefsPropsUtil.getBoolean(
181                                    companyId, PropsKeys.LDAP_IMPORT_ENABLED,
182                                    PropsValues.LDAP_IMPORT_ENABLED)) {
183    
184                            return true;
185                    }
186                    else {
187                            return false;
188                    }
189            }
190    
191            public static boolean isImportOnStartup(long companyId)
192                    throws SystemException {
193    
194                    if (PrefsPropsUtil.getBoolean(
195                                    companyId, PropsKeys.LDAP_IMPORT_ON_STARTUP)) {
196    
197                            return true;
198                    }
199                    else {
200                            return false;
201                    }
202            }
203    
204            public static boolean isNtlmEnabled(long companyId)
205                    throws SystemException {
206    
207                    if (!isAuthEnabled(companyId)) {
208                            return false;
209                    }
210    
211                    if (PrefsPropsUtil.getBoolean(
212                                    companyId, PropsKeys.NTLM_AUTH_ENABLED,
213                                    PropsValues.NTLM_AUTH_ENABLED)) {
214    
215                            return true;
216                    }
217                    else {
218                            return false;
219                    }
220            }
221    
222            public static boolean isPasswordPolicyEnabled(long companyId)
223                    throws SystemException {
224    
225                    if (PrefsPropsUtil.getBoolean(
226                                    companyId, PropsKeys.LDAP_PASSWORD_POLICY_ENABLED,
227                                    PropsValues.LDAP_PASSWORD_POLICY_ENABLED)) {
228    
229                            return true;
230                    }
231                    else {
232                            return false;
233                    }
234            }
235    
236            public static boolean isSiteMinderEnabled(long companyId)
237                    throws SystemException {
238    
239                    if (!isAuthEnabled(companyId)) {
240                            return false;
241                    }
242    
243                    if (PrefsPropsUtil.getBoolean(
244                                    companyId, PropsKeys.SITEMINDER_AUTH_ENABLED,
245                                    PropsValues.SITEMINDER_AUTH_ENABLED)) {
246    
247                            return true;
248                    }
249                    else {
250                            return false;
251                    }
252            }
253    
254            private static Log _log = LogFactoryUtil.getLog(LDAPSettingsUtil.class);
255    
256    }