1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class EditRoleAssignmentsAction extends PortletAction {
56  
57      public void processAction(
58              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
59              ActionRequest actionRequest, ActionResponse actionResponse)
60          throws Exception {
61  
62          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
63  
64          try {
65              if (cmd.equals("role_groups")) {
66                  updateRoleGroups(actionRequest);
67              }
68              else if (cmd.equals("role_users")) {
69                  updateRoleUsers(actionRequest);
70              }
71  
72              if (Validator.isNotNull(cmd)) {
73                  String redirect = ParamUtil.getString(
74                      actionRequest, "assignmentsRedirect");
75  
76                  sendRedirect(actionRequest, actionResponse, redirect);
77              }
78          }
79          catch (Exception e) {
80              if (e instanceof NoSuchRoleException ||
81                  e instanceof PrincipalException ||
82                  e instanceof RoleAssignmentException) {
83  
84                  SessionErrors.add(actionRequest, e.getClass().getName());
85  
86                  setForward(actionRequest, "portlet.enterprise_admin.error");
87              }
88              else {
89                  throw e;
90              }
91          }
92      }
93  
94      public ActionForward render(
95              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
96              RenderRequest renderRequest, RenderResponse renderResponse)
97          throws Exception {
98  
99          try {
100             ActionUtil.getRole(renderRequest);
101         }
102         catch (Exception e) {
103             if (e instanceof NoSuchRoleException ||
104                 e instanceof PrincipalException) {
105 
106                 SessionErrors.add(renderRequest, e.getClass().getName());
107 
108                 return mapping.findForward("portlet.enterprise_admin.error");
109             }
110             else {
111                 throw e;
112             }
113         }
114 
115         return mapping.findForward(getForward(
116             renderRequest, "portlet.enterprise_admin.edit_role_assignments"));
117     }
118 
119     protected void updateRoleGroups(ActionRequest actionRequest)
120         throws Exception {
121 
122         long roleId = ParamUtil.getLong(actionRequest, "roleId");
123 
124         long[] addGroupIds = StringUtil.split(
125             ParamUtil.getString(actionRequest, "addGroupIds"), 0L);
126         long[] removeGroupIds = StringUtil.split(
127             ParamUtil.getString(actionRequest, "removeGroupIds"), 0L);
128 
129         Role role = RoleLocalServiceUtil.getRole(roleId);
130 
131         if (role.getName().equals(RoleConstants.OWNER)) {
132             throw new RoleAssignmentException(role.getName());
133         }
134 
135         GroupServiceUtil.addRoleGroups(roleId, addGroupIds);
136         GroupServiceUtil.unsetRoleGroups(roleId, removeGroupIds);
137     }
138 
139     protected void updateRoleUsers(ActionRequest actionRequest)
140         throws Exception {
141 
142         long roleId = ParamUtil.getLong(actionRequest, "roleId");
143 
144         long[] addUserIds = StringUtil.split(
145             ParamUtil.getString(actionRequest, "addUserIds"), 0L);
146         long[] removeUserIds = StringUtil.split(
147             ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
148 
149         Role role = RoleLocalServiceUtil.getRole(roleId);
150 
151         if (role.getName().equals(RoleConstants.OWNER)) {
152             throw new RoleAssignmentException(role.getName());
153         }
154 
155         UserServiceUtil.addRoleUsers(roleId, addUserIds);
156         UserServiceUtil.unsetRoleUsers(roleId, removeUserIds);
157     }
158 
159 }