/** * Utility method that drills down from the current node using the specified list of child * names, and returns an element only if it fully matches the provided path * * @param pathElements */ public SecureTreeNode getNode(String... pathElements) { SecureTreeNode curr = this; for (int i = 0; i < pathElements.length; i++) { final SecureTreeNode next = curr.getChild(pathElements[i]); if (next == null) { return null; } else { curr = next; } } return curr; }
/** * Utility method that drills down from the current node using the specified list of child * names, and returns the latest element found along that path (might not be correspondent to * the full path specified, security paths can be incomplete, the definition of the parent * applies to the missing children as well) * * @param pathElements */ public SecureTreeNode getDeepestNode(String... pathElements) { SecureTreeNode curr = this; SecureTreeNode result = this; for (int i = 0; i < pathElements.length; i++) { final SecureTreeNode next = curr.getChild(pathElements[i]); if (next == null) { return result; } else { curr = next; // don't return info about a node that has no explicit // rule associated, the parent will do if (curr.authorizedRoles != null && !curr.authorizedRoles.isEmpty()) { result = curr; } } } return curr; }
/** * Adds a child to this path element * * @param name */ public SecureTreeNode addChild(String name) { if (getChild(name) != null) throw new IllegalArgumentException( "This pathElement " + name + " is already among my children"); SecureTreeNode child = new SecureTreeNode(this); children.put(name, child); return child; }
@Test public void testEmptyRoot() { SecureTreeNode root = new SecureTreeNode(); // smoke tests assertNull(root.getChild("NotThere")); assertEquals(SecureTreeNode.EVERYBODY, root.getAuthorizedRoles(AccessMode.READ)); assertEquals(SecureTreeNode.EVERYBODY, root.getAuthorizedRoles(AccessMode.WRITE)); // empty, deepest node is itself SecureTreeNode node = root.getDeepestNode(new String[] {"a", "b"}); assertSame(root, node); // allows access to everyone assertTrue(root.canAccess(anonymous, AccessMode.WRITE)); assertTrue(root.canAccess(anonymous, AccessMode.READ)); // make sure this includes not having a current user as well assertTrue(root.canAccess(null, AccessMode.WRITE)); assertTrue(root.canAccess(null, AccessMode.READ)); } }
SecureTreeNode topp = root.getChild("topp"); assertNotNull(topp); assertEquals(3, topp.children.size()); SecureTreeNode states = topp.getChild("states"); SecureTreeNode landmarks = topp.getChild("landmarks"); SecureTreeNode bases = topp.getChild("bases"); assertNotNull(states); assertNotNull(landmarks);