001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.portal.util.PortalUtil;
022    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
023    import com.liferay.portlet.asset.model.AssetEntry;
024    import com.liferay.portlet.asset.model.AssetRendererFactory;
025    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
026    
027    /**
028     * @author Samuel Kong
029     */
030    public class AssetEntryPermission {
031    
032            public static void check(
033                            PermissionChecker permissionChecker, AssetEntry entry,
034                            String actionId)
035                    throws PortalException {
036    
037                    if (!contains(permissionChecker, entry, actionId)) {
038                            throw new PrincipalException();
039                    }
040            }
041    
042            public static void check(
043                            PermissionChecker permissionChecker, long entryId, String actionId)
044                    throws PortalException, SystemException {
045    
046                    if (!contains(permissionChecker, entryId, actionId)) {
047                            throw new PrincipalException();
048                    }
049            }
050    
051            public static void check(
052                            PermissionChecker permissionChecker, String className, long classPK,
053                            String actionId)
054                    throws PortalException, SystemException {
055    
056                    if (!contains(permissionChecker, className, classPK, actionId)) {
057                            throw new PrincipalException();
058                    }
059            }
060    
061            public static boolean contains(
062                            PermissionChecker permissionChecker, AssetEntry entry,
063                            String actionId)
064                    throws PortalException {
065    
066                    String className = PortalUtil.getClassName(entry.getClassNameId());
067    
068                    AssetRendererFactory assetRendererFactory =
069                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
070                                    className);
071    
072                    try {
073                            return assetRendererFactory.hasPermission(
074                                    permissionChecker, entry.getClassPK(), actionId);
075                    }
076                    catch (Exception e) {
077                            throw new PrincipalException(e);
078                    }
079            }
080    
081            public static boolean contains(
082                            PermissionChecker permissionChecker, long entryId, String actionId)
083                    throws PortalException, SystemException {
084    
085                    AssetEntry entry = AssetEntryLocalServiceUtil.getEntry(entryId);
086    
087                    return contains(permissionChecker, entry, actionId);
088            }
089    
090            public static boolean contains(
091                            PermissionChecker permissionChecker, String className, long classPK,
092                            String actionId)
093                    throws PortalException, SystemException {
094    
095                    AssetEntry entry = AssetEntryLocalServiceUtil.getEntry(
096                            className, classPK);
097    
098                    return contains(permissionChecker, entry, actionId);
099            }
100    
101    }