1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.wiki.service.permission;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.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.portal.util.PropsValues;
23  import com.liferay.portlet.wiki.NoSuchPageException;
24  import com.liferay.portlet.wiki.model.WikiPage;
25  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
26  
27  /**
28   * <a href="WikiPagePermission.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class WikiPagePermission {
33  
34      public static void check(
35              PermissionChecker permissionChecker, long resourcePrimKey,
36              String actionId)
37          throws PortalException, SystemException {
38  
39          if (!contains(permissionChecker, resourcePrimKey, actionId)) {
40              throw new PrincipalException();
41          }
42      }
43  
44      public static void check(
45              PermissionChecker permissionChecker, long nodeId, String title,
46              String actionId)
47          throws PortalException, SystemException {
48  
49          if (!contains(permissionChecker, nodeId, title, actionId)) {
50              throw new PrincipalException();
51          }
52      }
53  
54      public static void check(
55              PermissionChecker permissionChecker, WikiPage page, String actionId)
56          throws PortalException {
57  
58          if (!contains(permissionChecker, page, actionId)) {
59              throw new PrincipalException();
60          }
61      }
62  
63      public static boolean contains(
64              PermissionChecker permissionChecker, long resourcePrimKey,
65              String actionId)
66          throws PortalException, SystemException {
67  
68          WikiPage page = WikiPageLocalServiceUtil.getPage(resourcePrimKey);
69  
70          return contains(permissionChecker, page, actionId);
71      }
72  
73      public static boolean contains(
74              PermissionChecker permissionChecker, long nodeId, String title,
75              String actionId)
76          throws PortalException, SystemException {
77  
78          try {
79              WikiPage page = WikiPageLocalServiceUtil.getPage(nodeId, title);
80  
81              return contains(permissionChecker, page, actionId);
82          }
83          catch (NoSuchPageException nspe) {
84              return WikiNodePermission.contains(
85                  permissionChecker, nodeId, ActionKeys.VIEW);
86          }
87      }
88  
89      public static boolean contains(
90          PermissionChecker permissionChecker, WikiPage page, String actionId) {
91  
92          if (actionId.equals(ActionKeys.VIEW)) {
93              WikiPage redirectPage = page.getRedirectPage();
94  
95              if (redirectPage != null) {
96                  page = redirectPage;
97              }
98          }
99  
100         if (permissionChecker.hasOwnerPermission(
101                 page.getCompanyId(), WikiPage.class.getName(),
102                 page.getResourcePrimKey(), page.getUserId(), actionId)) {
103 
104             return true;
105         }
106 
107         if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
108             WikiPage parentPage = page.getParentPage();
109 
110             if ((parentPage != null) &&
111                 !contains(permissionChecker, parentPage, ActionKeys.VIEW)) {
112 
113                 return false;
114             }
115         }
116 
117         return permissionChecker.hasPermission(
118             page.getGroupId(), WikiPage.class.getName(),
119             page.getResourcePrimKey(), actionId);
120     }
121 
122 }