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.GetterUtil;
022    import com.liferay.portal.kernel.util.PrefsPropsUtil;
023    import com.liferay.portal.kernel.util.PropertiesUtil;
024    import com.liferay.portal.kernel.util.PropsKeys;
025    import com.liferay.portal.kernel.util.PropsUtil;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.service.UserLocalServiceUtil;
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 DefaultLDAPSettings implements LDAPSettings {
039    
040            @Override
041            public 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            @Override
074            public Properties getContactExpandoMappings(
075                            long ldapServerId, long companyId)
076                    throws Exception {
077    
078                    String postfix = getPropertyPostfix(ldapServerId);
079    
080                    Properties contactExpandoMappings = PropertiesUtil.load(
081                            PrefsPropsUtil.getString(
082                                    companyId, PropsKeys.LDAP_CONTACT_CUSTOM_MAPPINGS + postfix,
083                                    StringPool.BLANK));
084    
085                    LogUtil.debug(_log, contactExpandoMappings);
086    
087                    return contactExpandoMappings;
088            }
089    
090            @Override
091            public Properties getContactMappings(long ldapServerId, long companyId)
092                    throws Exception {
093    
094                    String postfix = getPropertyPostfix(ldapServerId);
095    
096                    Properties contactMappings = PropertiesUtil.load(
097                            PrefsPropsUtil.getString(
098                                    companyId, PropsKeys.LDAP_CONTACT_MAPPINGS + postfix,
099                                    StringPool.BLANK));
100    
101                    LogUtil.debug(_log, contactMappings);
102    
103                    return contactMappings;
104            }
105    
106            @Override
107            public Properties getGroupMappings(long ldapServerId, long companyId)
108                    throws Exception {
109    
110                    String postfix = getPropertyPostfix(ldapServerId);
111    
112                    Properties groupMappings = PropertiesUtil.load(
113                            PrefsPropsUtil.getString(
114                                    companyId, PropsKeys.LDAP_GROUP_MAPPINGS + postfix,
115                                    StringPool.BLANK));
116    
117                    LogUtil.debug(_log, groupMappings);
118    
119                    return groupMappings;
120            }
121    
122            @Override
123            public long getPreferredLDAPServerId(long companyId, String screenName) {
124                    User user = UserLocalServiceUtil.fetchUserByScreenName(
125                            companyId, screenName);
126    
127                    if (user == null) {
128                            return -1;
129                    }
130    
131                    return user.getLdapServerId();
132            }
133    
134            @Override
135            public String getPropertyPostfix(long ldapServerId) {
136                    return StringPool.PERIOD + ldapServerId;
137            }
138    
139            @Override
140            public Properties getUserExpandoMappings(long ldapServerId, long companyId)
141                    throws Exception {
142    
143                    String postfix = getPropertyPostfix(ldapServerId);
144    
145                    Properties userExpandoMappings = PropertiesUtil.load(
146                            PrefsPropsUtil.getString(
147                                    companyId, PropsKeys.LDAP_USER_CUSTOM_MAPPINGS + postfix,
148                                    StringPool.BLANK));
149    
150                    LogUtil.debug(_log, userExpandoMappings);
151    
152                    return userExpandoMappings;
153            }
154    
155            @Override
156            public Properties getUserMappings(long ldapServerId, long companyId)
157                    throws Exception {
158    
159                    String postfix = getPropertyPostfix(ldapServerId);
160    
161                    Properties userMappings = PropertiesUtil.load(
162                            PrefsPropsUtil.getString(
163                                    companyId, PropsKeys.LDAP_USER_MAPPINGS + postfix,
164                                    StringPool.BLANK));
165    
166                    LogUtil.debug(_log, userMappings);
167    
168                    return userMappings;
169            }
170    
171            @Override
172            public boolean isExportEnabled(long companyId) {
173                    boolean defaultImportUserPasswordAutogenerated =
174                            GetterUtil.get(
175                                    PropsUtil.get(
176                                            PropsKeys.LDAP_IMPORT_USER_PASSWORD_AUTOGENERATED),
177                                    false);
178    
179                    if (isImportEnabled(companyId) &&
180                            defaultImportUserPasswordAutogenerated) {
181    
182                            return false;
183                    }
184    
185                    boolean defaultLDAPExportEnabled = GetterUtil.get(
186                            PropsUtil.get(PropsKeys.LDAP_EXPORT_ENABLED), false);
187    
188                    if (PrefsPropsUtil.getBoolean(
189                                    companyId, PropsKeys.LDAP_EXPORT_ENABLED,
190                            defaultLDAPExportEnabled)) {
191    
192                            return true;
193                    }
194                    else {
195                            return false;
196                    }
197            }
198    
199            @Override
200            public boolean isExportGroupEnabled(long companyId) {
201                    boolean defaultLDAPExportGroupEnabled = GetterUtil.get(
202                            PropsUtil.get(PropsKeys.LDAP_EXPORT_GROUP_ENABLED), false);
203    
204                    if (PrefsPropsUtil.getBoolean(
205                                    companyId, PropsKeys.LDAP_EXPORT_GROUP_ENABLED,
206                            defaultLDAPExportGroupEnabled)) {
207    
208                            return true;
209                    }
210                    else {
211                            return false;
212                    }
213            }
214    
215            @Override
216            public boolean isImportEnabled(long companyId) {
217                    boolean defaultLDAPImportEnabled = GetterUtil.get(
218                            PropsUtil.get(PropsKeys.LDAP_IMPORT_ENABLED), false);
219    
220                    if (PrefsPropsUtil.getBoolean(
221                                    companyId, PropsKeys.LDAP_IMPORT_ENABLED,
222                            defaultLDAPImportEnabled)) {
223    
224                            return true;
225                    }
226                    else {
227                            return false;
228                    }
229            }
230    
231            @Override
232            public boolean isImportOnStartup(long companyId) {
233                    if (PrefsPropsUtil.getBoolean(
234                                    companyId, PropsKeys.LDAP_IMPORT_ON_STARTUP)) {
235    
236                            return true;
237                    }
238                    else {
239                            return false;
240                    }
241            }
242    
243            @Override
244            public boolean isPasswordPolicyEnabled(long companyId) {
245                    boolean defaultLDAPPasswordPolicyEnabled = GetterUtil.get(
246                            PropsUtil.get(PropsKeys.LDAP_PASSWORD_POLICY_ENABLED), false);
247    
248                    if (PrefsPropsUtil.getBoolean(
249                                    companyId, PropsKeys.LDAP_PASSWORD_POLICY_ENABLED,
250                            defaultLDAPPasswordPolicyEnabled)) {
251    
252                            return true;
253                    }
254                    else {
255                            return false;
256                    }
257            }
258    
259            private static final Log _log = LogFactoryUtil.getLog(
260                    DefaultLDAPSettings.class);
261    
262    }