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.kernel.workflow.permission.WorkflowPermissionUtil;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portlet.wiki.NoSuchPageException;
024    import com.liferay.portlet.wiki.model.WikiPage;
025    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class WikiPagePermission {
031    
032            public static void check(
033                            PermissionChecker permissionChecker, long resourcePrimKey,
034                            String actionId)
035                    throws PortalException, SystemException {
036    
037                    if (!contains(permissionChecker, resourcePrimKey, actionId)) {
038                            throw new PrincipalException();
039                    }
040            }
041    
042            public static void check(
043                            PermissionChecker permissionChecker, long nodeId, String title,
044                            double version, String actionId)
045                    throws PortalException, SystemException {
046    
047                    if (!contains(permissionChecker, nodeId, title, version, actionId)) {
048                            throw new PrincipalException();
049                    }
050            }
051    
052            public static void check(
053                            PermissionChecker permissionChecker, long nodeId, String title,
054                            String actionId)
055                    throws PortalException, SystemException {
056    
057                    if (!contains(permissionChecker, nodeId, title, actionId)) {
058                            throw new PrincipalException();
059                    }
060            }
061    
062            public static void check(
063                            PermissionChecker permissionChecker, WikiPage page, String actionId)
064                    throws PortalException {
065    
066                    if (!contains(permissionChecker, page, actionId)) {
067                            throw new PrincipalException();
068                    }
069            }
070    
071            public static boolean contains(
072                            PermissionChecker permissionChecker, long resourcePrimKey,
073                            String actionId)
074                    throws PortalException, SystemException {
075    
076                    WikiPage page = WikiPageLocalServiceUtil.getPage(
077                            resourcePrimKey, (Boolean)null);
078    
079                    return contains(permissionChecker, page, actionId);
080            }
081    
082            public static boolean contains(
083                            PermissionChecker permissionChecker, long nodeId, String title,
084                            double version, String actionId)
085                    throws PortalException, SystemException {
086    
087                    try {
088                            WikiPage page = WikiPageLocalServiceUtil.getPage(
089                                    nodeId, title, version);
090    
091                            return contains(permissionChecker, page, actionId);
092                    }
093                    catch (NoSuchPageException nspe) {
094                            return WikiNodePermission.contains(
095                                    permissionChecker, nodeId, ActionKeys.ADD_PAGE);
096                    }
097            }
098    
099            public static boolean contains(
100                            PermissionChecker permissionChecker, long nodeId, String title,
101                            String actionId)
102                    throws PortalException, SystemException {
103    
104                    try {
105                            WikiPage page = WikiPageLocalServiceUtil.getPage(
106                                    nodeId, title, null);
107    
108                            return contains(permissionChecker, page, actionId);
109                    }
110                    catch (NoSuchPageException nspe) {
111                            return WikiNodePermission.contains(
112                                    permissionChecker, nodeId, ActionKeys.ADD_PAGE);
113                    }
114            }
115    
116            public static boolean contains(
117                    PermissionChecker permissionChecker, WikiPage page, String actionId) {
118    
119                    if (page.isPending()) {
120                            Boolean hasPermission = WorkflowPermissionUtil.hasPermission(
121                                    permissionChecker, page.getGroupId(), WikiPage.class.getName(),
122                                    page.getResourcePrimKey(), actionId);
123    
124                            if (hasPermission != null) {
125                                    return hasPermission.booleanValue();
126                            }
127                    }
128    
129                    if (page.isDraft() && actionId.equals(ActionKeys.DELETE) &&
130                            (page.getStatusByUserId() == permissionChecker.getUserId())) {
131    
132                            return true;
133                    }
134    
135                    if (permissionChecker.hasOwnerPermission(
136                                    page.getCompanyId(), WikiPage.class.getName(), page.getPageId(),
137                                    page.getUserId(), actionId)) {
138    
139                            return true;
140                    }
141    
142                    return permissionChecker.hasPermission(
143                            page.getGroupId(), WikiPage.class.getName(),
144                            page.getResourcePrimKey(), actionId);
145            }
146    
147    }