View Javadoc
1   /**
2    * This file Copyright (c) 2012-2018 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       *
93       * @param permissions list of permissions
94       * @param session workspace session
95       * @param configuration AccessControlProvider configuration, parameters from workspace.xml, in this class for obtaining noTypes parameter
96       */
97      public NodeTypeBasedPermissions(List<Permission> permissions, SessionImpl session, Map<?, ?> configuration) {
98          super(permissions, session, configuration);
99          String nodeTypes = (String) configuration.get("nodeTypes");
100 
101         if (nodeTypes != null) {
102             String[] splittedTypes = nodeTypes.split(",");
103             for (String type : splittedTypes) {
104                 allowedNodeTypes.add(type);
105             }
106         }
107     }
108 
109     @Override
110     public boolean canRead(Path itemPath, ItemId itemId) throws RepositoryException {
111 
112         if ((itemId != null && "cafebabe-cafe-babe-cafe-babecafebabe".equals(itemId.toString())) || (itemPath != null && "/".equals(itemPath.toString()))) {
113             // quick check - allow access to root to all like in old mgnl security
114             return true;
115         }
116 
117         if (itemPath == null) {
118 
119             // we deal only with permissions on nodes
120             if (!itemId.denotesNode()) {
121                 itemId = ((PropertyId) itemId).getParentId();
122             }
123 
124             synchronized (monitor) {
125 
126                 if (readCache.containsKey(itemId)) {
127                     return readCache.get(itemId);
128                 }
129 
130                 itemPath = session.getHierarchyManager().getPath(itemId);
131                 boolean canRead = canRead(itemPath, itemId);
132                 readCache.put(itemId, canRead);
133                 return canRead;
134             }
135         }
136 
137         String originalPath = pathResolver.getJCRPath(itemPath);
138         int emersion = getEmersion(originalPath);
139         String path = pathResolver.getJCRPath(itemPath.getAncestor(emersion));
140 
141         log.debug("Read request for {} :: {}. The permissions will be retrieved from ancestor path: {}.", originalPath, itemId, path);
142         return ami.isGranted(path, Permission.READ);
143     }
144 
145     @Override
146     public boolean grants(Path itemPath, int permissions) throws RepositoryException {
147 
148         long magnoliaPermissions = convertJackrabbitPermissionsToMagnoliaPermissions(permissions);
149         String originalPath = pathResolver.getJCRPath(itemPath);
150         int emersion = getEmersion(originalPath);
151         String path = pathResolver.getJCRPath(itemPath.getAncestor(emersion));
152 
153         log.debug("{} permission request for {}. The permissions will be retrieved from ancestor path: {}.", PermissionImpl.getPermissionAsName(permissions), originalPath, 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 }