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