1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.convert.action;
16  
17  import com.liferay.portal.NoSuchRoleException;
18  import com.liferay.portal.RolePermissionsException;
19  import com.liferay.portal.kernel.servlet.SessionErrors;
20  import com.liferay.portal.kernel.util.Constants;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.security.auth.PrincipalException;
24  import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
25  import com.liferay.portal.struts.PortletAction;
26  
27  import javax.portlet.ActionRequest;
28  import javax.portlet.ActionResponse;
29  import javax.portlet.PortletConfig;
30  import javax.portlet.RenderRequest;
31  import javax.portlet.RenderResponse;
32  
33  import org.apache.struts.action.ActionForm;
34  import org.apache.struts.action.ActionForward;
35  import org.apache.struts.action.ActionMapping;
36  
37  /**
38   * <a href="EditPermissionsAction.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Alexander Chow
41   */
42  public class EditPermissionsAction extends PortletAction {
43  
44      public void processAction(
45              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
46              ActionRequest actionRequest, ActionResponse actionResponse)
47          throws Exception {
48  
49          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
50  
51          try {
52              if (cmd.equals("merge")) {
53                  merge(actionRequest, actionResponse);
54              }
55              else if (cmd.equals("reassign")) {
56                  reassign(actionRequest, actionResponse);
57              }
58  
59              sendRedirect(actionRequest, actionResponse);
60          }
61          catch (Exception e) {
62              if (e instanceof NoSuchRoleException ||
63                  e instanceof PrincipalException ||
64                  e instanceof RolePermissionsException) {
65  
66                  SessionErrors.add(actionRequest, e.getClass().getName());
67  
68                  setForward(actionRequest, "portlet.admin.error");
69              }
70              else {
71                  throw e;
72              }
73          }
74      }
75  
76      public ActionForward render(
77              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
78              RenderRequest renderRequest, RenderResponse renderResponse)
79          throws Exception {
80  
81          return mapping.findForward(
82              getForward(renderRequest, "portlet.admin.edit_permissions"));
83      }
84  
85      protected void merge(
86              ActionRequest actionRequest, ActionResponse actionResponse)
87          throws Exception {
88  
89          long[] roleIds = StringUtil.split(
90              ParamUtil.getString(actionRequest, "roleIds"), 0L);
91  
92          long toRoleId = roleIds[0];
93  
94          for (int i = 1; i < roleIds.length; i++) {
95              long fromRoleId = roleIds[i];
96  
97              ResourcePermissionLocalServiceUtil.mergePermissions(
98                  fromRoleId, toRoleId);
99          }
100     }
101 
102     protected void reassign(
103             ActionRequest actionRequest, ActionResponse actionResponse)
104         throws Exception {
105 
106         long resourcePermissionId = ParamUtil.getLong(
107             actionRequest, "resourcePermissionId");
108         long toRoleId = ParamUtil.getLong(actionRequest, "toRoleId");
109 
110         ResourcePermissionLocalServiceUtil.reassignPermissions(
111             resourcePermissionId, toRoleId);
112     }
113 
114 }