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.NoSuchUserException;
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.model.User;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.service.RoleServiceUtil;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.util.PortalUtil;
33  
34  import javax.portlet.ActionRequest;
35  import javax.portlet.ActionResponse;
36  import javax.portlet.PortletConfig;
37  import javax.portlet.RenderRequest;
38  import javax.portlet.RenderResponse;
39  
40  import org.apache.struts.action.ActionForm;
41  import org.apache.struts.action.ActionForward;
42  import org.apache.struts.action.ActionMapping;
43  
44  /**
45   * <a href="EditUserRegularRoleAssignmentsAction.java.html"><b><i>View Source
46   * </i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class EditUserRegularRoleAssignmentsAction 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 (Validator.isNotNull(cmd)) {
62                  updateUserRoles(actionRequest);
63  
64                  sendRedirect(actionRequest, actionResponse);
65              }
66          }
67          catch (Exception e) {
68              if (e instanceof NoSuchUserException ||
69                  e instanceof PrincipalException) {
70  
71                  SessionErrors.add(actionRequest, e.getClass().getName());
72  
73                  setForward(actionRequest, "portlet.enterprise_admin.error");
74              }
75              else {
76                  throw e;
77              }
78          }
79      }
80  
81      public ActionForward render(
82              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
83              RenderRequest renderRequest, RenderResponse renderResponse)
84          throws Exception {
85  
86          try {
87              PortalUtil.getSelectedUser(renderRequest);
88          }
89          catch (Exception e) {
90              if (e instanceof NoSuchUserException ||
91                  e instanceof PrincipalException) {
92  
93                  SessionErrors.add(renderRequest, e.getClass().getName());
94  
95                  return mapping.findForward("portlet.enterprise_admin.error");
96              }
97              else {
98                  throw e;
99              }
100         }
101 
102         return mapping.findForward(getForward(
103             renderRequest,
104             "portlet.enterprise_admin.edit_user_regular_role_assignments"));
105     }
106 
107     protected void updateUserRoles(ActionRequest actionRequest)
108         throws Exception {
109 
110         User user = PortalUtil.getSelectedUser(actionRequest);
111 
112         long[] addRoleIds = StringUtil.split(
113             ParamUtil.getString(actionRequest, "addRoleIds"), 0L);
114         long[] removeRoleIds = StringUtil.split(
115             ParamUtil.getString(actionRequest, "removeRoleIds"), 0L);
116 
117         RoleServiceUtil.addUserRoles(user.getUserId(), addRoleIds);
118         RoleServiceUtil.unsetUserRoles(user.getUserId(), removeRoleIds);
119     }
120 
121 }