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, SystemException {
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                    throws SystemException {
129    
130                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
131                            permissionChecker, page.getGroupId(), WikiPage.class.getName(),
132                            page.getPageId(), PortletKeys.WIKI, actionId);
133    
134                    if (hasPermission != null) {
135                            return hasPermission.booleanValue();
136                    }
137    
138                    if (page.isDraft()) {
139                            if (actionId.equals(ActionKeys.VIEW) &&
140                                    !contains(permissionChecker, page, ActionKeys.UPDATE)) {
141    
142                                    return false;
143                            }
144    
145                            if (actionId.equals(ActionKeys.DELETE) &&
146                                    (page.getStatusByUserId() == permissionChecker.getUserId())) {
147    
148                                    return true;
149                            }
150                    }
151                    else if (page.isPending()) {
152                            hasPermission = WorkflowPermissionUtil.hasPermission(
153                                    permissionChecker, page.getGroupId(), WikiPage.class.getName(),
154                                    page.getResourcePrimKey(), actionId);
155    
156                            if ((hasPermission != null) && hasPermission.booleanValue()) {
157                                    return true;
158                            }
159                    }
160                    else if (page.isScheduled()) {
161                            if (actionId.equals(ActionKeys.VIEW) &&
162                                    !contains(permissionChecker, page, ActionKeys.UPDATE)) {
163    
164                                    return false;
165                            }
166                    }
167    
168                    if (actionId.equals(ActionKeys.VIEW)) {
169                            WikiPage redirectPage = page.fetchRedirectPage();
170    
171                            if (redirectPage != null) {
172                                    page = redirectPage;
173                            }
174    
175                            if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
176                                    WikiNode node = page.getNode();
177    
178                                    if (!WikiNodePermission.contains(
179                                                    permissionChecker, node, actionId)) {
180    
181                                            return false;
182                                    }
183    
184                                    while (page != null) {
185                                            if (!_hasPermission(permissionChecker, page, actionId)) {
186                                                    return false;
187                                            }
188    
189                                            page = page.fetchParentPage();
190                                    }
191    
192                                    return true;
193                            }
194                    }
195    
196                    return _hasPermission(permissionChecker, page, actionId);
197            }
198    
199            private static boolean _hasPermission(
200                    PermissionChecker permissionChecker, WikiPage page, String actionId) {
201    
202                    if (permissionChecker.hasOwnerPermission(
203                                    page.getCompanyId(), WikiPage.class.getName(),
204                                    page.getResourcePrimKey(), page.getUserId(), actionId) ||
205                            permissionChecker.hasPermission(
206                                    page.getGroupId(), WikiPage.class.getName(),
207                                    page.getResourcePrimKey(), actionId)) {
208    
209                            return true;
210                    }
211    
212                    return false;
213            }
214    
215    }