001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.portalsettings.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            @Override
053            public void processAction(
054                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
055                            ActionRequest actionRequest, ActionResponse actionResponse)
056                    throws Exception {
057    
058                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
059    
060                    try {
061                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
062                                    updateLDAPServer(actionRequest);
063                            }
064                            else if (cmd.equals(Constants.DELETE)) {
065                                    deleteLDAPServer(actionRequest);
066                            }
067    
068                            sendRedirect(actionRequest, actionResponse);
069                    }
070                    catch (Exception e) {
071                            if (e instanceof PrincipalException) {
072                                    SessionErrors.add(actionRequest, e.getClass().getName());
073    
074                                    setForward(actionRequest, "portlet.portal_settings.error");
075                            }
076                            else {
077                                    throw e;
078                            }
079                    }
080            }
081    
082            @Override
083            public ActionForward render(
084                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
085                            RenderRequest renderRequest, RenderResponse renderResponse)
086                    throws Exception {
087    
088                    return mapping.findForward(getForward(
089                            renderRequest, "portlet.portal_settings.edit_ldap_server"));
090            }
091    
092            protected UnicodeProperties addLDAPServer(
093                            long companyId, UnicodeProperties properties)
094                    throws Exception {
095    
096                    String defaultPostfix = LDAPSettingsUtil.getPropertyPostfix(0);
097    
098                    String[] defaultKeys = new String[_KEYS.length];
099    
100                    for (int i = 0; i < _KEYS.length; i++) {
101                            defaultKeys[i] = _KEYS[i] + defaultPostfix;
102                    }
103    
104                    long ldapServerId = CounterLocalServiceUtil.increment();
105    
106                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
107    
108                    String[] keys = properties.keySet().toArray(new String[0]);
109    
110                    for (String key : keys) {
111                            if (ArrayUtil.contains(defaultKeys, key)) {
112                                    String value = properties.remove(key);
113    
114                                    if (key.equals(
115                                                    PropsKeys.LDAP_SECURITY_CREDENTIALS + defaultPostfix) &&
116                                            value.equals(Portal.TEMP_OBFUSCATION_VALUE)) {
117    
118                                            value = PrefsPropsUtil.getString(
119                                                    PropsKeys.LDAP_SECURITY_CREDENTIALS);
120                                    }
121    
122                                    properties.setProperty(
123                                            key.replace(defaultPostfix, postfix), value);
124                            }
125                    }
126    
127                    PortletPreferences preferences = PrefsPropsUtil.getPreferences(
128                            companyId);
129    
130                    String ldapServerIds = preferences.getValue(
131                            "ldap.server.ids", StringPool.BLANK);
132    
133                    ldapServerIds = StringUtil.add(
134                            ldapServerIds, String.valueOf(ldapServerId));
135    
136                    properties.setProperty("ldap.server.ids", ldapServerIds);
137    
138                    return properties;
139            }
140    
141            protected void deleteLDAPServer(ActionRequest actionRequest)
142                    throws Exception {
143    
144                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
145                            WebKeys.THEME_DISPLAY);
146    
147                    long ldapServerId = ParamUtil.getLong(actionRequest, "ldapServerId");
148    
149                    // Remove preferences
150    
151                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
152    
153                    String[] keys = new String[_KEYS.length];
154    
155                    for (int i = 0; i < _KEYS.length; i++) {
156                            keys[i] = _KEYS[i] + postfix;
157                    }
158    
159                    CompanyServiceUtil.removePreferences(
160                            themeDisplay.getCompanyId(), keys);
161    
162                    // Update preferences
163    
164                    PortletPreferences preferences = PrefsPropsUtil.getPreferences(
165                            themeDisplay.getCompanyId());
166    
167                    UnicodeProperties properties = new UnicodeProperties();
168    
169                    String ldapServerIds = preferences.getValue(
170                            "ldap.server.ids", StringPool.BLANK);
171    
172                    ldapServerIds = StringUtil.remove(
173                            ldapServerIds, String.valueOf(ldapServerId));
174    
175                    properties.put("ldap.server.ids", ldapServerIds);
176    
177                    CompanyServiceUtil.updatePreferences(
178                            themeDisplay.getCompanyId(), properties);
179            }
180    
181            protected void updateLDAPServer(ActionRequest actionRequest)
182                    throws Exception {
183    
184                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
185                            WebKeys.THEME_DISPLAY);
186    
187                    long ldapServerId = ParamUtil.getLong(actionRequest, "ldapServerId");
188    
189                    UnicodeProperties properties = PropertiesParamUtil.getProperties(
190                            actionRequest, "settings--");
191    
192                    if (ldapServerId <= 0) {
193                            properties = addLDAPServer(
194                                    themeDisplay.getCompanyId(), properties);
195                    }
196    
197                    CompanyServiceUtil.updatePreferences(
198                            themeDisplay.getCompanyId(), properties);
199            }
200    
201            private final String[] _KEYS = {
202                    PropsKeys.LDAP_AUTH_SEARCH_FILTER,
203                    PropsKeys.LDAP_BASE_DN,
204                    PropsKeys.LDAP_BASE_PROVIDER_URL,
205                    PropsKeys.LDAP_CONTACT_CUSTOM_MAPPINGS,
206                    PropsKeys.LDAP_CONTACT_MAPPINGS,
207                    PropsKeys.LDAP_GROUP_DEFAULT_OBJECT_CLASSES,
208                    PropsKeys.LDAP_GROUP_MAPPINGS,
209                    PropsKeys.LDAP_GROUPS_DN,
210                    PropsKeys.LDAP_IMPORT_GROUP_SEARCH_FILTER,
211                    PropsKeys.LDAP_IMPORT_USER_SEARCH_FILTER,
212                    PropsKeys.LDAP_SECURITY_CREDENTIALS,
213                    PropsKeys.LDAP_SECURITY_PRINCIPAL,
214                    PropsKeys.LDAP_SERVER_NAME,
215                    PropsKeys.LDAP_USER_CUSTOM_MAPPINGS,
216                    PropsKeys.LDAP_USER_DEFAULT_OBJECT_CLASSES,
217                    PropsKeys.LDAP_USER_MAPPINGS,
218                    PropsKeys.LDAP_USERS_DN
219            };
220    
221    }