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