1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.NoSuchUserException;
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.model.User;
32  import com.liferay.portal.security.auth.PrincipalException;
33  import com.liferay.portal.service.RoleServiceUtil;
34  import com.liferay.portal.struts.PortletAction;
35  import com.liferay.portal.util.PortalUtil;
36  
37  import javax.portlet.ActionRequest;
38  import javax.portlet.ActionResponse;
39  import javax.portlet.PortletConfig;
40  import javax.portlet.RenderRequest;
41  import javax.portlet.RenderResponse;
42  
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="EditUserRegularRoleAssignmentsAction.java.html"><b><i>View Source
49   * </i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   *
53   */
54  public class EditUserRegularRoleAssignmentsAction extends PortletAction {
55  
56      public void processAction(
57              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
58              ActionRequest actionRequest, ActionResponse actionResponse)
59          throws Exception {
60  
61          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
62  
63          try {
64              if (Validator.isNotNull(cmd)) {
65                  updateUserRoles(actionRequest);
66  
67                  sendRedirect(actionRequest, actionResponse);
68              }
69          }
70          catch (Exception e) {
71              if (e instanceof NoSuchUserException ||
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              PortalUtil.getSelectedUser(renderRequest);
91          }
92          catch (Exception e) {
93              if (e instanceof NoSuchUserException ||
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_regular_role_assignments"));
108     }
109 
110     protected void updateUserRoles(ActionRequest actionRequest)
111         throws Exception {
112 
113         User user = PortalUtil.getSelectedUser(actionRequest);
114 
115         long[] addRoleIds = StringUtil.split(
116             ParamUtil.getString(actionRequest, "addRoleIds"), 0L);
117         long[] removeRoleIds = StringUtil.split(
118             ParamUtil.getString(actionRequest, "removeRoleIds"), 0L);
119 
120         RoleServiceUtil.addUserRoles(user.getUserId(), addRoleIds);
121         RoleServiceUtil.unsetUserRoles(user.getUserId(), removeRoleIds);
122     }
123 
124 }