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.NoSuchRoleException;
26  import com.liferay.portal.RoleAssignmentException;
27  import com.liferay.portal.kernel.servlet.SessionErrors;
28  import com.liferay.portal.kernel.util.Constants;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.Role;
33  import com.liferay.portal.model.RoleConstants;
34  import com.liferay.portal.security.auth.PrincipalException;
35  import com.liferay.portal.service.GroupServiceUtil;
36  import com.liferay.portal.service.RoleLocalServiceUtil;
37  import com.liferay.portal.service.UserServiceUtil;
38  import com.liferay.portal.struts.PortletAction;
39  
40  import javax.portlet.ActionRequest;
41  import javax.portlet.ActionResponse;
42  import javax.portlet.PortletConfig;
43  import javax.portlet.RenderRequest;
44  import javax.portlet.RenderResponse;
45  
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionForward;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="EditRoleAssignmentsAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class EditRoleAssignmentsAction extends PortletAction {
57  
58      public void processAction(
59              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
60              ActionRequest actionRequest, ActionResponse actionResponse)
61          throws Exception {
62  
63          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
64  
65          try {
66              if (cmd.equals("role_groups")) {
67                  updateRoleGroups(actionRequest);
68              }
69              else if (cmd.equals("role_users")) {
70                  updateRoleUsers(actionRequest);
71              }
72  
73              if (Validator.isNotNull(cmd)) {
74                  String redirect = ParamUtil.getString(
75                      actionRequest, "assignmentsRedirect");
76  
77                  sendRedirect(actionRequest, actionResponse, redirect);
78              }
79          }
80          catch (Exception e) {
81              if (e instanceof NoSuchRoleException ||
82                  e instanceof PrincipalException ||
83                  e instanceof RoleAssignmentException) {
84  
85                  SessionErrors.add(actionRequest, e.getClass().getName());
86  
87                  setForward(actionRequest, "portlet.enterprise_admin.error");
88              }
89              else {
90                  throw e;
91              }
92          }
93      }
94  
95      public ActionForward render(
96              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
97              RenderRequest renderRequest, RenderResponse renderResponse)
98          throws Exception {
99  
100         try {
101             ActionUtil.getRole(renderRequest);
102         }
103         catch (Exception e) {
104             if (e instanceof NoSuchRoleException ||
105                 e instanceof PrincipalException) {
106 
107                 SessionErrors.add(renderRequest, e.getClass().getName());
108 
109                 return mapping.findForward("portlet.enterprise_admin.error");
110             }
111             else {
112                 throw e;
113             }
114         }
115 
116         return mapping.findForward(getForward(
117             renderRequest, "portlet.enterprise_admin.edit_role_assignments"));
118     }
119 
120     protected void updateRoleGroups(ActionRequest actionRequest)
121         throws Exception {
122 
123         long roleId = ParamUtil.getLong(actionRequest, "roleId");
124 
125         long[] addGroupIds = StringUtil.split(
126             ParamUtil.getString(actionRequest, "addGroupIds"), 0L);
127         long[] removeGroupIds = StringUtil.split(
128             ParamUtil.getString(actionRequest, "removeGroupIds"), 0L);
129 
130         Role role = RoleLocalServiceUtil.getRole(roleId);
131 
132         if (role.getName().equals(RoleConstants.OWNER)) {
133             throw new RoleAssignmentException(role.getName());
134         }
135 
136         GroupServiceUtil.addRoleGroups(roleId, addGroupIds);
137         GroupServiceUtil.unsetRoleGroups(roleId, removeGroupIds);
138     }
139 
140     protected void updateRoleUsers(ActionRequest actionRequest)
141         throws Exception {
142 
143         long roleId = ParamUtil.getLong(actionRequest, "roleId");
144 
145         long[] addUserIds = StringUtil.split(
146             ParamUtil.getString(actionRequest, "addUserIds"), 0L);
147         long[] removeUserIds = StringUtil.split(
148             ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
149 
150         Role role = RoleLocalServiceUtil.getRole(roleId);
151 
152         if (role.getName().equals(RoleConstants.OWNER)) {
153             throw new RoleAssignmentException(role.getName());
154         }
155 
156         UserServiceUtil.addRoleUsers(roleId, addUserIds);
157         UserServiceUtil.unsetRoleUsers(roleId, removeUserIds);
158     }
159 
160 }