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