001    /**
002     * Copyright (c) 2000-present 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.ldap.LDAPUtil;
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.model.User;
026    import com.liferay.portal.service.UserLocalServiceUtil;
027    import com.liferay.portal.util.PrefsPropsUtil;
028    import com.liferay.portal.util.PropsValues;
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 Exception {
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, userId
060                            });
061    
062                    LDAPUtil.validateFilter(filter);
063    
064                    if (_log.isDebugEnabled()) {
065                            _log.debug("Search filter after transformation " + filter);
066                    }
067    
068                    return filter;
069            }
070    
071            public static Properties getContactExpandoMappings(
072                            long ldapServerId, long companyId)
073                    throws Exception {
074    
075                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
076    
077                    Properties contactExpandoMappings = PropertiesUtil.load(
078                            PrefsPropsUtil.getString(
079                                    companyId, PropsKeys.LDAP_CONTACT_CUSTOM_MAPPINGS + postfix,
080                                    StringPool.BLANK));
081    
082                    LogUtil.debug(_log, contactExpandoMappings);
083    
084                    return contactExpandoMappings;
085            }
086    
087            public static Properties getContactMappings(
088                            long ldapServerId, long companyId)
089                    throws Exception {
090    
091                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
092    
093                    Properties contactMappings = PropertiesUtil.load(
094                            PrefsPropsUtil.getString(
095                                    companyId, PropsKeys.LDAP_CONTACT_MAPPINGS + postfix,
096                                    StringPool.BLANK));
097    
098                    LogUtil.debug(_log, contactMappings);
099    
100                    return contactMappings;
101            }
102    
103            public static Properties getGroupMappings(long ldapServerId, long companyId)
104                    throws Exception {
105    
106                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
107    
108                    Properties groupMappings = PropertiesUtil.load(
109                            PrefsPropsUtil.getString(
110                                    companyId, PropsKeys.LDAP_GROUP_MAPPINGS + postfix,
111                                    StringPool.BLANK));
112    
113                    LogUtil.debug(_log, groupMappings);
114    
115                    return groupMappings;
116            }
117    
118            public static long getPreferredLDAPServerId(
119                    long companyId, String screenName) {
120    
121                    User user = UserLocalServiceUtil.fetchUserByScreenName(
122                            companyId, screenName);
123    
124                    if (user == null) {
125                            return -1;
126                    }
127    
128                    return user.getLdapServerId();
129            }
130    
131            public static String getPropertyPostfix(long ldapServerId) {
132                    return StringPool.PERIOD + ldapServerId;
133            }
134    
135            public static Properties getUserExpandoMappings(
136                            long ldapServerId, long companyId)
137                    throws Exception {
138    
139                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
140    
141                    Properties userExpandoMappings = PropertiesUtil.load(
142                            PrefsPropsUtil.getString(
143                                    companyId, PropsKeys.LDAP_USER_CUSTOM_MAPPINGS + postfix,
144                                    StringPool.BLANK));
145    
146                    LogUtil.debug(_log, userExpandoMappings);
147    
148                    return userExpandoMappings;
149            }
150    
151            public static Properties getUserMappings(long ldapServerId, long companyId)
152                    throws Exception {
153    
154                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
155    
156                    Properties userMappings = PropertiesUtil.load(
157                            PrefsPropsUtil.getString(
158                                    companyId, PropsKeys.LDAP_USER_MAPPINGS + postfix,
159                                    StringPool.BLANK));
160    
161                    LogUtil.debug(_log, userMappings);
162    
163                    return userMappings;
164            }
165    
166            public static boolean isExportEnabled(long companyId) {
167                    if (isImportEnabled(companyId) &&
168                            PropsValues.LDAP_IMPORT_USER_PASSWORD_AUTOGENERATED) {
169    
170                            return false;
171                    }
172    
173                    if (PrefsPropsUtil.getBoolean(
174                                    companyId, PropsKeys.LDAP_EXPORT_ENABLED,
175                                    PropsValues.LDAP_EXPORT_ENABLED)) {
176    
177                            return true;
178                    }
179                    else {
180                            return false;
181                    }
182            }
183    
184            public static boolean isExportGroupEnabled(long companyId) {
185                    if (PrefsPropsUtil.getBoolean(
186                                    companyId, PropsKeys.LDAP_EXPORT_GROUP_ENABLED,
187                                    PropsValues.LDAP_EXPORT_GROUP_ENABLED)) {
188    
189                            return true;
190                    }
191                    else {
192                            return false;
193                    }
194            }
195    
196            public static boolean isImportEnabled(long companyId) {
197                    if (PrefsPropsUtil.getBoolean(
198                                    companyId, PropsKeys.LDAP_IMPORT_ENABLED,
199                                    PropsValues.LDAP_IMPORT_ENABLED)) {
200    
201                            return true;
202                    }
203                    else {
204                            return false;
205                    }
206            }
207    
208            public static boolean isImportOnStartup(long companyId) {
209                    if (PrefsPropsUtil.getBoolean(
210                                    companyId, PropsKeys.LDAP_IMPORT_ON_STARTUP)) {
211    
212                            return true;
213                    }
214                    else {
215                            return false;
216                    }
217            }
218    
219            public static boolean isPasswordPolicyEnabled(long companyId) {
220                    if (PrefsPropsUtil.getBoolean(
221                                    companyId, PropsKeys.LDAP_PASSWORD_POLICY_ENABLED,
222                                    PropsValues.LDAP_PASSWORD_POLICY_ENABLED)) {
223    
224                            return true;
225                    }
226                    else {
227                            return false;
228                    }
229            }
230    
231            private static final Log _log = LogFactoryUtil.getLog(
232                    LDAPSettingsUtil.class);
233    
234    }