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