001    /**
002     * Copyright (c) 2000-2010 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.imagegallery.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.staging.permission.StagingPermissionUtil;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.util.PortletKeys;
024    import com.liferay.portal.util.PropsValues;
025    import com.liferay.portlet.imagegallery.model.IGFolder;
026    import com.liferay.portlet.imagegallery.model.IGFolderConstants;
027    import com.liferay.portlet.imagegallery.model.IGImage;
028    import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class IGImagePermission {
034    
035            public static void check(
036                            PermissionChecker permissionChecker, IGImage image, String actionId)
037                    throws PortalException, SystemException {
038    
039                    if (!contains(permissionChecker, image, actionId)) {
040                            throw new PrincipalException();
041                    }
042            }
043    
044            public static void check(
045                            PermissionChecker permissionChecker, long imageId, String actionId)
046                    throws PortalException, SystemException {
047    
048                    if (!contains(permissionChecker, imageId, actionId)) {
049                            throw new PrincipalException();
050                    }
051            }
052    
053            public static boolean contains(
054                            PermissionChecker permissionChecker, IGImage image, String actionId)
055                    throws PortalException, SystemException {
056    
057                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
058                            permissionChecker, image.getGroupId(), IGImage.class.getName(),
059                            image.getImageId(), PortletKeys.IMAGE_GALLERY, actionId);
060    
061                    if (hasPermission != null) {
062                            return hasPermission.booleanValue();
063                    }
064    
065                    if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
066                            if (image.getFolderId() !=
067                                            IGFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
068    
069                                    IGFolder folder = image.getFolder();
070    
071                                    if (!IGFolderPermission.contains(
072                                                    permissionChecker, folder, ActionKeys.ACCESS) &&
073                                            !IGFolderPermission.contains(
074                                                    permissionChecker, folder, ActionKeys.VIEW)) {
075    
076                                            return false;
077                                    }
078                            }
079                    }
080    
081                    if (permissionChecker.hasOwnerPermission(
082                                    image.getCompanyId(), IGImage.class.getName(),
083                                    image.getImageId(), image.getUserId(), actionId)) {
084    
085                            return true;
086                    }
087    
088                    return permissionChecker.hasPermission(
089                            image.getGroupId(), IGImage.class.getName(), image.getImageId(),
090                            actionId);
091            }
092    
093            public static boolean contains(
094                            PermissionChecker permissionChecker, long imageId, String actionId)
095                    throws PortalException, SystemException {
096    
097                    IGImage image = IGImageLocalServiceUtil.getImage(imageId);
098    
099                    return contains(permissionChecker, image, actionId);
100            }
101    
102    }