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.portlet.asset.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.security.auth.PrincipalException;
019    import com.liferay.portal.security.permission.ActionKeys;
020    import com.liferay.portal.security.permission.PermissionChecker;
021    import com.liferay.portal.util.PropsValues;
022    import com.liferay.portlet.asset.model.AssetCategory;
023    import com.liferay.portlet.asset.model.AssetCategoryConstants;
024    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
025    
026    /**
027     * @author Eduardo Lundgren
028     */
029    public class AssetCategoryPermission {
030    
031            public static void check(
032                            PermissionChecker permissionChecker, AssetCategory category,
033                            String actionId)
034                    throws PortalException {
035    
036                    if (!contains(permissionChecker, category, actionId)) {
037                            throw new PrincipalException();
038                    }
039            }
040    
041            public static void check(
042                            PermissionChecker permissionChecker, long groupId, long categoryId,
043                            String actionId)
044                    throws PortalException {
045    
046                    if (!contains(permissionChecker, groupId, categoryId, actionId)) {
047                            throw new PrincipalException();
048                    }
049            }
050    
051            public static void check(
052                            PermissionChecker permissionChecker, long categoryId,
053                            String actionId)
054                    throws PortalException {
055    
056                    if (!contains(permissionChecker, categoryId, actionId)) {
057                            throw new PrincipalException();
058                    }
059            }
060    
061            public static boolean contains(
062                            PermissionChecker permissionChecker, AssetCategory category,
063                            String actionId)
064                    throws PortalException {
065    
066                    if (actionId.equals(ActionKeys.VIEW) &&
067                            !AssetVocabularyPermission.contains(
068                                    permissionChecker, category.getVocabularyId(),
069                                    ActionKeys.VIEW)) {
070    
071                            return false;
072                    }
073    
074                    if (actionId.equals(ActionKeys.VIEW) &&
075                            PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
076    
077                            long categoryId = category.getCategoryId();
078    
079                            while (categoryId !=
080                                                    AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
081    
082                                    category = AssetCategoryLocalServiceUtil.getCategory(
083                                            categoryId);
084    
085                                    if (!_hasPermission(permissionChecker, category, actionId)) {
086                                            return false;
087                                    }
088    
089                                    categoryId = category.getParentCategoryId();
090                            }
091    
092                            return AssetVocabularyPermission.contains(
093                                    permissionChecker, category.getVocabularyId(), actionId);
094                    }
095    
096                    return _hasPermission(permissionChecker, category, actionId);
097            }
098    
099            public static boolean contains(
100                            PermissionChecker permissionChecker, long groupId, long categoryId,
101                            String actionId)
102                    throws PortalException {
103    
104                    if (categoryId == AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
105                            return AssetPermission.contains(
106                                    permissionChecker, groupId, actionId);
107                    }
108                    else {
109                            return contains(permissionChecker, categoryId, actionId);
110                    }
111            }
112    
113            public static boolean contains(
114                            PermissionChecker permissionChecker, long categoryId,
115                            String actionId)
116                    throws PortalException {
117    
118                    AssetCategory category = AssetCategoryLocalServiceUtil.getCategory(
119                            categoryId);
120    
121                    return contains(permissionChecker, category, actionId);
122            }
123    
124            private static boolean _hasPermission(
125                    PermissionChecker permissionChecker, AssetCategory category,
126                    String actionId) {
127    
128                    if (permissionChecker.hasOwnerPermission(
129                                    category.getCompanyId(), AssetCategory.class.getName(),
130                                    category.getCategoryId(), category.getUserId(), actionId) ||
131                            permissionChecker.hasPermission(
132                                    category.getGroupId(), AssetCategory.class.getName(),
133                                    category.getCategoryId(), actionId)) {
134    
135                            return true;
136                    }
137    
138                    return false;
139            }
140    
141    }