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.PermissionChecker;
021    import com.liferay.portlet.asset.model.AssetCategory;
022    import com.liferay.portlet.asset.model.AssetCategoryConstants;
023    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
024    
025    /**
026     * @author Eduardo Lundgren
027     */
028    public class AssetCategoryPermission {
029    
030            public static void check(
031                            PermissionChecker permissionChecker, AssetCategory category,
032                            String actionId)
033                    throws PortalException {
034    
035                    if (!contains(permissionChecker, category, actionId)) {
036                            throw new PrincipalException();
037                    }
038            }
039    
040            public static void check(
041                            PermissionChecker permissionChecker, long groupId, long categoryId,
042                            String actionId)
043                    throws PortalException, SystemException {
044    
045                    if (!contains(permissionChecker, groupId, categoryId, actionId)) {
046                            throw new PrincipalException();
047                    }
048            }
049    
050            public static void check(
051                            PermissionChecker permissionChecker, long categoryId,
052                            String actionId)
053                    throws PortalException, SystemException {
054    
055                    if (!contains(permissionChecker, categoryId, actionId)) {
056                            throw new PrincipalException();
057                    }
058            }
059    
060            public static boolean contains(
061                    PermissionChecker permissionChecker, AssetCategory category,
062                    String actionId) {
063    
064                    if (permissionChecker.hasOwnerPermission(
065                                    category.getCompanyId(), AssetCategory.class.getName(),
066                                    category.getCategoryId(), category.getUserId(), actionId)) {
067    
068                            return true;
069                    }
070    
071                    return permissionChecker.hasPermission(
072                            category.getGroupId(), AssetCategory.class.getName(),
073                            category.getCategoryId(), actionId);
074            }
075    
076            public static boolean contains(
077                            PermissionChecker permissionChecker, long groupId, long categoryId,
078                            String actionId)
079                    throws PortalException, SystemException {
080    
081                    if (categoryId == AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
082                            return AssetPermission.contains(
083                                    permissionChecker, groupId, actionId);
084                    }
085                    else {
086                            return contains(permissionChecker, categoryId, actionId);
087                    }
088            }
089    
090            public static boolean contains(
091                            PermissionChecker permissionChecker, long categoryId,
092                            String actionId)
093                    throws PortalException, SystemException {
094    
095                    AssetCategory category = AssetCategoryLocalServiceUtil.getCategory(
096                            categoryId);
097    
098                    return contains(permissionChecker, category, actionId);
099            }
100    
101    }