View Javadoc
1   /*
2    * Copyright (C) 2003-2007 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.ecm.utils.permission;
18  
19  import java.security.AccessControlException;
20  
21  import javax.jcr.Node;
22  import javax.jcr.RepositoryException;
23  
24  import org.exoplatform.services.jcr.access.PermissionType;
25  import org.exoplatform.services.jcr.core.ExtendedNode;
26  import org.exoplatform.services.security.IdentityConstants;
27  
28  /**
29   * The Class PermissionUtil use to check permission for a node
30   */
31  public class PermissionUtil {
32  
33    /**
34     * Can read node
35     *
36     * @param node the node
37     * @return true, if successful
38     * @throws RepositoryException the repository exception
39     */
40    public static boolean canRead(Node node) throws RepositoryException {
41      return checkPermission(node,PermissionType.READ);
42    }
43  
44    /**
45     * Can add node.
46     *
47     * @param node the node
48     * @return true, if successful
49     * @throws RepositoryException the repository exception
50     */
51    public static boolean canAddNode(Node node) throws RepositoryException {
52      return checkPermission(node,PermissionType.ADD_NODE);
53    }
54  
55    /**
56     * Can change permission.
57     *
58     * @param node the node
59     * @return true, if successful
60     * @throws RepositoryException the repository exception
61     */
62    public static boolean canChangePermission(Node node) throws RepositoryException {
63      return checkPermission(node,PermissionType.CHANGE_PERMISSION);
64    }
65  
66    /**
67     * Checks if is any role.
68     *
69     * @param node the node
70     * @return true, if is any role
71     * @throws RepositoryException the repository exception
72     */
73    public static boolean isAnyRole(Node node)throws RepositoryException {
74      return checkPermission(node,IdentityConstants.ANY);
75    }
76  
77    /**
78     * Can set property.
79     *
80     * @param node the node
81     * @return true, if successful
82     * @throws RepositoryException the repository exception
83     */
84    public static boolean canSetProperty(Node node) throws RepositoryException {
85      return checkPermission(node,PermissionType.SET_PROPERTY);
86    }
87  
88    /**
89     * Can remove node.
90     *
91     * @param node the node
92     * @return true, if successful
93     * @throws RepositoryException the repository exception
94     */
95    public static boolean canRemoveNode(Node node) throws RepositoryException {
96      return checkPermission(node,PermissionType.REMOVE);
97    }
98  
99    private static boolean checkPermission(Node node,String permissionType) throws RepositoryException {
100     try {
101       ((ExtendedNode)node).checkPermission(permissionType);
102       return true;
103     } catch(AccessControlException e) {
104       return false;
105     }
106   }
107 
108 }