1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.wiki.service.permission;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.security.auth.PrincipalException;
20  import com.liferay.portal.security.permission.ActionKeys;
21  import com.liferay.portal.security.permission.PermissionChecker;
22  import com.liferay.portlet.wiki.NoSuchPageException;
23  import com.liferay.portlet.wiki.model.WikiPage;
24  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
25  
26  /**
27   * <a href="WikiPagePermission.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class WikiPagePermission {
32  
33      public static void check(
34              PermissionChecker permissionChecker, long resourcePrimKey,
35              String actionId)
36          throws PortalException, SystemException {
37  
38          if (!contains(permissionChecker, resourcePrimKey, actionId)) {
39              throw new PrincipalException();
40          }
41      }
42  
43      public static void check(
44              PermissionChecker permissionChecker, long nodeId, String title,
45              String actionId)
46          throws PortalException, SystemException {
47  
48          if (!contains(permissionChecker, nodeId, title, actionId)) {
49              throw new PrincipalException();
50          }
51      }
52  
53      public static void check(
54              PermissionChecker permissionChecker, WikiPage page, String actionId)
55          throws PortalException {
56  
57          if (!contains(permissionChecker, page, actionId)) {
58              throw new PrincipalException();
59          }
60      }
61  
62      public static boolean contains(
63              PermissionChecker permissionChecker, long resourcePrimKey,
64              String actionId)
65          throws PortalException, SystemException {
66  
67          WikiPage page = WikiPageLocalServiceUtil.getPage(resourcePrimKey);
68  
69          return contains(permissionChecker, page, actionId);
70      }
71  
72      public static boolean contains(
73              PermissionChecker permissionChecker, long nodeId, String title,
74              String actionId)
75          throws PortalException, SystemException {
76  
77          try {
78              WikiPage page = WikiPageLocalServiceUtil.getPage(nodeId, title);
79  
80              return contains(permissionChecker, page, actionId);
81          }
82          catch (NoSuchPageException nspe) {
83              return WikiNodePermission.contains(
84                  permissionChecker, nodeId, ActionKeys.ADD_PAGE);
85          }
86      }
87  
88      public static boolean contains(
89          PermissionChecker permissionChecker, WikiPage page, String actionId) {
90  
91          if (permissionChecker.hasOwnerPermission(
92                  page.getCompanyId(), WikiPage.class.getName(), page.getPageId(),
93                  page.getUserId(), actionId)) {
94  
95              return true;
96          }
97  
98          return permissionChecker.hasPermission(
99              page.getGroupId(), WikiPage.class.getName(),
100             page.getResourcePrimKey(), actionId);
101     }
102 
103 }