1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.portal.NoSuchPasswordPolicyException;
26  import com.liferay.portal.kernel.servlet.SessionErrors;
27  import com.liferay.portal.kernel.util.Constants;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.service.OrganizationServiceUtil;
33  import com.liferay.portal.service.UserServiceUtil;
34  import com.liferay.portal.struts.PortletAction;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.ActionResponse;
38  import javax.portlet.PortletConfig;
39  import javax.portlet.RenderRequest;
40  import javax.portlet.RenderResponse;
41  
42  import org.apache.struts.action.ActionForm;
43  import org.apache.struts.action.ActionForward;
44  import org.apache.struts.action.ActionMapping;
45  
46  /**
47   * <a href="EditPasswordPolicyAssignmentsAction.java.html"><b><i>View Source</i>
48   * </b></a>
49   *
50   * @author Scott Lee
51   *
52   */
53  public class EditPasswordPolicyAssignmentsAction extends PortletAction {
54  
55      public void processAction(
56              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
57              ActionRequest actionRequest, ActionResponse actionResponse)
58          throws Exception {
59  
60          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
61  
62          try {
63              if (cmd.equals("password_policy_organizations")) {
64                  updatePasswordPolicyOrganizations(actionRequest);
65              }
66              else if (cmd.equals("password_policy_users")) {
67                  updatePasswordPolicyUsers(actionRequest);
68              }
69  
70              if (Validator.isNotNull(cmd)) {
71                  String redirect = ParamUtil.getString(
72                      actionRequest, "assignmentsRedirect");
73  
74                  sendRedirect(actionRequest, actionResponse, redirect);
75              }
76          }
77          catch (Exception e) {
78              if (e instanceof NoSuchPasswordPolicyException ||
79                  e instanceof PrincipalException) {
80  
81                  SessionErrors.add(actionRequest, e.getClass().getName());
82  
83                  setForward(actionRequest, "portlet.enterprise_admin.error");
84              }
85              else {
86                  throw e;
87              }
88          }
89      }
90  
91      public ActionForward render(
92              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
93              RenderRequest renderRequest, RenderResponse renderResponse)
94          throws Exception {
95  
96          try {
97              ActionUtil.getPasswordPolicy(renderRequest);
98          }
99          catch (Exception e) {
100             if (e instanceof NoSuchPasswordPolicyException ||
101                 e instanceof PrincipalException) {
102 
103                 SessionErrors.add(renderRequest, e.getClass().getName());
104 
105                 return mapping.findForward("portlet.enterprise_admin.error");
106             }
107             else {
108                 throw e;
109             }
110         }
111 
112         return mapping.findForward(getForward(
113             renderRequest,
114             "portlet.enterprise_admin.edit_password_policy_assignments"));
115     }
116 
117     protected void updatePasswordPolicyOrganizations(
118             ActionRequest actionRequest)
119         throws Exception {
120 
121         long passwordPolicyId = ParamUtil.getLong(
122             actionRequest, "passwordPolicyId");
123 
124         long[] addOrganizationIds = StringUtil.split(
125             ParamUtil.getString(actionRequest, "addOrganizationIds"), 0L);
126         long[] removeOrganizationIds = StringUtil.split(
127             ParamUtil.getString(actionRequest, "removeOrganizationIds"), 0L);
128 
129         OrganizationServiceUtil.addPasswordPolicyOrganizations(
130             passwordPolicyId, addOrganizationIds);
131         OrganizationServiceUtil.unsetPasswordPolicyOrganizations(
132             passwordPolicyId, removeOrganizationIds);
133     }
134 
135     protected void updatePasswordPolicyUsers(ActionRequest actionRequest)
136         throws Exception {
137 
138         long passwordPolicyId = ParamUtil.getLong(
139             actionRequest, "passwordPolicyId");
140 
141         long[] addUserIds = StringUtil.split(
142             ParamUtil.getString(actionRequest, "addUserIds"), 0L);
143         long[] removeUserIds = StringUtil.split(
144             ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
145 
146         UserServiceUtil.addPasswordPolicyUsers(passwordPolicyId, addUserIds);
147         UserServiceUtil.unsetPasswordPolicyUsers(
148             passwordPolicyId, removeUserIds);
149     }
150 
151 }