1   /**
2    * Copyright (c) 2000-2007 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.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.service.OrganizationServiceUtil;
32  import com.liferay.portal.service.UserServiceUtil;
33  import com.liferay.portal.struts.PortletAction;
34  import com.liferay.util.servlet.SessionErrors;
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 config,
57              ActionRequest req, ActionResponse res)
58          throws Exception {
59  
60          String cmd = ParamUtil.getString(req, Constants.CMD);
61  
62          try {
63              if (cmd.equals("password_policy_organizations")) {
64                  updatePasswordPolicyOrganizations(req);
65              }
66              else if (cmd.equals("password_policy_users")) {
67                  updatePasswordPolicyUsers(req);
68              }
69  
70              if (Validator.isNotNull(cmd)) {
71                  sendRedirect(req, res);
72              }
73          }
74          catch (Exception e) {
75              if (e instanceof NoSuchPasswordPolicyException ||
76                  e instanceof PrincipalException) {
77  
78                  SessionErrors.add(req, e.getClass().getName());
79  
80                  setForward(req, "portlet.enterprise_admin.error");
81              }
82              else {
83                  throw e;
84              }
85          }
86      }
87  
88      public ActionForward render(
89              ActionMapping mapping, ActionForm form, PortletConfig config,
90              RenderRequest req, RenderResponse res)
91          throws Exception {
92  
93          try {
94              ActionUtil.getPasswordPolicy(req);
95          }
96          catch (Exception e) {
97              if (e instanceof NoSuchPasswordPolicyException ||
98                  e instanceof PrincipalException) {
99  
100                 SessionErrors.add(req, e.getClass().getName());
101 
102                 return mapping.findForward("portlet.enterprise_admin.error");
103             }
104             else {
105                 throw e;
106             }
107         }
108 
109         return mapping.findForward(getForward(
110             req, "portlet.enterprise_admin.edit_password_policy_assignments"));
111     }
112 
113     protected void updatePasswordPolicyOrganizations(ActionRequest req)
114         throws Exception {
115 
116         long passwordPolicyId = ParamUtil.getLong(req, "passwordPolicyId");
117 
118         long[] addOrganizationIds = StringUtil.split(
119             ParamUtil.getString(req, "addOrganizationIds"), 0L);
120         long[] removeOrganizationIds = StringUtil.split(
121             ParamUtil.getString(req, "removeOrganizationIds"), 0L);
122 
123         OrganizationServiceUtil.addPasswordPolicyOrganizations(
124             passwordPolicyId, addOrganizationIds);
125         OrganizationServiceUtil.unsetPasswordPolicyOrganizations(
126             passwordPolicyId, removeOrganizationIds);
127     }
128 
129     protected void updatePasswordPolicyUsers(ActionRequest req)
130         throws Exception {
131 
132         long passwordPolicyId = ParamUtil.getLong(req, "passwordPolicyId");
133 
134         long[] addUserIds = StringUtil.split(
135             ParamUtil.getString(req, "addUserIds"), 0L);
136         long[] removeUserIds = StringUtil.split(
137             ParamUtil.getString(req, "removeUserIds"), 0L);
138 
139         UserServiceUtil.addPasswordPolicyUsers(passwordPolicyId, addUserIds);
140         UserServiceUtil.unsetPasswordPolicyUsers(
141             passwordPolicyId, removeUserIds);
142     }
143 
144 }