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