1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.enterpriseadmin.action;
21  
22  import com.liferay.portal.NoSuchRoleException;
23  import com.liferay.portal.RoleAssignmentException;
24  import com.liferay.portal.kernel.servlet.SessionErrors;
25  import com.liferay.portal.kernel.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Role;
30  import com.liferay.portal.model.RoleConstants;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.service.GroupServiceUtil;
33  import com.liferay.portal.service.RoleLocalServiceUtil;
34  import com.liferay.portal.service.UserServiceUtil;
35  import com.liferay.portal.struts.PortletAction;
36  
37  import javax.portlet.ActionRequest;
38  import javax.portlet.ActionResponse;
39  import javax.portlet.PortletConfig;
40  import javax.portlet.RenderRequest;
41  import javax.portlet.RenderResponse;
42  
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="EditRoleAssignmentsAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class EditRoleAssignmentsAction 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("role_groups")) {
64                  updateRoleGroups(actionRequest);
65              }
66              else if (cmd.equals("role_users")) {
67                  updateRoleUsers(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 NoSuchRoleException ||
79                  e instanceof PrincipalException ||
80                  e instanceof RoleAssignmentException) {
81  
82                  SessionErrors.add(actionRequest, e.getClass().getName());
83  
84                  setForward(actionRequest, "portlet.enterprise_admin.error");
85              }
86              else {
87                  throw e;
88              }
89          }
90      }
91  
92      public ActionForward render(
93              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
94              RenderRequest renderRequest, RenderResponse renderResponse)
95          throws Exception {
96  
97          try {
98              ActionUtil.getRole(renderRequest);
99          }
100         catch (Exception e) {
101             if (e instanceof NoSuchRoleException ||
102                 e instanceof PrincipalException) {
103 
104                 SessionErrors.add(renderRequest, e.getClass().getName());
105 
106                 return mapping.findForward("portlet.enterprise_admin.error");
107             }
108             else {
109                 throw e;
110             }
111         }
112 
113         return mapping.findForward(getForward(
114             renderRequest, "portlet.enterprise_admin.edit_role_assignments"));
115     }
116 
117     protected void updateRoleGroups(ActionRequest actionRequest)
118         throws Exception {
119 
120         long roleId = ParamUtil.getLong(actionRequest, "roleId");
121 
122         long[] addGroupIds = StringUtil.split(
123             ParamUtil.getString(actionRequest, "addGroupIds"), 0L);
124         long[] removeGroupIds = StringUtil.split(
125             ParamUtil.getString(actionRequest, "removeGroupIds"), 0L);
126 
127         Role role = RoleLocalServiceUtil.getRole(roleId);
128 
129         if (role.getName().equals(RoleConstants.OWNER)) {
130             throw new RoleAssignmentException(role.getName());
131         }
132 
133         GroupServiceUtil.addRoleGroups(roleId, addGroupIds);
134         GroupServiceUtil.unsetRoleGroups(roleId, removeGroupIds);
135     }
136 
137     protected void updateRoleUsers(ActionRequest actionRequest)
138         throws Exception {
139 
140         long roleId = ParamUtil.getLong(actionRequest, "roleId");
141 
142         long[] addUserIds = StringUtil.split(
143             ParamUtil.getString(actionRequest, "addUserIds"), 0L);
144         long[] removeUserIds = StringUtil.split(
145             ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
146 
147         Role role = RoleLocalServiceUtil.getRole(roleId);
148 
149         if (role.getName().equals(RoleConstants.OWNER)) {
150             throw new RoleAssignmentException(role.getName());
151         }
152 
153         UserServiceUtil.addRoleUsers(roleId, addUserIds);
154         UserServiceUtil.unsetRoleUsers(roleId, removeUserIds);
155     }
156 
157 }