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