1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.enterpriseadmin.action;
16  
17  import com.liferay.counter.service.CounterLocalServiceUtil;
18  import com.liferay.portal.kernel.servlet.SessionErrors;
19  import com.liferay.portal.kernel.util.ArrayUtil;
20  import com.liferay.portal.kernel.util.Constants;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.kernel.util.PropertiesParamUtil;
23  import com.liferay.portal.kernel.util.PropsKeys;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.kernel.util.UnicodeProperties;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.security.ldap.LDAPSettingsUtil;
29  import com.liferay.portal.service.CompanyServiceUtil;
30  import com.liferay.portal.struts.PortletAction;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portal.util.Portal;
33  import com.liferay.portal.util.PrefsPropsUtil;
34  import com.liferay.portal.util.WebKeys;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.ActionResponse;
38  import javax.portlet.PortletConfig;
39  import javax.portlet.PortletPreferences;
40  import javax.portlet.RenderRequest;
41  import javax.portlet.RenderResponse;
42  
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="EditLDAPServerAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Ryan Park
51   */
52  public class EditLDAPServerAction extends PortletAction {
53  
54      public void processAction(
55              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
56              ActionRequest actionRequest, ActionResponse actionResponse)
57          throws Exception {
58  
59          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
60  
61          try {
62              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
63                  updateLDAPServer(actionRequest);
64              }
65              else if (cmd.equals(Constants.DELETE)) {
66                  deleteLDAPServer(actionRequest);
67              }
68  
69              sendRedirect(actionRequest, actionResponse);
70          }
71          catch (Exception e) {
72              if (e instanceof PrincipalException) {
73                  SessionErrors.add(actionRequest, e.getClass().getName());
74  
75                  setForward(actionRequest, "portlet.enterprise_admin.error");
76              }
77              else {
78                  throw e;
79              }
80          }
81      }
82  
83      public ActionForward render(
84              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
85              RenderRequest renderRequest, RenderResponse renderResponse)
86          throws Exception {
87  
88          return mapping.findForward(getForward(
89              renderRequest, "portlet.enterprise_admin.edit_ldap_server"));
90      }
91  
92      protected UnicodeProperties addLDAPServer(
93              long companyId, UnicodeProperties properties)
94          throws Exception {
95  
96          long ldapServerId = CounterLocalServiceUtil.increment();
97  
98          String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
99  
100         String[] keys = properties.keySet().toArray(new String[0]);
101 
102         for (String key : keys) {
103             if (ArrayUtil.contains(_KEYS, key)) {
104                 String value = properties.remove(key);
105 
106                 if (key.equals(PropsKeys.LDAP_SECURITY_CREDENTIALS) &&
107                     value.equals(Portal.TEMP_OBFUSCATION_VALUE)) {
108 
109                     value = PrefsPropsUtil.getString(
110                         PropsKeys.LDAP_SECURITY_CREDENTIALS);
111                 }
112 
113                 properties.setProperty(key + postfix, value);
114             }
115         }
116 
117         PortletPreferences preferences = PrefsPropsUtil.getPreferences(
118             companyId);
119 
120         String ldapServerIds = preferences.getValue(
121             "ldap.server.ids", StringPool.BLANK);
122 
123         ldapServerIds = StringUtil.add(
124             ldapServerIds, String.valueOf(ldapServerId));
125 
126         properties.setProperty("ldap.server.ids", ldapServerIds);
127 
128         return properties;
129     }
130 
131     protected void deleteLDAPServer(ActionRequest actionRequest)
132         throws Exception {
133 
134         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
135             WebKeys.THEME_DISPLAY);
136 
137         long ldapServerId = ParamUtil.getLong(actionRequest, "ldapServerId");
138 
139         // Remove preferences
140 
141         String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
142 
143         String[] keys = new String[_KEYS.length];
144 
145         for (int i = 0; i < _KEYS.length; i++) {
146             keys[i] = _KEYS[i] + postfix;
147         }
148 
149         CompanyServiceUtil.removePreferences(
150             themeDisplay.getCompanyId(), keys);
151 
152         // Update preferences
153 
154         PortletPreferences preferences = PrefsPropsUtil.getPreferences(
155             themeDisplay.getCompanyId());
156 
157         UnicodeProperties properties = new UnicodeProperties();
158 
159         String ldapServerIds = preferences.getValue(
160             "ldap.server.ids", StringPool.BLANK);
161 
162         ldapServerIds = StringUtil.remove(
163             ldapServerIds, String.valueOf(ldapServerId));
164 
165         properties.put("ldap.server.ids", ldapServerIds);
166 
167         CompanyServiceUtil.updatePreferences(
168             themeDisplay.getCompanyId(), properties);
169     }
170 
171     protected void updateLDAPServer(ActionRequest actionRequest)
172         throws Exception {
173 
174         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
175             WebKeys.THEME_DISPLAY);
176 
177         long ldapServerId = ParamUtil.getLong(actionRequest, "ldapServerId");
178 
179         UnicodeProperties properties = PropertiesParamUtil.getProperties(
180             actionRequest, "settings(");
181 
182         if (ldapServerId <= 0) {
183             properties = addLDAPServer(
184                 themeDisplay.getCompanyId(), properties);
185         }
186 
187         CompanyServiceUtil.updatePreferences(
188             themeDisplay.getCompanyId(), properties);
189     }
190 
191     private final String[] _KEYS = {
192         PropsKeys.LDAP_AUTH_SEARCH_FILTER,
193         PropsKeys.LDAP_BASE_DN,
194         PropsKeys.LDAP_BASE_PROVIDER_URL,
195         PropsKeys.LDAP_GROUP_MAPPINGS,
196         PropsKeys.LDAP_GROUPS_DN,
197         PropsKeys.LDAP_IMPORT_GROUP_SEARCH_FILTER,
198         PropsKeys.LDAP_IMPORT_USER_SEARCH_FILTER,
199         PropsKeys.LDAP_SECURITY_CREDENTIALS,
200         PropsKeys.LDAP_SECURITY_PRINCIPAL,
201         PropsKeys.LDAP_SERVER_NAME,
202         PropsKeys.LDAP_USER_DEFAULT_OBJECT_CLASSES,
203         PropsKeys.LDAP_USER_MAPPINGS,
204         PropsKeys.LDAP_USERS_DN
205     };
206 
207 }