1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.enterpriseadmin.action;
16  
17  import com.liferay.portal.NoSuchRoleException;
18  import com.liferay.portal.RoleAssignmentException;
19  import com.liferay.portal.kernel.servlet.SessionErrors;
20  import com.liferay.portal.kernel.util.Constants;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.model.Role;
25  import com.liferay.portal.model.RoleConstants;
26  import com.liferay.portal.security.auth.PrincipalException;
27  import com.liferay.portal.service.GroupServiceUtil;
28  import com.liferay.portal.service.RoleLocalServiceUtil;
29  import com.liferay.portal.service.UserServiceUtil;
30  import com.liferay.portal.struts.PortletAction;
31  
32  import javax.portlet.ActionRequest;
33  import javax.portlet.ActionResponse;
34  import javax.portlet.PortletConfig;
35  import javax.portlet.RenderRequest;
36  import javax.portlet.RenderResponse;
37  
38  import org.apache.struts.action.ActionForm;
39  import org.apache.struts.action.ActionForward;
40  import org.apache.struts.action.ActionMapping;
41  
42  /**
43   * <a href="EditRoleAssignmentsAction.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   */
47  public class EditRoleAssignmentsAction extends PortletAction {
48  
49      public void processAction(
50              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
51              ActionRequest actionRequest, ActionResponse actionResponse)
52          throws Exception {
53  
54          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
55  
56          try {
57              if (cmd.equals("role_groups")) {
58                  updateRoleGroups(actionRequest);
59              }
60              else if (cmd.equals("role_users")) {
61                  updateRoleUsers(actionRequest);
62              }
63  
64              if (Validator.isNotNull(cmd)) {
65                  String redirect = ParamUtil.getString(
66                      actionRequest, "assignmentsRedirect");
67  
68                  sendRedirect(actionRequest, actionResponse, redirect);
69              }
70          }
71          catch (Exception e) {
72              if (e instanceof NoSuchRoleException ||
73                  e instanceof PrincipalException ||
74                  e instanceof RoleAssignmentException) {
75  
76                  SessionErrors.add(actionRequest, e.getClass().getName());
77  
78                  setForward(actionRequest, "portlet.enterprise_admin.error");
79              }
80              else {
81                  throw e;
82              }
83          }
84      }
85  
86      public ActionForward render(
87              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
88              RenderRequest renderRequest, RenderResponse renderResponse)
89          throws Exception {
90  
91          try {
92              ActionUtil.getRole(renderRequest);
93          }
94          catch (Exception e) {
95              if (e instanceof NoSuchRoleException ||
96                  e instanceof PrincipalException) {
97  
98                  SessionErrors.add(renderRequest, e.getClass().getName());
99  
100                 return mapping.findForward("portlet.enterprise_admin.error");
101             }
102             else {
103                 throw e;
104             }
105         }
106 
107         return mapping.findForward(getForward(
108             renderRequest, "portlet.enterprise_admin.edit_role_assignments"));
109     }
110 
111     protected void updateRoleGroups(ActionRequest actionRequest)
112         throws Exception {
113 
114         long roleId = ParamUtil.getLong(actionRequest, "roleId");
115 
116         long[] addGroupIds = StringUtil.split(
117             ParamUtil.getString(actionRequest, "addGroupIds"), 0L);
118         long[] removeGroupIds = StringUtil.split(
119             ParamUtil.getString(actionRequest, "removeGroupIds"), 0L);
120 
121         Role role = RoleLocalServiceUtil.getRole(roleId);
122 
123         if (role.getName().equals(RoleConstants.OWNER)) {
124             throw new RoleAssignmentException(role.getName());
125         }
126 
127         GroupServiceUtil.addRoleGroups(roleId, addGroupIds);
128         GroupServiceUtil.unsetRoleGroups(roleId, removeGroupIds);
129     }
130 
131     protected void updateRoleUsers(ActionRequest actionRequest)
132         throws Exception {
133 
134         long roleId = ParamUtil.getLong(actionRequest, "roleId");
135 
136         long[] addUserIds = StringUtil.split(
137             ParamUtil.getString(actionRequest, "addUserIds"), 0L);
138         long[] removeUserIds = StringUtil.split(
139             ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
140 
141         Role role = RoleLocalServiceUtil.getRole(roleId);
142 
143         if (role.getName().equals(RoleConstants.OWNER)) {
144             throw new RoleAssignmentException(role.getName());
145         }
146 
147         UserServiceUtil.addRoleUsers(roleId, addUserIds);
148         UserServiceUtil.unsetRoleUsers(roleId, removeUserIds);
149     }
150 
151 }