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.wiki.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.security.auth.PrincipalException;
021    import com.liferay.portal.security.permission.BaseModelPermissionChecker;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.util.PortletKeys;
024    import com.liferay.portlet.wiki.model.WikiNode;
025    import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    @OSGiBeanProperties(
031            property = {"model.class.name=com.liferay.portlet.wiki.model.WikiNode"}
032    )
033    public class WikiNodePermission implements BaseModelPermissionChecker {
034    
035            public static void check(
036                            PermissionChecker permissionChecker, long nodeId, String actionId)
037                    throws PortalException {
038    
039                    if (!contains(permissionChecker, nodeId, actionId)) {
040                            throw new PrincipalException();
041                    }
042            }
043    
044            public static void check(
045                            PermissionChecker permissionChecker, long groupId, String name,
046                            String actionId)
047                    throws PortalException {
048    
049                    if (!contains(permissionChecker, groupId, name, actionId)) {
050                            throw new PrincipalException();
051                    }
052            }
053    
054            public static void check(
055                            PermissionChecker permissionChecker, WikiNode node, String actionId)
056                    throws PortalException {
057    
058                    if (!contains(permissionChecker, node, actionId)) {
059                            throw new PrincipalException();
060                    }
061            }
062    
063            public static boolean contains(
064                            PermissionChecker permissionChecker, long nodeId, String actionId)
065                    throws PortalException {
066    
067                    WikiNode node = WikiNodeLocalServiceUtil.getNode(nodeId);
068    
069                    return contains(permissionChecker, node, actionId);
070            }
071    
072            public static boolean contains(
073                            PermissionChecker permissionChecker, long groupId, String name,
074                            String actionId)
075                    throws PortalException {
076    
077                    WikiNode node = WikiNodeLocalServiceUtil.getNode(groupId, name);
078    
079                    return contains(permissionChecker, node, actionId);
080            }
081    
082            public static boolean contains(
083                    PermissionChecker permissionChecker, WikiNode node, String actionId) {
084    
085                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
086                            permissionChecker, node.getGroupId(), WikiNode.class.getName(),
087                            node.getNodeId(), PortletKeys.WIKI, actionId);
088    
089                    if (hasPermission != null) {
090                            return hasPermission.booleanValue();
091                    }
092    
093                    if (permissionChecker.hasOwnerPermission(
094                                    node.getCompanyId(), WikiNode.class.getName(), node.getNodeId(),
095                                    node.getUserId(), actionId)) {
096    
097                            return true;
098                    }
099    
100                    return permissionChecker.hasPermission(
101                            node.getGroupId(), WikiNode.class.getName(), node.getNodeId(),
102                            actionId);
103            }
104    
105            @Override
106            public void checkBaseModel(
107                            PermissionChecker permissionChecker, long groupId, long primaryKey,
108                            String actionId)
109                    throws PortalException {
110    
111                    check(permissionChecker, primaryKey, actionId);
112            }
113    
114    }