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.MustHavePermission(
038                                    permissionChecker, AssetCategory.class.getName(),
039                                    category.getCategoryId(), actionId);
040                    }
041            }
042    
043            public static void check(
044                            PermissionChecker permissionChecker, long groupId, long categoryId,
045                            String actionId)
046                    throws PortalException {
047    
048                    if (!contains(permissionChecker, groupId, categoryId, actionId)) {
049                            throw new PrincipalException.MustHavePermission(
050                                    permissionChecker, AssetCategory.class.getName(), categoryId,
051                                    actionId);
052                    }
053            }
054    
055            public static void check(
056                            PermissionChecker permissionChecker, long categoryId,
057                            String actionId)
058                    throws PortalException {
059    
060                    if (!contains(permissionChecker, categoryId, actionId)) {
061                            throw new PrincipalException.MustHavePermission(
062                                    permissionChecker, AssetCategory.class.getName(), categoryId,
063                                    actionId);
064                    }
065            }
066    
067            public static boolean contains(
068                            PermissionChecker permissionChecker, AssetCategory category,
069                            String actionId)
070                    throws PortalException {
071    
072                    if (actionId.equals(ActionKeys.VIEW) &&
073                            !AssetVocabularyPermission.contains(
074                                    permissionChecker, category.getVocabularyId(),
075                                    ActionKeys.VIEW)) {
076    
077                            return false;
078                    }
079    
080                    if (actionId.equals(ActionKeys.VIEW) &&
081                            PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
082    
083                            long categoryId = category.getCategoryId();
084    
085                            while (categoryId !=
086                                                    AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
087    
088                                    category = AssetCategoryLocalServiceUtil.getCategory(
089                                            categoryId);
090    
091                                    if (!_hasPermission(permissionChecker, category, actionId)) {
092                                            return false;
093                                    }
094    
095                                    categoryId = category.getParentCategoryId();
096                            }
097    
098                            return AssetVocabularyPermission.contains(
099                                    permissionChecker, category.getVocabularyId(), actionId);
100                    }
101    
102                    return _hasPermission(permissionChecker, category, actionId);
103            }
104    
105            public static boolean contains(
106                            PermissionChecker permissionChecker, long groupId, long categoryId,
107                            String actionId)
108                    throws PortalException {
109    
110                    if (categoryId == AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
111                            return AssetPermission.contains(
112                                    permissionChecker, groupId, actionId);
113                    }
114                    else {
115                            return contains(permissionChecker, categoryId, actionId);
116                    }
117            }
118    
119            public static boolean contains(
120                            PermissionChecker permissionChecker, long categoryId,
121                            String actionId)
122                    throws PortalException {
123    
124                    AssetCategory category = AssetCategoryLocalServiceUtil.getCategory(
125                            categoryId);
126    
127                    return contains(permissionChecker, category, actionId);
128            }
129    
130            private static boolean _hasPermission(
131                    PermissionChecker permissionChecker, AssetCategory category,
132                    String actionId) {
133    
134                    if (permissionChecker.hasOwnerPermission(
135                                    category.getCompanyId(), AssetCategory.class.getName(),
136                                    category.getCategoryId(), category.getUserId(), actionId) ||
137                            permissionChecker.hasPermission(
138                                    category.getGroupId(), AssetCategory.class.getName(),
139                                    category.getCategoryId(), actionId)) {
140    
141                            return true;
142                    }
143    
144                    return false;
145            }
146    
147    }