001    /**
002     * Copyright (c) 2000-present 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.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.model.Resource;
019    import com.liferay.portal.model.ResourceBlock;
020    import com.liferay.portal.model.ResourceConstants;
021    import com.liferay.portal.model.Role;
022    import com.liferay.portal.model.RoleConstants;
023    import com.liferay.portal.service.ResourceBlockLocalServiceUtil;
024    import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
025    
026    import java.util.List;
027    
028    /**
029     * @author Juan Fern??ndez
030     * @author Sergio Gonz??lez
031     */
032    public class ResourcePermissionUtil {
033    
034            public static void populateResourcePermissionActionIds(
035                            long groupId, Role role, Resource resource, List<String> actions,
036                            List<String> individualActions, List<String> groupActions,
037                            List<String> groupTemplateActions, List<String> companyActions)
038                    throws PortalException {
039    
040                    if (ResourceBlockLocalServiceUtil.isSupported(resource.getName())) {
041                            ResourceBlock resourceBlock =
042                                    ResourceBlockLocalServiceUtil.getResourceBlock(
043                                            resource.getName(), Long.valueOf(resource.getPrimKey()));
044    
045                            // Individual actions are not stored separately, so
046                            // individualActions will include group and company actions as well
047    
048                            individualActions.addAll(
049                                    ResourceBlockLocalServiceUtil.getPermissions(
050                                            resourceBlock, role.getRoleId()));
051                            groupActions.addAll(
052                                    ResourceBlockLocalServiceUtil.getGroupScopePermissions(
053                                            resourceBlock, role.getRoleId()));
054    
055                            // Resource blocks do not distinguish between company scope and
056                            // group template scope permissions, so the distinction must be
057                            // simulated here
058    
059                            if (role.getType() == RoleConstants.TYPE_REGULAR) {
060                                    companyActions.addAll(
061                                            ResourceBlockLocalServiceUtil.getCompanyScopePermissions(
062                                                    resourceBlock, role.getRoleId()));
063                            }
064                            else {
065                                    groupTemplateActions.addAll(
066                                            ResourceBlockLocalServiceUtil.getCompanyScopePermissions(
067                                                    resourceBlock, role.getRoleId()));
068                            }
069                    }
070                    else {
071                            individualActions.addAll(
072                                    ResourcePermissionLocalServiceUtil.
073                                            getAvailableResourcePermissionActionIds(
074                                                    resource.getCompanyId(), resource.getName(),
075                                                    resource.getScope(), resource.getPrimKey(),
076                                                    role.getRoleId(), actions));
077    
078                            groupActions.addAll(
079                                    ResourcePermissionLocalServiceUtil.
080                                            getAvailableResourcePermissionActionIds(
081                                                    resource.getCompanyId(), resource.getName(),
082                                                    ResourceConstants.SCOPE_GROUP, String.valueOf(groupId),
083                                                    role.getRoleId(), actions));
084    
085                            groupTemplateActions.addAll(
086                                    ResourcePermissionLocalServiceUtil.
087                                            getAvailableResourcePermissionActionIds(
088                                                    resource.getCompanyId(), resource.getName(),
089                                                    ResourceConstants.SCOPE_GROUP_TEMPLATE, "0",
090                                                    role.getRoleId(), actions));
091    
092                            companyActions.addAll(
093                                    ResourcePermissionLocalServiceUtil.
094                                            getAvailableResourcePermissionActionIds(
095                                                    resource.getCompanyId(), resource.getName(),
096                                                    ResourceConstants.SCOPE_COMPANY,
097                                                    String.valueOf(resource.getCompanyId()),
098                                                    role.getRoleId(), actions));
099                    }
100            }
101    
102    }