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