/** * Adds a new child tree with the given name and primary type name. * This method is a shortcut for calling {@link Tree#addChild(String)} and * {@link Tree#setProperty(String, Object, org.apache.jackrabbit.oak.api.Type)} * where the property name is {@link JcrConstants#JCR_PRIMARYTYPE}. * Note, that this method in addition verifies if the created tree exists * and is accessible in order to avoid {@link IllegalStateException} upon * subsequent modification of the new child. * * @param name The name of the child item. * @param primaryTypeName The name of the primary node type. * @return The new child node with the specified name and primary type. * @throws AccessDeniedException If the child does not exist after creation. */ @NotNull public NodeUtil addChild(String name, String primaryTypeName) throws AccessDeniedException { Tree child = tree.addChild(name); if (!child.exists()) { throw new AccessDeniedException(); } NodeUtil childUtil = new NodeUtil(child, mapper); childUtil.setName(JcrConstants.JCR_PRIMARYTYPE, primaryTypeName); return childUtil; }
@Test public void testChangePrimaryTypeOfCug() throws Exception { node.setNames(JcrConstants.JCR_MIXINTYPES, MIX_REP_CUG_MIXIN); NodeUtil cug = node.addChild(REP_CUG_POLICY, NT_REP_CUG_POLICY); cug.setStrings(REP_PRINCIPAL_NAMES, EveryonePrincipal.NAME); root.commit(); try { cug.setName(JcrConstants.JCR_PRIMARYTYPE, NodeTypeConstants.NT_OAK_UNSTRUCTURED); root.commit(); fail(); } catch (CommitFailedException e) { assertTrue(e.isAccessControlViolation()); assertEquals(21, e.getCode()); } }
@Test public void testRestrictionWithInvalidType() throws Exception { NodeUtil restriction = createAcl().getChild(aceName).getChild(REP_RESTRICTIONS); restriction.setName(REP_GLOB, "rep:glob"); try { root.commit(); fail("Creating restriction with invalid type should fail."); } catch (CommitFailedException e) { // success assertTrue(e.isAccessControlViolation()); assertThat(e.getMessage(), containsString("/testRoot/rep:policy")); } }
rNode.setName("boolean", "nt:base"); try { provider.validateRestrictions("/test", aceNode.getTree());
@Test public void testChangeRegularRepUnstructuredPrimaryType() throws Exception { Tree userTree = root.getTree(getUserManager(root).getAuthorizable(userId).getPath()); NodeUtil n = new NodeUtil(userTree).getOrAddChild("test", NodeTypeConstants.NT_REP_UNSTRUCTURED); root.commit(); n.setName(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED); root.commit(); }
@Test public void testChangeTokenParentPrimaryTypeToRepUnstructured() throws Exception { Tree userTree = root.getTree(getUserManager(root).getAuthorizable(userId).getPath()); NodeUtil node = new NodeUtil(userTree); node = node.addChild(TOKENS_NODE_NAME, JcrConstants.NT_UNSTRUCTURED); root.commit(); node.setName(JcrConstants.JCR_PRIMARYTYPE, TOKENS_NT_NAME); root.commit(); }
@Test public void testChangePrimaryType() { node = new NodeUtil(root.getTree(SUPPORTED_PATH2)); try { node.setName(JcrConstants.JCR_PRIMARYTYPE, NT_REP_CUG_POLICY); node.setStrings(REP_PRINCIPAL_NAMES, EveryonePrincipal.NAME); root.commit(); fail(); } catch (CommitFailedException e) { assertTrue(e.isAccessControlViolation()); assertEquals(20, e.getCode()); } }
@Test public void testChangeToReservedTokenNodeType() throws Exception { String parentPath = getTestUser().getPath() + "/"+TokenConstants.TOKENS_NODE_NAME; String path = parentPath+"/node"; try { Tree t = root.getTree(getTestUser().getPath()).addChild(TokenConstants.TOKENS_NODE_NAME); t.setProperty(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED, Type.NAME); t.addChild("node").setProperty(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED, Type.NAME); root.commit(); NodeUtil node = new NodeUtil(root.getTree(path)); node.setName(JcrConstants.JCR_PRIMARYTYPE, TokenConstants.TOKEN_NT_NAME); node.setString(JcrConstants.JCR_UUID, UUID.randomUUID().toString()); node.setString(TokenConstants.TOKEN_ATTRIBUTE_KEY, PasswordUtil.buildPasswordHash("key")); node.setDate(TokenConstants.TOKEN_ATTRIBUTE_EXPIRY, new Date().getTime()); root.commit(CommitMarker.asCommitAttributes()); } catch (CommitFailedException e) { assertEquals(62, e.getCode()); } finally { root.refresh(); root.getTree(parentPath).remove(); root.commit(); } } }
@Test public void testChangePrimaryType() throws RepositoryException { for (Authorizable a : authorizables) { try { NodeUtil node = new NodeUtil(getAuthorizableTree(a)); NodeUtil cache = node.addChild("childNode", JcrConstants.NT_UNSTRUCTURED); root.commit(); cache.setName(JcrConstants.JCR_PRIMARYTYPE, CacheConstants.NT_REP_CACHE); cache.setLong(CacheConstants.REP_EXPIRATION, 1); root.commit(); fail("Changing primary type of residual node below an user/group to rep:Cache must fail."); } catch (CommitFailedException e) { assertTrue(e.isConstraintViolation()); assertEquals(34, e.getCode()); } finally { root.refresh(); } } }
@Test public void testCreateNestedUser2Steps() throws Exception { Tree userTree = root.getTree(getTestUser().getPath()); NodeUtil userNode = new NodeUtil(userTree); NodeUtil profile = userNode.addChild("profile", JcrConstants.NT_UNSTRUCTURED); NodeUtil nested = profile.addChild("nested", JcrConstants.NT_UNSTRUCTURED); nested.setString(UserConstants.REP_PRINCIPAL_NAME, "nested"); nested.setString(UserConstants.REP_AUTHORIZABLE_ID, "nested"); nested.setString(JcrConstants.JCR_UUID, UUIDUtils.generateUUID("nested")); root.commit(); try { nested.setName(JcrConstants.JCR_PRIMARYTYPE, UserConstants.NT_REP_USER); root.commit(); fail("Creating nested users must be detected."); } catch (CommitFailedException e) { // success assertEquals(29, e.getCode()); } finally { root.refresh(); } }