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