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.communities.action;
21  
22  import com.liferay.portal.NoSuchGroupException;
23  import com.liferay.portal.NoSuchRoleException;
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.security.auth.PrincipalException;
29  import com.liferay.portal.service.UserGroupRoleServiceUtil;
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="EditUserRolesAction.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Charles May
46   *
47   */
48  public class EditUserRolesAction extends PortletAction {
49  
50      public void processAction(
51              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
52              ActionRequest actionRequest, ActionResponse actionResponse)
53          throws Exception {
54  
55          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
56  
57          try {
58              if (cmd.equals("user_group_role_users")) {
59                  updateUserGroupRoleUsers(actionRequest);
60              }
61  
62              sendRedirect(actionRequest, actionResponse);
63          }
64          catch (Exception e) {
65              if (e instanceof PrincipalException) {
66                  SessionErrors.add(actionRequest, e.getClass().getName());
67  
68                  setForward(actionRequest, "portlet.communities.error");
69              }
70              else {
71                  throw e;
72              }
73          }
74      }
75  
76      public ActionForward render(
77              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
78              RenderRequest renderRequest, RenderResponse renderResponse)
79          throws Exception {
80  
81          try {
82              ActionUtil.getGroup(renderRequest);
83              ActionUtil.getRole(renderRequest);
84          }
85          catch (Exception e) {
86              if (e instanceof NoSuchGroupException ||
87                  e instanceof NoSuchRoleException ||
88                  e instanceof PrincipalException) {
89  
90                  SessionErrors.add(renderRequest, e.getClass().getName());
91  
92                  return mapping.findForward("portlet.communities.error");
93              }
94              else {
95                  throw e;
96              }
97          }
98  
99          return mapping.findForward(
100             getForward(renderRequest, "portlet.communities.edit_user_roles"));
101     }
102 
103     protected void updateUserGroupRoleUsers(ActionRequest actionRequest)
104         throws Exception {
105 
106         long groupId = ParamUtil.getLong(actionRequest, "groupId");
107         long roleId = ParamUtil.getLong(actionRequest, "roleId");
108 
109         long[] addUserIds = StringUtil.split(
110             ParamUtil.getString(actionRequest, "addUserIds"), 0L);
111         long[] removeUserIds = StringUtil.split(
112             ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
113 
114         UserGroupRoleServiceUtil.addUserGroupRoles(addUserIds, groupId, roleId);
115         UserGroupRoleServiceUtil.deleteUserGroupRoles(
116             removeUserIds, groupId, roleId);
117     }
118 
119 }