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.communities.action;
16  
17  import com.liferay.portal.NoSuchGroupException;
18  import com.liferay.portal.NoSuchRoleException;
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.model.Role;
24  import com.liferay.portal.model.RoleConstants;
25  import com.liferay.portal.security.auth.PrincipalException;
26  import com.liferay.portal.service.UserGroupRoleServiceUtil;
27  import com.liferay.portal.struts.PortletAction;
28  import com.liferay.portal.util.WebKeys;
29  
30  import javax.portlet.ActionRequest;
31  import javax.portlet.ActionResponse;
32  import javax.portlet.PortletConfig;
33  import javax.portlet.RenderRequest;
34  import javax.portlet.RenderResponse;
35  
36  import org.apache.struts.action.ActionForm;
37  import org.apache.struts.action.ActionForward;
38  import org.apache.struts.action.ActionMapping;
39  
40  /**
41   * <a href="EditUserRolesAction.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Charles May
44   */
45  public class EditUserRolesAction extends PortletAction {
46  
47      public void processAction(
48              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
49              ActionRequest actionRequest, ActionResponse actionResponse)
50          throws Exception {
51  
52          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
53  
54          try {
55              if (cmd.equals("user_group_role_users")) {
56                  updateUserGroupRoleUsers(actionRequest);
57              }
58  
59              sendRedirect(actionRequest, actionResponse);
60          }
61          catch (Exception e) {
62              if (e instanceof PrincipalException) {
63                  SessionErrors.add(actionRequest, e.getClass().getName());
64  
65                  setForward(actionRequest, "portlet.communities.error");
66              }
67              else {
68                  throw e;
69              }
70          }
71      }
72  
73      public ActionForward render(
74              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
75              RenderRequest renderRequest, RenderResponse renderResponse)
76          throws Exception {
77  
78          try {
79              ActionUtil.getGroup(renderRequest);
80              ActionUtil.getRole(renderRequest);
81  
82              Role role = (Role)renderRequest.getAttribute(WebKeys.ROLE);
83  
84              if (role != null) {
85                  String name = role.getName();
86  
87                  if (name.equals(RoleConstants.COMMUNITY_MEMBER) ||
88                      name.equals(RoleConstants.ORGANIZATION_MEMBER)) {
89  
90                      throw new NoSuchRoleException();
91                  }
92              }
93          }
94          catch (Exception e) {
95              if (e instanceof NoSuchGroupException ||
96                  e instanceof NoSuchRoleException ||
97                  e instanceof PrincipalException) {
98  
99                  SessionErrors.add(renderRequest, e.getClass().getName());
100 
101                 return mapping.findForward("portlet.communities.error");
102             }
103             else {
104                 throw e;
105             }
106         }
107 
108         return mapping.findForward(
109             getForward(renderRequest, "portlet.communities.edit_user_roles"));
110     }
111 
112     protected void updateUserGroupRoleUsers(ActionRequest actionRequest)
113         throws Exception {
114 
115         long groupId = ParamUtil.getLong(actionRequest, "groupId");
116         long roleId = ParamUtil.getLong(actionRequest, "roleId");
117 
118         long[] addUserIds = StringUtil.split(
119             ParamUtil.getString(actionRequest, "addUserIds"), 0L);
120         long[] removeUserIds = StringUtil.split(
121             ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
122 
123         UserGroupRoleServiceUtil.addUserGroupRoles(addUserIds, groupId, roleId);
124         UserGroupRoleServiceUtil.deleteUserGroupRoles(
125             removeUserIds, groupId, roleId);
126     }
127 
128 }