001    /**
002     * Copyright (c) 2000-2010 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.enterpriseadmin.action;
016    
017    import com.liferay.counter.service.CounterLocalServiceUtil;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.PropertiesParamUtil;
023    import com.liferay.portal.kernel.util.PropsKeys;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.UnicodeProperties;
027    import com.liferay.portal.security.auth.PrincipalException;
028    import com.liferay.portal.security.ldap.LDAPSettingsUtil;
029    import com.liferay.portal.service.CompanyServiceUtil;
030    import com.liferay.portal.struts.PortletAction;
031    import com.liferay.portal.theme.ThemeDisplay;
032    import com.liferay.portal.util.Portal;
033    import com.liferay.portal.util.PrefsPropsUtil;
034    import com.liferay.portal.util.WebKeys;
035    
036    import javax.portlet.ActionRequest;
037    import javax.portlet.ActionResponse;
038    import javax.portlet.PortletConfig;
039    import javax.portlet.PortletPreferences;
040    import javax.portlet.RenderRequest;
041    import javax.portlet.RenderResponse;
042    
043    import org.apache.struts.action.ActionForm;
044    import org.apache.struts.action.ActionForward;
045    import org.apache.struts.action.ActionMapping;
046    
047    /**
048     * @author Ryan Park
049     */
050    public class EditLDAPServerAction extends PortletAction {
051    
052            public void processAction(
053                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
054                            ActionRequest actionRequest, ActionResponse actionResponse)
055                    throws Exception {
056    
057                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
058    
059                    try {
060                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
061                                    updateLDAPServer(actionRequest);
062                            }
063                            else if (cmd.equals(Constants.DELETE)) {
064                                    deleteLDAPServer(actionRequest);
065                            }
066    
067                            sendRedirect(actionRequest, actionResponse);
068                    }
069                    catch (Exception e) {
070                            if (e instanceof PrincipalException) {
071                                    SessionErrors.add(actionRequest, e.getClass().getName());
072    
073                                    setForward(actionRequest, "portlet.enterprise_admin.error");
074                            }
075                            else {
076                                    throw e;
077                            }
078                    }
079            }
080    
081            public ActionForward render(
082                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
083                            RenderRequest renderRequest, RenderResponse renderResponse)
084                    throws Exception {
085    
086                    return mapping.findForward(getForward(
087                            renderRequest, "portlet.enterprise_admin.edit_ldap_server"));
088            }
089    
090            protected UnicodeProperties addLDAPServer(
091                            long companyId, UnicodeProperties properties)
092                    throws Exception {
093    
094                    long ldapServerId = CounterLocalServiceUtil.increment();
095    
096                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
097    
098                    String[] keys = properties.keySet().toArray(new String[0]);
099    
100                    for (String key : keys) {
101                            if (ArrayUtil.contains(_KEYS, key)) {
102                                    String value = properties.remove(key);
103    
104                                    if (key.equals(PropsKeys.LDAP_SECURITY_CREDENTIALS) &&
105                                            value.equals(Portal.TEMP_OBFUSCATION_VALUE)) {
106    
107                                            value = PrefsPropsUtil.getString(
108                                                    PropsKeys.LDAP_SECURITY_CREDENTIALS);
109                                    }
110    
111                                    properties.setProperty(key + postfix, value);
112                            }
113                    }
114    
115                    PortletPreferences preferences = PrefsPropsUtil.getPreferences(
116                            companyId);
117    
118                    String ldapServerIds = preferences.getValue(
119                            "ldap.server.ids", StringPool.BLANK);
120    
121                    ldapServerIds = StringUtil.add(
122                            ldapServerIds, String.valueOf(ldapServerId));
123    
124                    properties.setProperty("ldap.server.ids", ldapServerIds);
125    
126                    return properties;
127            }
128    
129            protected void deleteLDAPServer(ActionRequest actionRequest)
130                    throws Exception {
131    
132                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
133                            WebKeys.THEME_DISPLAY);
134    
135                    long ldapServerId = ParamUtil.getLong(actionRequest, "ldapServerId");
136    
137                    // Remove preferences
138    
139                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
140    
141                    String[] keys = new String[_KEYS.length];
142    
143                    for (int i = 0; i < _KEYS.length; i++) {
144                            keys[i] = _KEYS[i] + postfix;
145                    }
146    
147                    CompanyServiceUtil.removePreferences(
148                            themeDisplay.getCompanyId(), keys);
149    
150                    // Update preferences
151    
152                    PortletPreferences preferences = PrefsPropsUtil.getPreferences(
153                            themeDisplay.getCompanyId());
154    
155                    UnicodeProperties properties = new UnicodeProperties();
156    
157                    String ldapServerIds = preferences.getValue(
158                            "ldap.server.ids", StringPool.BLANK);
159    
160                    ldapServerIds = StringUtil.remove(
161                            ldapServerIds, String.valueOf(ldapServerId));
162    
163                    properties.put("ldap.server.ids", ldapServerIds);
164    
165                    CompanyServiceUtil.updatePreferences(
166                            themeDisplay.getCompanyId(), properties);
167            }
168    
169            protected void updateLDAPServer(ActionRequest actionRequest)
170                    throws Exception {
171    
172                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
173                            WebKeys.THEME_DISPLAY);
174    
175                    long ldapServerId = ParamUtil.getLong(actionRequest, "ldapServerId");
176    
177                    UnicodeProperties properties = PropertiesParamUtil.getProperties(
178                            actionRequest, "settings--");
179    
180                    if (ldapServerId <= 0) {
181                            properties = addLDAPServer(
182                                    themeDisplay.getCompanyId(), properties);
183                    }
184    
185                    CompanyServiceUtil.updatePreferences(
186                            themeDisplay.getCompanyId(), properties);
187            }
188    
189            private final String[] _KEYS = {
190                    PropsKeys.LDAP_AUTH_SEARCH_FILTER,
191                    PropsKeys.LDAP_BASE_DN,
192                    PropsKeys.LDAP_BASE_PROVIDER_URL,
193                    PropsKeys.LDAP_CONTACT_CUSTOM_MAPPINGS,
194                    PropsKeys.LDAP_CONTACT_MAPPINGS,
195                    PropsKeys.LDAP_GROUP_DEFAULT_OBJECT_CLASSES,
196                    PropsKeys.LDAP_GROUP_MAPPINGS,
197                    PropsKeys.LDAP_GROUPS_DN,
198                    PropsKeys.LDAP_IMPORT_GROUP_SEARCH_FILTER,
199                    PropsKeys.LDAP_IMPORT_USER_SEARCH_FILTER,
200                    PropsKeys.LDAP_SECURITY_CREDENTIALS,
201                    PropsKeys.LDAP_SECURITY_PRINCIPAL,
202                    PropsKeys.LDAP_SERVER_NAME,
203                    PropsKeys.LDAP_USER_CUSTOM_MAPPINGS,
204                    PropsKeys.LDAP_USER_DEFAULT_OBJECT_CLASSES,
205                    PropsKeys.LDAP_USER_MAPPINGS,
206                    PropsKeys.LDAP_USERS_DN
207            };
208    
209    }