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