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.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.ldap.LDAPUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.log.LogUtil;
023    import com.liferay.portal.kernel.util.PropertiesUtil;
024    import com.liferay.portal.kernel.util.PropsKeys;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.model.User;
028    import com.liferay.portal.service.UserLocalServiceUtil;
029    import com.liferay.portal.util.PrefsPropsUtil;
030    import com.liferay.portal.util.PropsValues;
031    
032    import java.util.Properties;
033    
034    /**
035     * @author Edward Han
036     * @author Michael C. Han
037     * @author Brian Wing Shun Chan
038     */
039    public class LDAPSettingsUtil {
040    
041            public static String getAuthSearchFilter(
042                            long ldapServerId, long companyId, String emailAddress,
043                            String screenName, String userId)
044                    throws Exception {
045    
046                    String postfix = getPropertyPostfix(ldapServerId);
047    
048                    String filter = PrefsPropsUtil.getString(
049                            companyId, PropsKeys.LDAP_AUTH_SEARCH_FILTER + postfix);
050    
051                    if (_log.isDebugEnabled()) {
052                            _log.debug("Search filter before transformation " + filter);
053                    }
054    
055                    filter = StringUtil.replace(
056                            filter,
057                            new String[] {
058                                    "@company_id@", "@email_address@", "@screen_name@", "@user_id@"
059                            },
060                            new String[] {
061                                    String.valueOf(companyId), emailAddress, screenName, userId
062                            });
063    
064                    LDAPUtil.validateFilter(filter);
065    
066                    if (_log.isDebugEnabled()) {
067                            _log.debug("Search filter after transformation " + filter);
068                    }
069    
070                    return filter;
071            }
072    
073            public static Properties getContactExpandoMappings(
074                            long ldapServerId, long companyId)
075                    throws Exception {
076    
077                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
078    
079                    Properties contactExpandoMappings = PropertiesUtil.load(
080                            PrefsPropsUtil.getString(
081                                    companyId, PropsKeys.LDAP_CONTACT_CUSTOM_MAPPINGS + postfix));
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    
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    
112                    LogUtil.debug(_log, groupMappings);
113    
114                    return groupMappings;
115            }
116    
117            public static long getPreferredLDAPServerId(
118                            long companyId, String screenName)
119                    throws PortalException, SystemException {
120    
121                    User user = UserLocalServiceUtil.getUserByScreenName(
122                            companyId, screenName);
123    
124                    return user.getLdapServerId();
125            }
126    
127            public static String getPropertyPostfix(long ldapServerId) {
128                    return StringPool.PERIOD + ldapServerId;
129            }
130    
131            public static Properties getUserExpandoMappings(
132                            long ldapServerId, long companyId)
133                    throws Exception {
134    
135                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
136    
137                    Properties userExpandoMappings = PropertiesUtil.load(
138                            PrefsPropsUtil.getString(
139                                    companyId, PropsKeys.LDAP_USER_CUSTOM_MAPPINGS + postfix));
140    
141                    LogUtil.debug(_log, userExpandoMappings);
142    
143                    return userExpandoMappings;
144            }
145    
146            public static Properties getUserMappings(long ldapServerId, long companyId)
147                    throws Exception {
148    
149                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
150    
151                    Properties userMappings = PropertiesUtil.load(
152                            PrefsPropsUtil.getString(
153                                    companyId, PropsKeys.LDAP_USER_MAPPINGS + postfix));
154    
155                    LogUtil.debug(_log, userMappings);
156    
157                    return userMappings;
158            }
159    
160            public static boolean isExportEnabled(long companyId)
161                    throws SystemException {
162    
163                    if (isImportEnabled(companyId) &&
164                            PropsValues.LDAP_IMPORT_USER_PASSWORD_AUTOGENERATED) {
165    
166                            return false;
167                    }
168    
169                    if (PrefsPropsUtil.getBoolean(
170                                    companyId, PropsKeys.LDAP_EXPORT_ENABLED,
171                                    PropsValues.LDAP_EXPORT_ENABLED)) {
172    
173                            return true;
174                    }
175                    else {
176                            return false;
177                    }
178            }
179    
180            public static boolean isExportGroupEnabled(long companyId)
181                    throws SystemException {
182    
183                    if (PrefsPropsUtil.getBoolean(
184                                    companyId, PropsKeys.LDAP_EXPORT_GROUP_ENABLED,
185                                    PropsValues.LDAP_EXPORT_GROUP_ENABLED)) {
186    
187                            return true;
188                    }
189                    else {
190                            return false;
191                    }
192            }
193    
194            public static boolean isImportEnabled(long companyId)
195                    throws SystemException {
196    
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                    throws SystemException {
210    
211                    if (PrefsPropsUtil.getBoolean(
212                                    companyId, PropsKeys.LDAP_IMPORT_ON_STARTUP)) {
213    
214                            return true;
215                    }
216                    else {
217                            return false;
218                    }
219            }
220    
221            public static boolean isPasswordPolicyEnabled(long companyId)
222                    throws SystemException {
223    
224                    if (PrefsPropsUtil.getBoolean(
225                                    companyId, PropsKeys.LDAP_PASSWORD_POLICY_ENABLED,
226                                    PropsValues.LDAP_PASSWORD_POLICY_ENABLED)) {
227    
228                            return true;
229                    }
230                    else {
231                            return false;
232                    }
233            }
234    
235            private static Log _log = LogFactoryUtil.getLog(LDAPSettingsUtil.class);
236    
237    }