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.PermissionChecker;
020    import com.liferay.portlet.asset.model.AssetTag;
021    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
022    
023    /**
024     * @author Eduardo Lundgren
025     */
026    public class AssetTagPermission {
027    
028            public static void check(
029                            PermissionChecker permissionChecker, AssetTag tag, String actionId)
030                    throws PortalException {
031    
032                    if (!contains(permissionChecker, tag, actionId)) {
033                            throw new PrincipalException();
034                    }
035            }
036    
037            public static void check(
038                            PermissionChecker permissionChecker, long tagId, String actionId)
039                    throws PortalException {
040    
041                    if (!contains(permissionChecker, tagId, actionId)) {
042                            throw new PrincipalException();
043                    }
044            }
045    
046            public static boolean contains(
047                    PermissionChecker permissionChecker, AssetTag tag, String actionId) {
048    
049                    if (permissionChecker.hasOwnerPermission(
050                                    tag.getCompanyId(), AssetTag.class.getName(), tag.getTagId(),
051                                    tag.getUserId(), actionId)) {
052    
053                            return true;
054                    }
055    
056                    return permissionChecker.hasPermission(
057                            tag.getGroupId(), AssetTag.class.getName(), tag.getTagId(),
058                            actionId);
059            }
060    
061            public static boolean contains(
062                            PermissionChecker permissionChecker, long tagId, String actionId)
063                    throws PortalException {
064    
065                    AssetTag tag = AssetTagLocalServiceUtil.getTag(tagId);
066    
067                    return contains(permissionChecker, tag, actionId);
068            }
069    
070    }