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                                    StringPool.BLANK));
083    
084                    LogUtil.debug(_log, contactExpandoMappings);
085    
086                    return contactExpandoMappings;
087            }
088    
089            public static Properties getContactMappings(
090                            long ldapServerId, long companyId)
091                    throws Exception {
092    
093                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
094    
095                    Properties contactMappings = PropertiesUtil.load(
096                            PrefsPropsUtil.getString(
097                                    companyId, PropsKeys.LDAP_CONTACT_MAPPINGS + postfix,
098                                    StringPool.BLANK));
099    
100                    LogUtil.debug(_log, contactMappings);
101    
102                    return contactMappings;
103            }
104    
105            public static Properties getGroupMappings(long ldapServerId, long companyId)
106                    throws Exception {
107    
108                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
109    
110                    Properties groupMappings = PropertiesUtil.load(
111                            PrefsPropsUtil.getString(
112                                    companyId, PropsKeys.LDAP_GROUP_MAPPINGS + postfix,
113                                    StringPool.BLANK));
114    
115                    LogUtil.debug(_log, groupMappings);
116    
117                    return groupMappings;
118            }
119    
120            public static long getPreferredLDAPServerId(
121                            long companyId, String screenName)
122                    throws PortalException, SystemException {
123    
124                    User user = UserLocalServiceUtil.getUserByScreenName(
125                            companyId, screenName);
126    
127                    return user.getLdapServerId();
128            }
129    
130            public static String getPropertyPostfix(long ldapServerId) {
131                    return StringPool.PERIOD + ldapServerId;
132            }
133    
134            public static Properties getUserExpandoMappings(
135                            long ldapServerId, long companyId)
136                    throws Exception {
137    
138                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
139    
140                    Properties userExpandoMappings = PropertiesUtil.load(
141                            PrefsPropsUtil.getString(
142                                    companyId, PropsKeys.LDAP_USER_CUSTOM_MAPPINGS + postfix,
143                                    StringPool.BLANK));
144    
145                    LogUtil.debug(_log, userExpandoMappings);
146    
147                    return userExpandoMappings;
148            }
149    
150            public static Properties getUserMappings(long ldapServerId, long companyId)
151                    throws Exception {
152    
153                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
154    
155                    Properties userMappings = PropertiesUtil.load(
156                            PrefsPropsUtil.getString(
157                                    companyId, PropsKeys.LDAP_USER_MAPPINGS + postfix,
158                                    StringPool.BLANK));
159    
160                    LogUtil.debug(_log, userMappings);
161    
162                    return userMappings;
163            }
164    
165            public static boolean isExportEnabled(long companyId)
166                    throws SystemException {
167    
168                    if (isImportEnabled(companyId) &&
169                            PropsValues.LDAP_IMPORT_USER_PASSWORD_AUTOGENERATED) {
170    
171                            return false;
172                    }
173    
174                    if (PrefsPropsUtil.getBoolean(
175                                    companyId, PropsKeys.LDAP_EXPORT_ENABLED,
176                                    PropsValues.LDAP_EXPORT_ENABLED)) {
177    
178                            return true;
179                    }
180                    else {
181                            return false;
182                    }
183            }
184    
185            public static boolean isExportGroupEnabled(long companyId)
186                    throws SystemException {
187    
188                    if (PrefsPropsUtil.getBoolean(
189                                    companyId, PropsKeys.LDAP_EXPORT_GROUP_ENABLED,
190                                    PropsValues.LDAP_EXPORT_GROUP_ENABLED)) {
191    
192                            return true;
193                    }
194                    else {
195                            return false;
196                    }
197            }
198    
199            public static boolean isImportEnabled(long companyId)
200                    throws SystemException {
201    
202                    if (PrefsPropsUtil.getBoolean(
203                                    companyId, PropsKeys.LDAP_IMPORT_ENABLED,
204                                    PropsValues.LDAP_IMPORT_ENABLED)) {
205    
206                            return true;
207                    }
208                    else {
209                            return false;
210                    }
211            }
212    
213            public static boolean isImportOnStartup(long companyId)
214                    throws SystemException {
215    
216                    if (PrefsPropsUtil.getBoolean(
217                                    companyId, PropsKeys.LDAP_IMPORT_ON_STARTUP)) {
218    
219                            return true;
220                    }
221                    else {
222                            return false;
223                    }
224            }
225    
226            public static boolean isPasswordPolicyEnabled(long companyId)
227                    throws SystemException {
228    
229                    if (PrefsPropsUtil.getBoolean(
230                                    companyId, PropsKeys.LDAP_PASSWORD_POLICY_ENABLED,
231                                    PropsValues.LDAP_PASSWORD_POLICY_ENABLED)) {
232    
233                            return true;
234                    }
235                    else {
236                            return false;
237                    }
238            }
239    
240            private static Log _log = LogFactoryUtil.getLog(LDAPSettingsUtil.class);
241    
242    }