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.PermissionChecker;
21  import com.liferay.portlet.wiki.model.WikiNode;
22  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
23  
24  /**
25   * <a href="WikiNodePermission.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Brian Wing Shun Chan
28   */
29  public class WikiNodePermission {
30  
31      public static void check(
32              PermissionChecker permissionChecker, long nodeId, String actionId)
33          throws PortalException, SystemException {
34  
35          if (!contains(permissionChecker, nodeId, actionId)) {
36              throw new PrincipalException();
37          }
38      }
39  
40      public static void check(
41              PermissionChecker permissionChecker, long groupId, String name,
42              String actionId)
43          throws PortalException, SystemException {
44  
45          if (!contains(permissionChecker, groupId, name, actionId)) {
46              throw new PrincipalException();
47          }
48      }
49  
50      public static void check(
51              PermissionChecker permissionChecker, WikiNode node, String actionId)
52          throws PortalException {
53  
54          if (!contains(permissionChecker, node, actionId)) {
55              throw new PrincipalException();
56          }
57      }
58  
59      public static boolean contains(
60              PermissionChecker permissionChecker, long nodeId, String actionId)
61          throws PortalException, SystemException {
62  
63          WikiNode node = WikiNodeLocalServiceUtil.getNode(nodeId);
64  
65          return contains(permissionChecker, node, actionId);
66      }
67  
68      public static boolean contains(
69              PermissionChecker permissionChecker, long groupId, String name,
70              String actionId)
71          throws PortalException, SystemException {
72  
73          WikiNode node = WikiNodeLocalServiceUtil.getNode(groupId, name);
74  
75          return contains(permissionChecker, node, actionId);
76      }
77  
78      public static boolean contains(
79          PermissionChecker permissionChecker, WikiNode node, String actionId) {
80  
81          if (permissionChecker.hasOwnerPermission(
82                  node.getCompanyId(), WikiNode.class.getName(), node.getNodeId(),
83                  node.getUserId(), actionId)) {
84  
85              return true;
86          }
87  
88          return permissionChecker.hasPermission(
89              node.getGroupId(), WikiNode.class.getName(), node.getNodeId(),
90              actionId);
91      }
92  
93  }