001    /**
002     * Copyright (c) 2000-2013 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.ldap.LDAPUtil;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.log.LogUtil;
022    import com.liferay.portal.kernel.util.PropertiesUtil;
023    import com.liferay.portal.kernel.util.PropsKeys;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.util.PrefsPropsUtil;
027    import com.liferay.portal.util.PropsValues;
028    
029    import java.util.Properties;
030    
031    /**
032     * @author Edward Han
033     * @author Michael C. Han
034     * @author Brian Wing Shun Chan
035     */
036    public class LDAPSettingsUtil {
037    
038            public static String getAuthSearchFilter(
039                            long ldapServerId, long companyId, String emailAddress,
040                            String screenName, String userId)
041                    throws Exception {
042    
043                    String postfix = getPropertyPostfix(ldapServerId);
044    
045                    String filter = PrefsPropsUtil.getString(
046                            companyId, PropsKeys.LDAP_AUTH_SEARCH_FILTER + postfix);
047    
048                    if (_log.isDebugEnabled()) {
049                            _log.debug("Search filter before transformation " + filter);
050                    }
051    
052                    filter = StringUtil.replace(
053                            filter,
054                            new String[] {
055                                    "@company_id@", "@email_address@", "@screen_name@", "@user_id@"
056                            },
057                            new String[] {
058                                    String.valueOf(companyId), emailAddress, screenName, userId
059                            });
060    
061                    LDAPUtil.validateFilter(filter);
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 Exception {
088    
089                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
090    
091                    Properties contactMappings = PropertiesUtil.load(
092                            PrefsPropsUtil.getString(
093                                    companyId, 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 (isImportEnabled(companyId) &&
151                            PropsValues.LDAP_IMPORT_USER_PASSWORD_AUTOGENERATED) {
152    
153                            return false;
154                    }
155    
156                    if (PrefsPropsUtil.getBoolean(
157                                    companyId, PropsKeys.LDAP_EXPORT_ENABLED,
158                                    PropsValues.LDAP_EXPORT_ENABLED)) {
159    
160                            return true;
161                    }
162                    else {
163                            return false;
164                    }
165            }
166    
167            public static boolean isExportGroupEnabled(long companyId)
168                    throws SystemException {
169    
170                    if (PrefsPropsUtil.getBoolean(
171                                    companyId, PropsKeys.LDAP_EXPORT_GROUP_ENABLED,
172                                    PropsValues.LDAP_EXPORT_GROUP_ENABLED)) {
173    
174                            return true;
175                    }
176                    else {
177                            return false;
178                    }
179            }
180    
181            public static boolean isImportEnabled(long companyId)
182                    throws SystemException {
183    
184                    if (PrefsPropsUtil.getBoolean(
185                                    companyId, PropsKeys.LDAP_IMPORT_ENABLED,
186                                    PropsValues.LDAP_IMPORT_ENABLED)) {
187    
188                            return true;
189                    }
190                    else {
191                            return false;
192                    }
193            }
194    
195            public static boolean isImportOnStartup(long companyId)
196                    throws SystemException {
197    
198                    if (PrefsPropsUtil.getBoolean(
199                                    companyId, PropsKeys.LDAP_IMPORT_ON_STARTUP)) {
200    
201                            return true;
202                    }
203                    else {
204                            return false;
205                    }
206            }
207    
208            public static boolean isPasswordPolicyEnabled(long companyId)
209                    throws SystemException {
210    
211                    if (PrefsPropsUtil.getBoolean(
212                                    companyId, PropsKeys.LDAP_PASSWORD_POLICY_ENABLED,
213                                    PropsValues.LDAP_PASSWORD_POLICY_ENABLED)) {
214    
215                            return true;
216                    }
217                    else {
218                            return false;
219                    }
220            }
221    
222            private static Log _log = LogFactoryUtil.getLog(LDAPSettingsUtil.class);
223    
224    }