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