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.portlet.portalsettings.action;
016    
017    import com.liferay.counter.service.CounterLocalServiceUtil;
018    import com.liferay.portal.kernel.ldap.DuplicateLDAPServerNameException;
019    import com.liferay.portal.kernel.ldap.LDAPFilterException;
020    import com.liferay.portal.kernel.ldap.LDAPServerNameException;
021    import com.liferay.portal.kernel.ldap.LDAPUtil;
022    import com.liferay.portal.kernel.servlet.SessionErrors;
023    import com.liferay.portal.kernel.util.Constants;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.PropertiesParamUtil;
026    import com.liferay.portal.kernel.util.PropsKeys;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.StringUtil;
029    import com.liferay.portal.kernel.util.UnicodeProperties;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.security.auth.PrincipalException;
032    import com.liferay.portal.security.ldap.LDAPSettingsUtil;
033    import com.liferay.portal.service.CompanyServiceUtil;
034    import com.liferay.portal.struts.PortletAction;
035    import com.liferay.portal.theme.ThemeDisplay;
036    import com.liferay.portal.util.Portal;
037    import com.liferay.portal.util.PrefsPropsUtil;
038    import com.liferay.portal.util.WebKeys;
039    
040    import java.util.HashSet;
041    import java.util.Set;
042    
043    import javax.portlet.ActionRequest;
044    import javax.portlet.ActionResponse;
045    import javax.portlet.PortletConfig;
046    import javax.portlet.PortletPreferences;
047    import javax.portlet.RenderRequest;
048    import javax.portlet.RenderResponse;
049    
050    import org.apache.struts.action.ActionForm;
051    import org.apache.struts.action.ActionForward;
052    import org.apache.struts.action.ActionMapping;
053    
054    /**
055     * @author Ryan Park
056     */
057    public class EditLDAPServerAction extends PortletAction {
058    
059            @Override
060            public void processAction(
061                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
062                            ActionRequest actionRequest, ActionResponse actionResponse)
063                    throws Exception {
064    
065                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
066    
067                    try {
068                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
069                                    updateLDAPServer(actionRequest);
070                            }
071                            else if (cmd.equals(Constants.DELETE)) {
072                                    deleteLDAPServer(actionRequest);
073                            }
074    
075                            sendRedirect(actionRequest, actionResponse);
076                    }
077                    catch (Exception e) {
078                            if (e instanceof DuplicateLDAPServerNameException ||
079                                    e instanceof LDAPFilterException ||
080                                    e instanceof LDAPServerNameException) {
081    
082                                    SessionErrors.add(actionRequest, e.getClass());
083                            }
084                            else if (e instanceof PrincipalException) {
085                                    SessionErrors.add(actionRequest, e.getClass());
086    
087                                    setForward(actionRequest, "portlet.portal_settings.error");
088                            }
089                            else {
090                                    throw e;
091                            }
092                    }
093            }
094    
095            @Override
096            public ActionForward render(
097                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
098                            RenderRequest renderRequest, RenderResponse renderResponse)
099                    throws Exception {
100    
101                    return mapping.findForward(
102                            getForward(
103                                    renderRequest, "portlet.portal_settings.edit_ldap_server"));
104            }
105    
106            protected UnicodeProperties addLDAPServer(
107                            long companyId, UnicodeProperties properties)
108                    throws Exception {
109    
110                    String defaultPostfix = LDAPSettingsUtil.getPropertyPostfix(0);
111    
112                    Set<String> defaultKeys = new HashSet<String>(_KEYS.length);
113    
114                    for (String key : _KEYS) {
115                            defaultKeys.add(key + defaultPostfix);
116                    }
117    
118                    long ldapServerId = CounterLocalServiceUtil.increment();
119    
120                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
121    
122                    Set<String> keysSet = properties.keySet();
123    
124                    String[] keys = keysSet.toArray(new String[keysSet.size()]);
125    
126                    for (String key : keys) {
127                            if (defaultKeys.contains(key)) {
128                                    String value = properties.remove(key);
129    
130                                    if (key.equals(
131                                                    PropsKeys.LDAP_SECURITY_CREDENTIALS + defaultPostfix) &&
132                                            value.equals(Portal.TEMP_OBFUSCATION_VALUE)) {
133    
134                                            value = PrefsPropsUtil.getString(
135                                                    PropsKeys.LDAP_SECURITY_CREDENTIALS);
136                                    }
137    
138                                    properties.setProperty(
139                                            key.replace(defaultPostfix, postfix), value);
140                            }
141                    }
142    
143                    PortletPreferences preferences = PrefsPropsUtil.getPreferences(
144                            companyId);
145    
146                    String ldapServerIds = preferences.getValue(
147                            "ldap.server.ids", StringPool.BLANK);
148    
149                    ldapServerIds = StringUtil.add(
150                            ldapServerIds, String.valueOf(ldapServerId));
151    
152                    properties.setProperty("ldap.server.ids", ldapServerIds);
153    
154                    return properties;
155            }
156    
157            protected void deleteLDAPServer(ActionRequest actionRequest)
158                    throws Exception {
159    
160                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
161                            WebKeys.THEME_DISPLAY);
162    
163                    long ldapServerId = ParamUtil.getLong(actionRequest, "ldapServerId");
164    
165                    // Remove preferences
166    
167                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
168    
169                    String[] keys = new String[_KEYS.length];
170    
171                    for (int i = 0; i < _KEYS.length; i++) {
172                            keys[i] = _KEYS[i] + postfix;
173                    }
174    
175                    CompanyServiceUtil.removePreferences(themeDisplay.getCompanyId(), keys);
176    
177                    // Update preferences
178    
179                    PortletPreferences preferences = PrefsPropsUtil.getPreferences(
180                            themeDisplay.getCompanyId());
181    
182                    UnicodeProperties properties = new UnicodeProperties();
183    
184                    String ldapServerIds = preferences.getValue(
185                            "ldap.server.ids", StringPool.BLANK);
186    
187                    ldapServerIds = StringUtil.remove(
188                            ldapServerIds, String.valueOf(ldapServerId));
189    
190                    properties.put("ldap.server.ids", ldapServerIds);
191    
192                    CompanyServiceUtil.updatePreferences(
193                            themeDisplay.getCompanyId(), properties);
194            }
195    
196            protected void updateLDAPServer(ActionRequest actionRequest)
197                    throws Exception {
198    
199                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
200                            WebKeys.THEME_DISPLAY);
201    
202                    long ldapServerId = ParamUtil.getLong(actionRequest, "ldapServerId");
203    
204                    UnicodeProperties properties = PropertiesParamUtil.getProperties(
205                            actionRequest, "settings--");
206    
207                    validateLDAPServerName(
208                            ldapServerId, themeDisplay.getCompanyId(), properties);
209    
210                    String filter = ParamUtil.getString(
211                            actionRequest, "importUserSearchFilter");
212    
213                    LDAPUtil.validateFilter(filter);
214    
215                    if (ldapServerId <= 0) {
216                            properties = addLDAPServer(themeDisplay.getCompanyId(), properties);
217                    }
218    
219                    CompanyServiceUtil.updatePreferences(
220                            themeDisplay.getCompanyId(), properties);
221            }
222    
223            protected void validateLDAPServerName(
224                            long ldapServerId, long companyId, UnicodeProperties properties)
225                    throws Exception {
226    
227                    String ldapServerName = properties.getProperty(
228                            "ldap.server.name." + ldapServerId);
229    
230                    if (Validator.isNull(ldapServerName)) {
231                            throw new LDAPServerNameException();
232                    }
233    
234                    long[] existingLDAPServerIds = StringUtil.split(
235                            PrefsPropsUtil.getString(companyId, "ldap.server.ids"), 0L);
236    
237                    for (long existingLDAPServerId : existingLDAPServerIds) {
238                            if (ldapServerId == existingLDAPServerId) {
239                                    continue;
240                            }
241    
242                            String existingLDAPServerName = PrefsPropsUtil.getString(
243                                    companyId, "ldap.server.name." + existingLDAPServerId);
244    
245                            if (ldapServerName.equals(existingLDAPServerName)) {
246                                    throw new DuplicateLDAPServerNameException();
247                            }
248                    }
249            }
250    
251            private static final String[] _KEYS = {
252                    PropsKeys.LDAP_AUTH_SEARCH_FILTER, PropsKeys.LDAP_BASE_DN,
253                    PropsKeys.LDAP_BASE_PROVIDER_URL,
254                    PropsKeys.LDAP_CONTACT_CUSTOM_MAPPINGS, PropsKeys.LDAP_CONTACT_MAPPINGS,
255                    PropsKeys.LDAP_GROUP_DEFAULT_OBJECT_CLASSES,
256                    PropsKeys.LDAP_GROUP_MAPPINGS, PropsKeys.LDAP_GROUPS_DN,
257                    PropsKeys.LDAP_IMPORT_GROUP_SEARCH_FILTER,
258                    PropsKeys.LDAP_IMPORT_USER_SEARCH_FILTER,
259                    PropsKeys.LDAP_SECURITY_CREDENTIALS, PropsKeys.LDAP_SECURITY_PRINCIPAL,
260                    PropsKeys.LDAP_SERVER_NAME, PropsKeys.LDAP_USER_CUSTOM_MAPPINGS,
261                    PropsKeys.LDAP_USER_DEFAULT_OBJECT_CLASSES,
262                    PropsKeys.LDAP_USER_MAPPINGS, PropsKeys.LDAP_USERS_DN
263            };
264    
265    }