View Javadoc

1   /**
2    * This file Copyright (c) 2012-2014 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.core;
35  
36  import info.magnolia.cms.security.Permission;
37  import info.magnolia.cms.security.PermissionImpl;
38  
39  import java.util.HashSet;
40  import java.util.List;
41  import java.util.Map;
42  import java.util.Set;
43  
44  import javax.jcr.AccessDeniedException;
45  import javax.jcr.ItemNotFoundException;
46  import javax.jcr.NamespaceException;
47  import javax.jcr.Node;
48  import javax.jcr.RepositoryException;
49  
50  import org.apache.jackrabbit.core.SessionImpl;
51  import org.apache.jackrabbit.core.id.ItemId;
52  import org.apache.jackrabbit.core.id.PropertyId;
53  import org.apache.jackrabbit.spi.Name;
54  import org.apache.jackrabbit.spi.Path;
55  import org.apache.jackrabbit.spi.commons.conversion.CachingPathResolver;
56  import org.apache.jackrabbit.spi.commons.conversion.IllegalNameException;
57  import org.apache.jackrabbit.spi.commons.conversion.NameResolver;
58  import org.apache.jackrabbit.spi.commons.conversion.ParsingPathResolver;
59  import org.apache.jackrabbit.spi.commons.conversion.PathResolver;
60  import org.slf4j.Logger;
61  import org.slf4j.LoggerFactory;
62  
63  /**
64   * Permissions are retrieved from requested node or from its ancestor, if the node isn't one of valid node types specified via constructor.
65   * Permission based on user ACL for given workspace. Caches the result of resolving paths from ids, the caching
66   * implementation based {@link org.apache.jackrabbit.core.security.authorization.principalbased.ACLProvider.CompiledPermissionImpl}.
67   */
68  public class NodeTypeBasedPermissions extends DefaultACLBasedPermissions {
69  
70      private final Set<String> allowedNodeTypes = new HashSet<String>();
71  
72      private static final Logger log = LoggerFactory.getLogger(NodeTypeBasedPermissions.class);
73  
74      /**
75       * Used to convert a jackrabbit Path abstraction into a path string with slashes and no namespaces.
76       */
77      private final PathResolver pathResolver = new CachingPathResolver(new ParsingPathResolver(null, new NameResolver() {
78  
79          @Override
80          public Name getQName(String name) throws IllegalNameException, NamespaceException {
81              throw new UnsupportedOperationException();
82          }
83  
84          @Override
85          public String getJCRName(Name name) throws NamespaceException {
86              return name.getLocalName();
87          }
88      }));
89  
90      /**
91       * Constructor.
92       * @param permissions list of permissions
93       * @param session workspace session
94       * @param configuration AccessControlProvider configuration, parameters from workspace.xml, in this class for obtaining noTypes parameter
95       */
96      public NodeTypeBasedPermissions(List<Permission> permissions, SessionImpl session, Map<?, ?> configuration) {
97          super(permissions,session, configuration);
98          String nodeTypes = (String)configuration.get("nodeTypes");
99  
100         if (nodeTypes != null) {
101             String[] splittedTypes = nodeTypes.split(",");
102             for (String type: splittedTypes) {
103                     allowedNodeTypes.add(type);
104             }
105         }
106     }
107 
108     @Override
109     public boolean canRead(Path itemPath, ItemId itemId) throws RepositoryException {
110 
111         if ((itemId != null && "cafebabe-cafe-babe-cafe-babecafebabe".equals(itemId.toString())) || (itemPath != null && "/".equals(itemPath.toString()))) {
112             // quick check - allow access to root to all like in old mgnl security
113             return true;
114         }
115 
116         if (itemPath == null) {
117 
118             // we deal only with permissions on nodes
119             if (!itemId.denotesNode()) {
120                 itemId = ((PropertyId)itemId).getParentId();
121             }
122 
123             synchronized (monitor) {
124 
125                 if (readCache.containsKey(itemId)) {
126                     return readCache.get(itemId);
127                 }
128 
129                 itemPath = session.getHierarchyManager().getPath(itemId);
130                 boolean canRead = canRead(itemPath, itemId);
131                 readCache.put(itemId, canRead);
132                 return canRead;
133             }
134         }
135 
136         String originalPath = pathResolver.getJCRPath(itemPath);
137         int emersion = getEmersion(originalPath);
138         String path = pathResolver.getJCRPath(itemPath.getAncestor(emersion));
139 
140         log.debug("Read request for " + originalPath + " :: " + itemId + ". The permissions will be retrieved from ancestor path: " + path + ".");
141         return ami.isGranted(path, Permission.READ);
142     }
143 
144     @Override
145     public boolean grants(Path itemPath, int permissions) throws RepositoryException {
146 
147         long magnoliaPermissions = convertJackrabbitPermissionsToMagnoliaPermissions(permissions);
148         String originalPath = pathResolver.getJCRPath(itemPath);
149         int emersion = getEmersion(originalPath);
150         String path = pathResolver.getJCRPath(itemPath.getAncestor(emersion));
151 
152         log.debug(PermissionImpl.getPermissionAsName(permissions) + "permission request for " + originalPath +
153                 ". The permissions will be retrieved from ancestor path: " + path + ".");
154         return ami.isGranted(path, magnoliaPermissions);
155     }
156 
157     private int getEmersion(String originalPath) throws AccessDeniedException, RepositoryException {
158         int emersion = 0;
159 
160         if (session.nodeExists(originalPath)) {
161             Node node = session.getNode(originalPath);
162             try {
163                 while (!isAllowedNodeType(node)) {
164                     node = node.getParent();
165                     emersion++;
166                 }
167             } catch (ItemNotFoundException e) {
168                 return 0; //node with an allowed type wasn't find, behave like DefaultACLBasedPermissions
169             }
170         }
171         return emersion;
172     }
173 
174     private boolean isAllowedNodeType(Node node) throws RepositoryException {
175         if (allowedNodeTypes.isEmpty()) {
176             return true;  //allowed node types aren't specified, behave like DefaultACLBasedPermissions
177         }
178         for(String nodeType: allowedNodeTypes) {
179             if (node.isNodeType(nodeType)) {
180                 return true;
181             }
182         }
183         return false;
184     }
185 }