1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.enterpriseadmin.action;
24  
25  import com.liferay.counter.service.CounterLocalServiceUtil;
26  import com.liferay.portal.kernel.servlet.SessionErrors;
27  import com.liferay.portal.kernel.util.ArrayUtil;
28  import com.liferay.portal.kernel.util.Constants;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.PropertiesParamUtil;
31  import com.liferay.portal.kernel.util.PropsKeys;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.kernel.util.UnicodeProperties;
35  import com.liferay.portal.security.auth.PrincipalException;
36  import com.liferay.portal.security.ldap.PortalLDAPUtil;
37  import com.liferay.portal.service.CompanyServiceUtil;
38  import com.liferay.portal.struts.PortletAction;
39  import com.liferay.portal.theme.ThemeDisplay;
40  import com.liferay.portal.util.PrefsPropsUtil;
41  import com.liferay.portal.util.WebKeys;
42  
43  import javax.portlet.ActionRequest;
44  import javax.portlet.ActionResponse;
45  import javax.portlet.PortletConfig;
46  import javax.portlet.PortletPreferences;
47  import javax.portlet.RenderRequest;
48  import javax.portlet.RenderResponse;
49  
50  import org.apache.struts.action.ActionForm;
51  import org.apache.struts.action.ActionForward;
52  import org.apache.struts.action.ActionMapping;
53  
54  /**
55   * <a href="EditLDAPServerAction.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Ryan Park
58   */
59  public class EditLDAPServerAction extends PortletAction {
60  
61      public void processAction(
62              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
63              ActionRequest actionRequest, ActionResponse actionResponse)
64          throws Exception {
65  
66          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
67  
68          try {
69              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
70                  updateLDAPServer(actionRequest);
71              }
72              else if (cmd.equals(Constants.DELETE)) {
73                  deleteLDAPServer(actionRequest);
74              }
75  
76              sendRedirect(actionRequest, actionResponse);
77          }
78          catch (Exception e) {
79              if (e instanceof PrincipalException) {
80                  SessionErrors.add(actionRequest, e.getClass().getName());
81  
82                  setForward(actionRequest, "portlet.enterprise_admin.error");
83              }
84              else {
85                  throw e;
86              }
87          }
88      }
89  
90      public ActionForward render(
91              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
92              RenderRequest renderRequest, RenderResponse renderResponse)
93          throws Exception {
94  
95          return mapping.findForward(getForward(
96              renderRequest, "portlet.enterprise_admin.edit_ldap_server"));
97      }
98  
99      protected UnicodeProperties addLDAPServer(
100             long companyId, UnicodeProperties properties)
101         throws Exception {
102 
103         long ldapServerId = CounterLocalServiceUtil.increment();
104 
105         String postfix = PortalLDAPUtil.getPropertyPostfix(ldapServerId);
106 
107         String[] keys = properties.keySet().toArray(new String[0]);
108 
109         for (String key : keys) {
110             if (ArrayUtil.contains(_KEYS, key)) {
111                 String value = properties.remove(key);
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 = PortalLDAPUtil.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 }