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