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.security.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.SetUtil;
020    import com.liferay.portal.kernel.util.WebKeys;
021    import com.liferay.portal.model.ResourceConstants;
022    import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
023    import com.liferay.portal.service.ResourcePermissionServiceUtil;
024    import com.liferay.portal.theme.ThemeDisplay;
025    
026    import java.util.ArrayList;
027    import java.util.HashSet;
028    import java.util.List;
029    import java.util.Set;
030    
031    import javax.portlet.ActionRequest;
032    
033    /**
034     * @author Hugo Huijser
035     */
036    public abstract class BasePermissionPropagator implements PermissionPropagator {
037    
038            protected Set<String> getActionIds(String className) {
039                    List<String> actionIds = ResourceActionsUtil.getModelResourceActions(
040                            className);
041    
042                    return SetUtil.fromCollection(actionIds);
043            }
044    
045            protected Set<String> getAvailableActionIds(
046                            long companyId, String className, long primKey, long roleId,
047                            Set<String> actionIds)
048                    throws PortalException, SystemException {
049    
050                    List<String> availableActionIds =
051                            ResourcePermissionLocalServiceUtil.
052                                    getAvailableResourcePermissionActionIds(
053                                            companyId, className, ResourceConstants.SCOPE_INDIVIDUAL,
054                                            String.valueOf(primKey), roleId, actionIds);
055    
056                    return SetUtil.fromCollection(availableActionIds);
057            }
058    
059            protected void propagateRolePermissions(
060                            ActionRequest actionRequest, long roleId, String parentClassName,
061                            long parentPrimKey, String childClassName, long childPrimKey)
062                    throws PortalException, SystemException {
063    
064                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
065                            WebKeys.THEME_DISPLAY);
066    
067                    Set<String> parentActionIds = getActionIds(parentClassName);
068                    Set<String> childActionIds = getActionIds(childClassName);
069    
070                    Set<String> parentAndChildCommonActionIds = new HashSet<String>();
071    
072                    for (String actionId : childActionIds) {
073                            if (parentActionIds.contains(actionId)) {
074                                    parentAndChildCommonActionIds.add(actionId);
075                            }
076                    }
077    
078                    Set<String> parentAvailableActionIds = getAvailableActionIds(
079                            themeDisplay.getCompanyId(), parentClassName, parentPrimKey, roleId,
080                            parentActionIds);
081                    Set<String> childAvailableActionIds = getAvailableActionIds(
082                            themeDisplay.getCompanyId(), childClassName, childPrimKey, roleId,
083                            childActionIds);
084    
085                    List<String> actionIds = new ArrayList<String>();
086    
087                    for (String actionId : parentAndChildCommonActionIds) {
088                            if (parentAvailableActionIds.contains(actionId)) {
089                                    actionIds.add(actionId);
090                            }
091                    }
092    
093                    for (String actionId : childAvailableActionIds) {
094                            if (!parentAndChildCommonActionIds.contains(actionId)) {
095                                    actionIds.add(actionId);
096                            }
097                    }
098    
099                    ResourcePermissionServiceUtil.setIndividualResourcePermissions(
100                            themeDisplay.getScopeGroupId(), themeDisplay.getCompanyId(),
101                            childClassName, String.valueOf(childPrimKey), roleId,
102                            actionIds.toArray(new String[actionIds.size()]));
103            }
104    
105    }