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();
038                    }
039            }
040    
041            public static void check(
042                            PermissionChecker permissionChecker, long entryId, String actionId)
043                    throws PortalException {
044    
045                    if (!contains(permissionChecker, entryId, actionId)) {
046                            throw new PrincipalException();
047                    }
048            }
049    
050            public static void check(
051                            PermissionChecker permissionChecker, String className, long classPK,
052                            String actionId)
053                    throws PortalException {
054    
055                    if (!contains(permissionChecker, className, classPK, actionId)) {
056                            throw new PrincipalException();
057                    }
058            }
059    
060            public static boolean contains(
061                            PermissionChecker permissionChecker, AssetEntry entry,
062                            String actionId)
063                    throws PortalException {
064    
065                    String className = PortalUtil.getClassName(entry.getClassNameId());
066    
067                    AssetRendererFactory assetRendererFactory =
068                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
069                                    className);
070    
071                    try {
072                            return assetRendererFactory.hasPermission(
073                                    permissionChecker, entry.getClassPK(), actionId);
074                    }
075                    catch (Exception e) {
076                            throw new PrincipalException(e);
077                    }
078            }
079    
080            public static boolean contains(
081                            PermissionChecker permissionChecker, long entryId, String actionId)
082                    throws PortalException {
083    
084                    AssetEntry entry = AssetEntryLocalServiceUtil.getEntry(entryId);
085    
086                    return contains(permissionChecker, entry, actionId);
087            }
088    
089            public static boolean contains(
090                            PermissionChecker permissionChecker, String className, long classPK,
091                            String actionId)
092                    throws PortalException {
093    
094                    AssetEntry entry = AssetEntryLocalServiceUtil.getEntry(
095                            className, classPK);
096    
097                    return contains(permissionChecker, entry, actionId);
098            }
099    
100    }