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.blogs.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
019    import com.liferay.portal.kernel.staging.permission.StagingPermissionUtil;
020    import com.liferay.portal.model.Group;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.security.permission.ResourcePermissionChecker;
024    import com.liferay.portal.service.GroupLocalServiceUtil;
025    import com.liferay.portal.util.PortletKeys;
026    
027    /**
028     * @author Jorge Ferrer
029     */
030    @OSGiBeanProperties(
031            property = {
032                    "model.class.name=com.liferay.portlet.blogs.model.BlogsEntry"
033            }
034    )
035    public class BlogsPermission implements ResourcePermissionChecker {
036    
037            public static final String RESOURCE_NAME = "com.liferay.portlet.blogs";
038    
039            public static void check(
040                            PermissionChecker permissionChecker, long groupId, String actionId)
041                    throws PortalException {
042    
043                    if (!contains(permissionChecker, groupId, actionId)) {
044                            throw new PrincipalException();
045                    }
046            }
047    
048            public static boolean contains(
049                            PermissionChecker permissionChecker, long classPK, String actionId)
050                    throws PortalException {
051    
052                    Group group = GroupLocalServiceUtil.fetchGroup(classPK);
053    
054                    if (group == null) {
055                            return BlogsEntryPermission.contains(
056                                    permissionChecker, classPK, actionId);
057                    }
058    
059                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
060                            permissionChecker, group.getGroupId(), RESOURCE_NAME,
061                            group.getGroupId(), PortletKeys.BLOGS, actionId);
062    
063                    if (hasPermission != null) {
064                            return hasPermission.booleanValue();
065                    }
066    
067                    return permissionChecker.hasPermission(
068                            classPK, RESOURCE_NAME, group.getGroupId(), actionId);
069            }
070    
071            @Override
072            public Boolean checkResource(
073                            PermissionChecker permissionChecker, long classPK, String actionId)
074                    throws PortalException {
075    
076                    return contains(permissionChecker, classPK, actionId);
077            }
078    
079    }