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