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.MustHavePermission(
034                                    permissionChecker, AssetTag.class.getName(), tag.getTagId(),
035                                    actionId);
036                    }
037            }
038    
039            public static void check(
040                            PermissionChecker permissionChecker, long tagId, String actionId)
041                    throws PortalException {
042    
043                    if (!contains(permissionChecker, tagId, actionId)) {
044                            throw new PrincipalException.MustHavePermission(
045                                    permissionChecker, AssetTag.class.getName(), tagId, actionId);
046                    }
047            }
048    
049            public static boolean contains(
050                    PermissionChecker permissionChecker, AssetTag tag, String actionId) {
051    
052                    if (permissionChecker.hasOwnerPermission(
053                                    tag.getCompanyId(), AssetTag.class.getName(), tag.getTagId(),
054                                    tag.getUserId(), actionId)) {
055    
056                            return true;
057                    }
058    
059                    return permissionChecker.hasPermission(
060                            tag.getGroupId(), AssetTag.class.getName(), tag.getTagId(),
061                            actionId);
062            }
063    
064            public static boolean contains(
065                            PermissionChecker permissionChecker, long tagId, String actionId)
066                    throws PortalException {
067    
068                    AssetTag tag = AssetTagLocalServiceUtil.getTag(tagId);
069    
070                    return contains(permissionChecker, tag, actionId);
071            }
072    
073    }