001    /**
002     * Copyright (c) 2000-2013 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.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.PermissionChecker;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portal.util.PropsValues;
026    import com.liferay.portlet.wiki.NoSuchPageException;
027    import com.liferay.portlet.wiki.NoSuchPageResourceException;
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    public class WikiPagePermission {
036    
037            public static void check(
038                            PermissionChecker permissionChecker, long resourcePrimKey,
039                            String actionId)
040                    throws PortalException, SystemException {
041    
042                    if (!contains(permissionChecker, resourcePrimKey, actionId)) {
043                            throw new PrincipalException();
044                    }
045            }
046    
047            public static void check(
048                            PermissionChecker permissionChecker, long nodeId, String title,
049                            double version, String actionId)
050                    throws PortalException, SystemException {
051    
052                    if (!contains(permissionChecker, nodeId, title, version, actionId)) {
053                            throw new PrincipalException();
054                    }
055            }
056    
057            public static void check(
058                            PermissionChecker permissionChecker, long nodeId, String title,
059                            String actionId)
060                    throws PortalException, SystemException {
061    
062                    if (!contains(permissionChecker, nodeId, title, actionId)) {
063                            throw new PrincipalException();
064                    }
065            }
066    
067            public static void check(
068                            PermissionChecker permissionChecker, WikiPage page, String actionId)
069                    throws PortalException {
070    
071                    if (!contains(permissionChecker, page, actionId)) {
072                            throw new PrincipalException();
073                    }
074            }
075    
076            public static boolean contains(
077                            PermissionChecker permissionChecker, long resourcePrimKey,
078                            String actionId)
079                    throws PortalException, SystemException {
080    
081                    try {
082                            WikiPage page = WikiPageLocalServiceUtil.getPage(
083                                    resourcePrimKey, (Boolean)null);
084    
085                            return contains(permissionChecker, page, actionId);
086                    }
087                    catch (NoSuchPageResourceException nspre) {
088                            return false;
089                    }
090            }
091    
092            public static boolean contains(
093                            PermissionChecker permissionChecker, long nodeId, String title,
094                            double version, String actionId)
095                    throws PortalException, SystemException {
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, SystemException {
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.getRedirectPage();
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.getParentPage();
189                                    }
190    
191                                    return true;
192                            }
193                    }
194    
195                    return _hasPermission(permissionChecker, page, actionId);
196            }
197    
198            private static boolean _hasPermission(
199                    PermissionChecker permissionChecker, WikiPage page, String actionId) {
200    
201                    if (permissionChecker.hasOwnerPermission(
202                                    page.getCompanyId(), WikiPage.class.getName(),
203                                    page.getResourcePrimKey(), page.getUserId(), actionId) ||
204                            permissionChecker.hasPermission(
205                                    page.getGroupId(), WikiPage.class.getName(),
206                                    page.getResourcePrimKey(), actionId)) {
207    
208                            return true;
209                    }
210    
211                    return false;
212            }
213    
214    }