001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 (actionId.equals(ActionKeys.VIEW)) {
121                            WikiPage redirectPage = page.getRedirectPage();
122    
123                            if (redirectPage != null) {
124                                    page = redirectPage;
125                            }
126                    }
127    
128                    if (page.isPending()) {
129                            Boolean hasPermission = WorkflowPermissionUtil.hasPermission(
130                                    permissionChecker, page.getGroupId(), WikiPage.class.getName(),
131                                    page.getResourcePrimKey(), actionId);
132    
133                            if (hasPermission != null) {
134                                    return hasPermission.booleanValue();
135                            }
136                    }
137    
138                    if (page.isDraft() && actionId.equals(ActionKeys.DELETE) &&
139                            (page.getStatusByUserId() == permissionChecker.getUserId())) {
140    
141                            return true;
142                    }
143    
144                    if (permissionChecker.hasOwnerPermission(
145                                    page.getCompanyId(), WikiPage.class.getName(), page.getPageId(),
146                                    page.getUserId(), actionId)) {
147    
148                            return true;
149                    }
150    
151                    if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
152                            WikiPage parentPage = page.getParentPage();
153    
154                            if ((parentPage != null) &&
155                                    !contains(permissionChecker, parentPage, ActionKeys.VIEW)) {
156    
157                                    return false;
158                            }
159                    }
160    
161                    return permissionChecker.hasPermission(
162                            page.getGroupId(), WikiPage.class.getName(),
163                            page.getResourcePrimKey(), actionId);
164            }
165    
166    }