001    /**
002     * Copyright (c) 2000-2012 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.NoSuchPageResourceException;
026    import com.liferay.portlet.wiki.model.WikiNode;
027    import com.liferay.portlet.wiki.model.WikiPage;
028    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class WikiPagePermission {
034    
035            public static void check(
036                            PermissionChecker permissionChecker, long resourcePrimKey,
037                            String actionId)
038                    throws PortalException, SystemException {
039    
040                    if (!contains(permissionChecker, resourcePrimKey, actionId)) {
041                            throw new PrincipalException();
042                    }
043            }
044    
045            public static void check(
046                            PermissionChecker permissionChecker, long nodeId, String title,
047                            double version, String actionId)
048                    throws PortalException, SystemException {
049    
050                    if (!contains(permissionChecker, nodeId, title, version, actionId)) {
051                            throw new PrincipalException();
052                    }
053            }
054    
055            public static void check(
056                            PermissionChecker permissionChecker, long nodeId, String title,
057                            String actionId)
058                    throws PortalException, SystemException {
059    
060                    if (!contains(permissionChecker, nodeId, title, actionId)) {
061                            throw new PrincipalException();
062                    }
063            }
064    
065            public static void check(
066                            PermissionChecker permissionChecker, WikiPage page, String actionId)
067                    throws PortalException {
068    
069                    if (!contains(permissionChecker, page, actionId)) {
070                            throw new PrincipalException();
071                    }
072            }
073    
074            public static boolean contains(
075                            PermissionChecker permissionChecker, long resourcePrimKey,
076                            String actionId)
077                    throws PortalException, SystemException {
078    
079                    try {
080                            WikiPage page = WikiPageLocalServiceUtil.getPage(
081                                    resourcePrimKey, (Boolean)null);
082    
083                            return contains(permissionChecker, page, actionId);
084                    }
085                    catch (NoSuchPageResourceException nspre) {
086                            return false;
087                    }
088            }
089    
090            public static boolean contains(
091                            PermissionChecker permissionChecker, long nodeId, String title,
092                            double version, String actionId)
093                    throws PortalException, SystemException {
094    
095                    try {
096                            WikiPage page = WikiPageLocalServiceUtil.getPage(
097                                    nodeId, title, version);
098    
099                            return contains(permissionChecker, page, actionId);
100                    }
101                    catch (NoSuchPageException nspe) {
102                            return WikiNodePermission.contains(
103                                    permissionChecker, nodeId, ActionKeys.VIEW);
104                    }
105            }
106    
107            public static boolean contains(
108                            PermissionChecker permissionChecker, long nodeId, String title,
109                            String actionId)
110                    throws PortalException, SystemException {
111    
112                    try {
113                            WikiPage page = WikiPageLocalServiceUtil.getPage(
114                                    nodeId, title, null);
115    
116                            return contains(permissionChecker, page, actionId);
117                    }
118                    catch (NoSuchPageException nspe) {
119                            return WikiNodePermission.contains(
120                                    permissionChecker, nodeId, ActionKeys.VIEW);
121                    }
122            }
123    
124            public static boolean contains(
125                    PermissionChecker permissionChecker, WikiPage page, String actionId) {
126    
127                    if (actionId.equals(ActionKeys.VIEW)) {
128                            WikiPage redirectPage = page.getRedirectPage();
129    
130                            if (redirectPage != null) {
131                                    page = redirectPage;
132                            }
133                    }
134    
135                    WikiNode node = page.getNode();
136    
137                    if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
138                            WikiPage originalPage = page;
139    
140                            if (!WikiNodePermission.contains(
141                                            permissionChecker, node, ActionKeys.VIEW)) {
142    
143                                    return false;
144                            }
145    
146                            while (page != null) {
147                                    if (!permissionChecker.hasOwnerPermission(
148                                                    page.getCompanyId(), WikiPage.class.getName(),
149                                                    page.getPageId(), page.getUserId(), ActionKeys.VIEW) &&
150                                            !permissionChecker.hasPermission(
151                                                    page.getGroupId(), WikiPage.class.getName(),
152                                                    page.getPageId(), ActionKeys.VIEW)) {
153    
154                                            return false;
155                                    }
156    
157                                    page = page.getParentPage();
158                            }
159    
160                            if (actionId.equals(ActionKeys.VIEW)) {
161                                    return true;
162                            }
163    
164                            page = originalPage;
165                    }
166    
167                    if (WikiNodePermission.contains(permissionChecker, node, actionId)) {
168                            return true;
169                    }
170    
171                    while (page != null) {
172                            if (page.isPending()) {
173                                    Boolean hasPermission = WorkflowPermissionUtil.hasPermission(
174                                            permissionChecker, page.getGroupId(),
175                                            WikiPage.class.getName(), page.getResourcePrimKey(),
176                                            actionId);
177    
178                                    if ((hasPermission != null) && hasPermission.booleanValue()) {
179                                            return true;
180                                    }
181                            }
182    
183                            if (page.isDraft() && actionId.equals(ActionKeys.DELETE) &&
184                                    (page.getStatusByUserId() == permissionChecker.getUserId())) {
185    
186                                    return true;
187                            }
188    
189                            if (permissionChecker.hasOwnerPermission(
190                                            page.getCompanyId(), WikiPage.class.getName(),
191                                            page.getPageId(), page.getUserId(), actionId) ||
192                                    permissionChecker.hasPermission(
193                                            page.getGroupId(), WikiPage.class.getName(),
194                                            page.getPageId(), actionId)) {
195    
196                                    return true;
197                            }
198    
199                            page = page.getParentPage();
200                    }
201    
202                    return false;
203            }
204    
205    }