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