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.model.User;
027    import com.liferay.portal.service.UserLocalServiceUtil;
028    import com.liferay.portal.util.PrefsPropsUtil;
029    import com.liferay.portal.util.PropsValues;
030    
031    import java.util.Properties;
032    
033    /**
034     * @author Edward Han
035     * @author Michael C. Han
036     * @author Brian Wing Shun Chan
037     */
038    public class LDAPSettingsUtil {
039    
040            public static String getAuthSearchFilter(
041                            long ldapServerId, long companyId, String emailAddress,
042                            String screenName, String userId)
043                    throws Exception {
044    
045                    String postfix = getPropertyPostfix(ldapServerId);
046    
047                    String filter = PrefsPropsUtil.getString(
048                            companyId, PropsKeys.LDAP_AUTH_SEARCH_FILTER + postfix);
049    
050                    if (_log.isDebugEnabled()) {
051                            _log.debug("Search filter before transformation " + filter);
052                    }
053    
054                    filter = StringUtil.replace(
055                            filter,
056                            new String[] {
057                                    "@company_id@", "@email_address@", "@screen_name@", "@user_id@"
058                            },
059                            new String[] {
060                                    String.valueOf(companyId), emailAddress, screenName, userId
061                            });
062    
063                    LDAPUtil.validateFilter(filter);
064    
065                    if (_log.isDebugEnabled()) {
066                            _log.debug("Search filter after transformation " + filter);
067                    }
068    
069                    return filter;
070            }
071    
072            public static Properties getContactExpandoMappings(
073                            long ldapServerId, long companyId)
074                    throws Exception {
075    
076                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
077    
078                    Properties contactExpandoMappings = PropertiesUtil.load(
079                            PrefsPropsUtil.getString(
080                                    companyId, PropsKeys.LDAP_CONTACT_CUSTOM_MAPPINGS + postfix,
081                                    StringPool.BLANK));
082    
083                    LogUtil.debug(_log, contactExpandoMappings);
084    
085                    return contactExpandoMappings;
086            }
087    
088            public static Properties getContactMappings(
089                            long ldapServerId, long companyId)
090                    throws Exception {
091    
092                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
093    
094                    Properties contactMappings = PropertiesUtil.load(
095                            PrefsPropsUtil.getString(
096                                    companyId, PropsKeys.LDAP_CONTACT_MAPPINGS + postfix,
097                                    StringPool.BLANK));
098    
099                    LogUtil.debug(_log, contactMappings);
100    
101                    return contactMappings;
102            }
103    
104            public static Properties getGroupMappings(long ldapServerId, long companyId)
105                    throws Exception {
106    
107                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
108    
109                    Properties groupMappings = PropertiesUtil.load(
110                            PrefsPropsUtil.getString(
111                                    companyId, PropsKeys.LDAP_GROUP_MAPPINGS + postfix,
112                                    StringPool.BLANK));
113    
114                    LogUtil.debug(_log, groupMappings);
115    
116                    return groupMappings;
117            }
118    
119            public static long getPreferredLDAPServerId(
120                            long companyId, String screenName)
121                    throws SystemException {
122    
123                    User user = UserLocalServiceUtil.fetchUserByScreenName(
124                            companyId, screenName);
125    
126                    if (user == null) {
127                            return -1;
128                    }
129    
130                    return user.getLdapServerId();
131            }
132    
133            public static String getPropertyPostfix(long ldapServerId) {
134                    return StringPool.PERIOD + ldapServerId;
135            }
136    
137            public static Properties getUserExpandoMappings(
138                            long ldapServerId, long companyId)
139                    throws Exception {
140    
141                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
142    
143                    Properties userExpandoMappings = PropertiesUtil.load(
144                            PrefsPropsUtil.getString(
145                                    companyId, PropsKeys.LDAP_USER_CUSTOM_MAPPINGS + postfix,
146                                    StringPool.BLANK));
147    
148                    LogUtil.debug(_log, userExpandoMappings);
149    
150                    return userExpandoMappings;
151            }
152    
153            public static Properties getUserMappings(long ldapServerId, long companyId)
154                    throws Exception {
155    
156                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
157    
158                    Properties userMappings = PropertiesUtil.load(
159                            PrefsPropsUtil.getString(
160                                    companyId, PropsKeys.LDAP_USER_MAPPINGS + postfix,
161                                    StringPool.BLANK));
162    
163                    LogUtil.debug(_log, userMappings);
164    
165                    return userMappings;
166            }
167    
168            public static boolean isExportEnabled(long companyId)
169                    throws SystemException {
170    
171                    if (isImportEnabled(companyId) &&
172                            PropsValues.LDAP_IMPORT_USER_PASSWORD_AUTOGENERATED) {
173    
174                            return false;
175                    }
176    
177                    if (PrefsPropsUtil.getBoolean(
178                                    companyId, PropsKeys.LDAP_EXPORT_ENABLED,
179                                    PropsValues.LDAP_EXPORT_ENABLED)) {
180    
181                            return true;
182                    }
183                    else {
184                            return false;
185                    }
186            }
187    
188            public static boolean isExportGroupEnabled(long companyId)
189                    throws SystemException {
190    
191                    if (PrefsPropsUtil.getBoolean(
192                                    companyId, PropsKeys.LDAP_EXPORT_GROUP_ENABLED,
193                                    PropsValues.LDAP_EXPORT_GROUP_ENABLED)) {
194    
195                            return true;
196                    }
197                    else {
198                            return false;
199                    }
200            }
201    
202            public static boolean isImportEnabled(long companyId)
203                    throws SystemException {
204    
205                    if (PrefsPropsUtil.getBoolean(
206                                    companyId, PropsKeys.LDAP_IMPORT_ENABLED,
207                                    PropsValues.LDAP_IMPORT_ENABLED)) {
208    
209                            return true;
210                    }
211                    else {
212                            return false;
213                    }
214            }
215    
216            public static boolean isImportOnStartup(long companyId)
217                    throws SystemException {
218    
219                    if (PrefsPropsUtil.getBoolean(
220                                    companyId, PropsKeys.LDAP_IMPORT_ON_STARTUP)) {
221    
222                            return true;
223                    }
224                    else {
225                            return false;
226                    }
227            }
228    
229            public static boolean isPasswordPolicyEnabled(long companyId)
230                    throws SystemException {
231    
232                    if (PrefsPropsUtil.getBoolean(
233                                    companyId, PropsKeys.LDAP_PASSWORD_POLICY_ENABLED,
234                                    PropsValues.LDAP_PASSWORD_POLICY_ENABLED)) {
235    
236                            return true;
237                    }
238                    else {
239                            return false;
240                    }
241            }
242    
243            private static Log _log = LogFactoryUtil.getLog(LDAPSettingsUtil.class);
244    
245    }