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.NoSuchUserGroupException;
18  import com.liferay.portal.kernel.servlet.SessionErrors;
19  import com.liferay.portal.kernel.util.Constants;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.security.auth.PrincipalException;
24  import com.liferay.portal.service.UserServiceUtil;
25  import com.liferay.portal.struts.PortletAction;
26  
27  import javax.portlet.ActionRequest;
28  import javax.portlet.ActionResponse;
29  import javax.portlet.PortletConfig;
30  import javax.portlet.RenderRequest;
31  import javax.portlet.RenderResponse;
32  
33  import org.apache.struts.action.ActionForm;
34  import org.apache.struts.action.ActionForward;
35  import org.apache.struts.action.ActionMapping;
36  
37  /**
38   * <a href="EditUserGroupAssignmentsAction.java.html"><b><i>View Source</i></b>
39   * </a>
40   *
41   * @author Charles May
42   */
43  public class EditUserGroupAssignmentsAction extends PortletAction {
44  
45      public void processAction(
46              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
47              ActionRequest actionRequest, ActionResponse actionResponse)
48          throws Exception {
49  
50          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
51  
52          try {
53              if (cmd.equals("user_group_users")) {
54                  updateUserGroupUsers(actionRequest);
55              }
56  
57              if (Validator.isNotNull(cmd)) {
58                  String redirect = ParamUtil.getString(
59                      actionRequest, "assignmentsRedirect");
60  
61                  sendRedirect(actionRequest, actionResponse, redirect);
62              }
63          }
64          catch (Exception e) {
65              if (e instanceof NoSuchUserGroupException ||
66                  e instanceof PrincipalException) {
67  
68                  SessionErrors.add(actionRequest, e.getClass().getName());
69  
70                  setForward(actionRequest, "portlet.enterprise_admin.error");
71              }
72              else {
73                  throw e;
74              }
75          }
76      }
77  
78      public ActionForward render(
79              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
80              RenderRequest renderRequest, RenderResponse renderResponse)
81          throws Exception {
82  
83          try {
84              ActionUtil.getUserGroup(renderRequest);
85          }
86          catch (Exception e) {
87              if (e instanceof NoSuchUserGroupException ||
88                  e instanceof PrincipalException) {
89  
90                  SessionErrors.add(renderRequest, e.getClass().getName());
91  
92                  return mapping.findForward("portlet.enterprise_admin.error");
93              }
94              else {
95                  throw e;
96              }
97          }
98  
99          return mapping.findForward(getForward(
100             renderRequest,
101             "portlet.enterprise_admin.edit_user_group_assignments"));
102     }
103 
104     protected void updateUserGroupUsers(ActionRequest actionRequest)
105         throws Exception {
106 
107         long userGroupId = ParamUtil.getLong(actionRequest, "userGroupId");
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         UserServiceUtil.addUserGroupUsers(userGroupId, addUserIds);
115         UserServiceUtil.unsetUserGroupUsers(userGroupId, removeUserIds);
116     }
117 
118 }