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.portal.util.PortalUtil;
021    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
022    import com.liferay.portlet.asset.model.AssetEntry;
023    import com.liferay.portlet.asset.model.AssetRendererFactory;
024    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
025    
026    /**
027     * @author Samuel Kong
028     */
029    public class AssetEntryPermission {
030    
031            public static void check(
032                            PermissionChecker permissionChecker, AssetEntry entry,
033                            String actionId)
034                    throws PortalException {
035    
036                    if (!contains(permissionChecker, entry, actionId)) {
037                            throw new PrincipalException.MustHavePermission(
038                                    permissionChecker,
039                                    PortalUtil.getClassName(entry.getClassNameId()),
040                                    entry.getClassPK(), actionId);
041                    }
042            }
043    
044            public static void check(
045                            PermissionChecker permissionChecker, long entryId, String actionId)
046                    throws PortalException {
047    
048                    AssetEntry entry = AssetEntryLocalServiceUtil.getEntry(entryId);
049    
050                    check(permissionChecker, entry, actionId);
051            }
052    
053            public static void check(
054                            PermissionChecker permissionChecker, String className, long classPK,
055                            String actionId)
056                    throws PortalException {
057    
058                    if (!contains(permissionChecker, className, classPK, actionId)) {
059                            throw new PrincipalException.MustHavePermission(
060                                    permissionChecker, className, classPK, actionId);
061                    }
062            }
063    
064            public static boolean contains(
065                            PermissionChecker permissionChecker, AssetEntry entry,
066                            String actionId)
067                    throws PortalException {
068    
069                    String className = PortalUtil.getClassName(entry.getClassNameId());
070    
071                    AssetRendererFactory<?> assetRendererFactory =
072                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
073                                    className);
074    
075                    try {
076                            return assetRendererFactory.hasPermission(
077                                    permissionChecker, entry.getClassPK(), actionId);
078                    }
079                    catch (Exception e) {
080                            throw new PrincipalException.MustHavePermission(
081                                    permissionChecker, className, entry.getClassPK(), actionId);
082                    }
083            }
084    
085            public static boolean contains(
086                            PermissionChecker permissionChecker, long entryId, String actionId)
087                    throws PortalException {
088    
089                    AssetEntry entry = AssetEntryLocalServiceUtil.getEntry(entryId);
090    
091                    return contains(permissionChecker, entry, actionId);
092            }
093    
094            public static boolean contains(
095                            PermissionChecker permissionChecker, String className, long classPK,
096                            String actionId)
097                    throws PortalException {
098    
099                    AssetEntry entry = AssetEntryLocalServiceUtil.getEntry(
100                            className, classPK);
101    
102                    return contains(permissionChecker, entry, actionId);
103            }
104    
105    }