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                            if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
135                                    WikiNode node = page.getNode();
136    
137                                    if (!WikiNodePermission.contains(
138                                                    permissionChecker, node, actionId)) {
139    
140                                            return false;
141                                    }
142    
143                                    while (page != null) {
144                                            if (!_hasPermission(permissionChecker, page, actionId)) {
145                                                    return false;
146                                            }
147    
148                                            page = page.getParentPage();
149                                    }
150    
151                                    return true;
152                            }
153                    }
154    
155                    if (page.isPending()) {
156                            Boolean hasPermission = WorkflowPermissionUtil.hasPermission(
157                                    permissionChecker, page.getGroupId(), WikiPage.class.getName(),
158                                    page.getResourcePrimKey(), actionId);
159    
160                            if ((hasPermission != null) && hasPermission.booleanValue()) {
161                                    return true;
162                            }
163                    }
164    
165                    if (page.isDraft() && actionId.equals(ActionKeys.DELETE) &&
166                            (page.getStatusByUserId() == permissionChecker.getUserId())) {
167    
168                            return true;
169                    }
170    
171                    return _hasPermission(permissionChecker, page, actionId);
172            }
173    
174            private static boolean _hasPermission(
175                    PermissionChecker permissionChecker, WikiPage page, String actionId) {
176    
177                    if (permissionChecker.hasOwnerPermission(
178                                    page.getCompanyId(), WikiPage.class.getName(),
179                                    page.getResourcePrimKey(), page.getUserId(), actionId) ||
180                            permissionChecker.hasPermission(
181                                    page.getGroupId(), WikiPage.class.getName(),
182                                    page.getResourcePrimKey(), actionId)) {
183    
184                            return true;
185                    }
186    
187                    return false;
188            }
189    
190    }