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