001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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                    return StringPool.PERIOD + ldapServerId;
116            }
117    
118            public static Properties getUserExpandoMappings(
119                            long ldapServerId, long companyId)
120                    throws Exception {
121    
122                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
123    
124                    Properties userExpandoMappings = PropertiesUtil.load(
125                            PrefsPropsUtil.getString(
126                                    companyId, PropsKeys.LDAP_USER_CUSTOM_MAPPINGS + postfix));
127    
128                    LogUtil.debug(_log, userExpandoMappings);
129    
130                    return userExpandoMappings;
131            }
132    
133            public static Properties getUserMappings(long ldapServerId, long companyId)
134                    throws Exception {
135    
136                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
137    
138                    Properties userMappings = PropertiesUtil.load(
139                            PrefsPropsUtil.getString(
140                                    companyId, PropsKeys.LDAP_USER_MAPPINGS + postfix));
141    
142                    LogUtil.debug(_log, userMappings);
143    
144                    return userMappings;
145            }
146    
147            public static boolean isExportEnabled(long companyId)
148                    throws SystemException {
149    
150                    if (PrefsPropsUtil.getBoolean(
151                                    companyId, PropsKeys.LDAP_EXPORT_ENABLED,
152                                    PropsValues.LDAP_EXPORT_ENABLED)) {
153    
154                            return true;
155                    }
156                    else {
157                            return false;
158                    }
159            }
160    
161            public static boolean isExportGroupEnabled(long companyId)
162                    throws SystemException {
163    
164                    if (PrefsPropsUtil.getBoolean(
165                                    companyId, PropsKeys.LDAP_EXPORT_GROUP_ENABLED,
166                                    PropsValues.LDAP_EXPORT_GROUP_ENABLED)) {
167    
168                            return true;
169                    }
170                    else {
171                            return false;
172                    }
173            }
174    
175            public static boolean isImportEnabled(long companyId)
176                    throws SystemException {
177    
178                    if (PrefsPropsUtil.getBoolean(
179                                    companyId, PropsKeys.LDAP_IMPORT_ENABLED,
180                                    PropsValues.LDAP_IMPORT_ENABLED)) {
181    
182                            return true;
183                    }
184                    else {
185                            return false;
186                    }
187            }
188    
189            public static boolean isImportOnStartup(long companyId)
190                    throws SystemException {
191    
192                    if (PrefsPropsUtil.getBoolean(
193                                    companyId, PropsKeys.LDAP_IMPORT_ON_STARTUP)) {
194    
195                            return true;
196                    }
197                    else {
198                            return false;
199                    }
200            }
201    
202            public static boolean isPasswordPolicyEnabled(long companyId)
203                    throws SystemException {
204    
205                    if (PrefsPropsUtil.getBoolean(
206                                    companyId, PropsKeys.LDAP_PASSWORD_POLICY_ENABLED,
207                                    PropsValues.LDAP_PASSWORD_POLICY_ENABLED)) {
208    
209                            return true;
210                    }
211                    else {
212                            return false;
213                    }
214            }
215    
216            private static Log _log = LogFactoryUtil.getLog(LDAPSettingsUtil.class);
217    
218    }