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.portal.convert.action;
24  
25  import com.liferay.portal.NoSuchRoleException;
26  import com.liferay.portal.RolePermissionsException;
27  import com.liferay.portal.kernel.servlet.SessionErrors;
28  import com.liferay.portal.kernel.util.Constants;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
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="EditPermissionsAction.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Alexander Chow
49   *
50   */
51  public class EditPermissionsAction 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("merge")) {
62                  merge(actionRequest, actionResponse);
63              }
64              else if (cmd.equals("reassign")) {
65                  reassign(actionRequest, actionResponse);
66              }
67  
68              sendRedirect(actionRequest, actionResponse);
69          }
70          catch (Exception e) {
71              if (e instanceof NoSuchRoleException ||
72                  e instanceof PrincipalException ||
73                  e instanceof RolePermissionsException) {
74  
75                  SessionErrors.add(actionRequest, e.getClass().getName());
76  
77                  setForward(actionRequest, "portlet.admin.error");
78              }
79              else {
80                  throw e;
81              }
82          }
83      }
84  
85      public ActionForward render(
86              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
87              RenderRequest renderRequest, RenderResponse renderResponse)
88          throws Exception {
89  
90          return mapping.findForward(
91              getForward(renderRequest, "portlet.admin.edit_permissions"));
92      }
93  
94      protected void merge(
95              ActionRequest actionRequest, ActionResponse actionResponse)
96          throws Exception {
97  
98          long[] roleIds = StringUtil.split(
99              ParamUtil.getString(actionRequest, "roleIds"), 0L);
100 
101         long toRoleId = roleIds[0];
102 
103         for (int i = 1; i < roleIds.length; i++) {
104             long fromRoleId = roleIds[i];
105 
106             ResourcePermissionLocalServiceUtil.mergePermissions(
107                 fromRoleId, toRoleId);
108         }
109     }
110 
111     protected void reassign(
112             ActionRequest actionRequest, ActionResponse actionResponse)
113         throws Exception {
114 
115         long resourcePermissionId = ParamUtil.getLong(
116             actionRequest, "resourcePermissionId");
117         long toRoleId = ParamUtil.getLong(actionRequest, "toRoleId");
118 
119         ResourcePermissionLocalServiceUtil.reassignPermissions(
120             resourcePermissionId, toRoleId);
121     }
122 
123 }