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.messageboards.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.messageboards.model.MBCategory"
033            }
034    )
035    public class MBPermission implements ResourcePermissionChecker {
036    
037            public static final String RESOURCE_NAME =
038                    "com.liferay.portlet.messageboards";
039    
040            public static void check(
041                            PermissionChecker permissionChecker, long groupId, String actionId)
042                    throws PortalException {
043    
044                    if (!contains(permissionChecker, groupId, actionId)) {
045                            throw new PrincipalException();
046                    }
047            }
048    
049            public static boolean contains(
050                            PermissionChecker permissionChecker, long classPK, String actionId)
051                    throws PortalException {
052    
053                    Group group = GroupLocalServiceUtil.fetchGroup(classPK);
054    
055                    if (group == null) {
056                            return MBCategoryPermission.contains(
057                                    permissionChecker, classPK, actionId);
058                    }
059    
060                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
061                            permissionChecker, group.getGroupId(), RESOURCE_NAME,
062                            group.getGroupId(), PortletKeys.MESSAGE_BOARDS, actionId);
063    
064                    if (hasPermission != null) {
065                            return hasPermission.booleanValue();
066                    }
067    
068                    return permissionChecker.hasPermission(
069                            classPK, RESOURCE_NAME, group.getGroupId(), actionId);
070            }
071    
072            @Override
073            public Boolean checkResource(
074                            PermissionChecker permissionChecker, long classPK, String actionId)
075                    throws PortalException {
076    
077                    return contains(permissionChecker, classPK, actionId);
078            }
079    
080    }