001    /**
002     * Copyright (c) 2000-2013 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.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    }