1   /**
2    * Copyright (c) 2000-2008 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                  String redirect = ParamUtil.getString(
72                      req, "assignmentsRedirect");
73  
74                  sendRedirect(req, res, redirect);
75              }
76          }
77          catch (Exception e) {
78              if (e instanceof NoSuchPasswordPolicyException ||
79                  e instanceof PrincipalException) {
80  
81                  SessionErrors.add(req, e.getClass().getName());
82  
83                  setForward(req, "portlet.enterprise_admin.error");
84              }
85              else {
86                  throw e;
87              }
88          }
89      }
90  
91      public ActionForward render(
92              ActionMapping mapping, ActionForm form, PortletConfig config,
93              RenderRequest req, RenderResponse res)
94          throws Exception {
95  
96          try {
97              ActionUtil.getPasswordPolicy(req);
98          }
99          catch (Exception e) {
100             if (e instanceof NoSuchPasswordPolicyException ||
101                 e instanceof PrincipalException) {
102 
103                 SessionErrors.add(req, 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             req, "portlet.enterprise_admin.edit_password_policy_assignments"));
114     }
115 
116     protected void updatePasswordPolicyOrganizations(ActionRequest req)
117         throws Exception {
118 
119         long passwordPolicyId = ParamUtil.getLong(req, "passwordPolicyId");
120 
121         long[] addOrganizationIds = StringUtil.split(
122             ParamUtil.getString(req, "addOrganizationIds"), 0L);
123         long[] removeOrganizationIds = StringUtil.split(
124             ParamUtil.getString(req, "removeOrganizationIds"), 0L);
125 
126         OrganizationServiceUtil.addPasswordPolicyOrganizations(
127             passwordPolicyId, addOrganizationIds);
128         OrganizationServiceUtil.unsetPasswordPolicyOrganizations(
129             passwordPolicyId, removeOrganizationIds);
130     }
131 
132     protected void updatePasswordPolicyUsers(ActionRequest req)
133         throws Exception {
134 
135         long passwordPolicyId = ParamUtil.getLong(req, "passwordPolicyId");
136 
137         long[] addUserIds = StringUtil.split(
138             ParamUtil.getString(req, "addUserIds"), 0L);
139         long[] removeUserIds = StringUtil.split(
140             ParamUtil.getString(req, "removeUserIds"), 0L);
141 
142         UserServiceUtil.addPasswordPolicyUsers(passwordPolicyId, addUserIds);
143         UserServiceUtil.unsetPasswordPolicyUsers(
144             passwordPolicyId, removeUserIds);
145     }
146 
147 }