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