001    /**
002     * Copyright (c) 2000-2013 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 actionMapping, ActionForm actionForm,
045                            PortletConfig portletConfig, ActionRequest actionRequest,
046                            ActionResponse actionResponse)
047                    throws Exception {
048    
049                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
050    
051                    try {
052                            if (cmd.equals("merge")) {
053                                    merge(actionRequest, actionResponse);
054                            }
055                            else if (cmd.equals("reassign")) {
056                                    reassign(actionRequest, actionResponse);
057                            }
058    
059                            sendRedirect(actionRequest, actionResponse);
060                    }
061                    catch (Exception e) {
062                            if (e instanceof NoSuchRoleException ||
063                                    e instanceof PrincipalException ||
064                                    e instanceof RolePermissionsException) {
065    
066                                    SessionErrors.add(actionRequest, e.getClass().getName());
067    
068                                    setForward(actionRequest, "portlet.admin.error");
069                            }
070                            else {
071                                    throw e;
072                            }
073                    }
074            }
075    
076            @Override
077            public ActionForward render(
078                            ActionMapping actionMapping, ActionForm actionForm,
079                            PortletConfig portletConfig, RenderRequest renderRequest,
080                            RenderResponse renderResponse)
081                    throws Exception {
082    
083                    return actionMapping.findForward(
084                            getForward(renderRequest, "portlet.admin.edit_permissions"));
085            }
086    
087            protected void merge(
088                            ActionRequest actionRequest, ActionResponse actionResponse)
089                    throws Exception {
090    
091                    long[] roleIds = StringUtil.split(
092                            ParamUtil.getString(actionRequest, "roleIds"), 0L);
093    
094                    long toRoleId = roleIds[0];
095    
096                    for (int i = 1; i < roleIds.length; i++) {
097                            long fromRoleId = roleIds[i];
098    
099                            ResourcePermissionLocalServiceUtil.mergePermissions(
100                                    fromRoleId, toRoleId);
101                    }
102            }
103    
104            protected void reassign(
105                            ActionRequest actionRequest, ActionResponse actionResponse)
106                    throws Exception {
107    
108                    long resourcePermissionId = ParamUtil.getLong(
109                            actionRequest, "resourcePermissionId");
110                    long toRoleId = ParamUtil.getLong(actionRequest, "toRoleId");
111    
112                    ResourcePermissionLocalServiceUtil.reassignPermissions(
113                            resourcePermissionId, toRoleId);
114            }
115    
116    }