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.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.getResourcePrimKey(), page.getUserId(),
150                                                    ActionKeys.VIEW) &&
151                                            !permissionChecker.hasPermission(
152                                                    page.getGroupId(), WikiPage.class.getName(),
153                                                    page.getResourcePrimKey(), ActionKeys.VIEW)) {
154    
155                                            return false;
156                                    }
157    
158                                    page = page.getParentPage();
159                            }
160    
161                            if (actionId.equals(ActionKeys.VIEW)) {
162                                    return true;
163                            }
164    
165                            page = originalPage;
166                    }
167    
168                    if (WikiNodePermission.contains(permissionChecker, node, actionId)) {
169                            return true;
170                    }
171    
172                    while (page != null) {
173                            if (page.isPending()) {
174                                    Boolean hasPermission = WorkflowPermissionUtil.hasPermission(
175                                            permissionChecker, page.getGroupId(),
176                                            WikiPage.class.getName(), page.getResourcePrimKey(),
177                                            actionId);
178    
179                                    if ((hasPermission != null) && hasPermission.booleanValue()) {
180                                            return true;
181                                    }
182                            }
183    
184                            if (page.isDraft() && actionId.equals(ActionKeys.DELETE) &&
185                                    (page.getStatusByUserId() == permissionChecker.getUserId())) {
186    
187                                    return true;
188                            }
189    
190                            if (permissionChecker.hasOwnerPermission(
191                                            page.getCompanyId(), WikiPage.class.getName(),
192                                            page.getResourcePrimKey(), page.getUserId(), actionId) ||
193                                    permissionChecker.hasPermission(
194                                            page.getGroupId(), WikiPage.class.getName(),
195                                            page.getResourcePrimKey(), actionId)) {
196    
197                                    return true;
198                            }
199    
200                            page = page.getParentPage();
201                    }
202    
203                    return false;
204            }
205    
206    }