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