001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.convert.action;
016    
017    import com.liferay.portal.NoSuchRoleException;
018    import com.liferay.portal.RolePermissionsException;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.security.auth.PrincipalException;
024    import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
025    import com.liferay.portal.struts.PortletAction;
026    
027    import javax.portlet.ActionRequest;
028    import javax.portlet.ActionResponse;
029    import javax.portlet.PortletConfig;
030    import javax.portlet.RenderRequest;
031    import javax.portlet.RenderResponse;
032    
033    import org.apache.struts.action.ActionForm;
034    import org.apache.struts.action.ActionForward;
035    import org.apache.struts.action.ActionMapping;
036    
037    /**
038     * @author Alexander Chow
039     */
040    public class EditPermissionsAction extends PortletAction {
041    
042            @Override
043            public void processAction(
044                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
045                            ActionRequest actionRequest, ActionResponse actionResponse)
046                    throws Exception {
047    
048                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
049    
050                    try {
051                            if (cmd.equals("merge")) {
052                                    merge(actionRequest, actionResponse);
053                            }
054                            else if (cmd.equals("reassign")) {
055                                    reassign(actionRequest, actionResponse);
056                            }
057    
058                            sendRedirect(actionRequest, actionResponse);
059                    }
060                    catch (Exception e) {
061                            if (e instanceof NoSuchRoleException ||
062                                    e instanceof PrincipalException ||
063                                    e instanceof RolePermissionsException) {
064    
065                                    SessionErrors.add(actionRequest, e.getClass().getName());
066    
067                                    setForward(actionRequest, "portlet.admin.error");
068                            }
069                            else {
070                                    throw e;
071                            }
072                    }
073            }
074    
075            @Override
076            public ActionForward render(
077                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
078                            RenderRequest renderRequest, RenderResponse renderResponse)
079                    throws Exception {
080    
081                    return mapping.findForward(
082                            getForward(renderRequest, "portlet.admin.edit_permissions"));
083            }
084    
085            protected void merge(
086                            ActionRequest actionRequest, ActionResponse actionResponse)
087                    throws Exception {
088    
089                    long[] roleIds = StringUtil.split(
090                            ParamUtil.getString(actionRequest, "roleIds"), 0L);
091    
092                    long toRoleId = roleIds[0];
093    
094                    for (int i = 1; i < roleIds.length; i++) {
095                            long fromRoleId = roleIds[i];
096    
097                            ResourcePermissionLocalServiceUtil.mergePermissions(
098                                    fromRoleId, toRoleId);
099                    }
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    }