Search in sources :

Example 1 with AccessControlList

use of com.enonic.xp.security.acl.AccessControlList in project xp by enonic.

the class ModifyNodeHandlerTest method testExample.

@Test
public void testExample() {
    final PropertyTree data = new PropertyTree();
    data.setString("notChanged", "originalValue");
    data.setString("myString", "originalValue");
    data.setString("toBeRemoved", "removeThis");
    final PropertySet mySet = data.addSet("mySet");
    mySet.setGeoPoint("myGeoPoint", new GeoPoint(30, -30));
    final Node node = Node.create().id(NodeId.from("abc")).parentPath(NodePath.ROOT).data(data).name("myNode").build();
    mockGetNode(node);
    mockUpdateNode(node);
    runScript("/lib/xp/examples/node/modify.js");
    Mockito.verify(this.nodeService).update(updateCaptor.capture());
    assertEquals(updateCaptor.getValue().getId(), NodeId.from("abc"));
    final EditableNode editedNode = getEditedNode(node);
    assertEquals("modified", editedNode.data.getString("myString"));
    assertEquals("originalValue", editedNode.data.getString("notChanged"));
    assertEquals(new GeoPoint(0, 0), editedNode.data.getGeoPoint("mySet.myGeoPoint"));
    final Iterable<String> myArray = editedNode.data.getStrings("myArray");
    assertNotNull(myArray);
    final ArrayList<String> myArrayValues = Lists.newArrayList(myArray);
    assertEquals(3, myArrayValues.size());
    assertTrue(myArrayValues.containsAll(List.of("modified1", "modified2", "modified3")));
    final AccessControlList permissions = editedNode.permissions;
    assertTrue(permissions.getEntry(PrincipalKey.from("role:newRole")).isAllowed(Permission.MODIFY));
    assertTrue(permissions.getEntry(PrincipalKey.from("user:system:newUser")).isAllowed(Permission.CREATE));
    final IndexConfigDocument indexConfigDocument = editedNode.indexConfigDocument;
    assertFalse(indexConfigDocument.getConfigForPath(PropertyPath.from("displayName")).isEnabled());
    assertTrue(indexConfigDocument.getConfigForPath(PropertyPath.from("whatever")).isFulltext());
}
Also used : AccessControlList(com.enonic.xp.security.acl.AccessControlList) GeoPoint(com.enonic.xp.util.GeoPoint) PropertyTree(com.enonic.xp.data.PropertyTree) Node(com.enonic.xp.node.Node) EditableNode(com.enonic.xp.node.EditableNode) PropertySet(com.enonic.xp.data.PropertySet) IndexConfigDocument(com.enonic.xp.index.IndexConfigDocument) EditableNode(com.enonic.xp.node.EditableNode) Test(org.junit.jupiter.api.Test)

Example 2 with AccessControlList

use of com.enonic.xp.security.acl.AccessControlList in project xp by enonic.

the class CreateContentCommandTest method mockNodeServiceCreate.

private Node mockNodeServiceCreate(final InvocationOnMock invocation) throws Throwable {
    CreateNodeParams params = (CreateNodeParams) invocation.getArguments()[0];
    final AccessControlList permissions = AccessControlList.create().add(AccessControlEntry.create().allowAll().principal(PrincipalKey.ofAnonymous()).build()).build();
    return Node.create().id(params.getNodeId() != null ? params.getNodeId() : new NodeId()).parentPath(params.getParent()).name(NodeName.from(params.getName())).data(params.getData()).indexConfigDocument(params.getIndexConfigDocument()).childOrder(params.getChildOrder() != null ? params.getChildOrder() : ChildOrder.defaultOrder()).permissions(permissions).inheritPermissions(params.inheritPermissions()).nodeType(params.getNodeType() != null ? params.getNodeType() : NodeType.DEFAULT_NODE_COLLECTION).timestamp(Instant.now()).build();
}
Also used : AccessControlList(com.enonic.xp.security.acl.AccessControlList) NodeId(com.enonic.xp.node.NodeId) CreateNodeParams(com.enonic.xp.node.CreateNodeParams)

Example 3 with AccessControlList

use of com.enonic.xp.security.acl.AccessControlList in project xp by enonic.

the class XmlNodeSerializerTest method doCreateNode.

private Node doCreateNode(final Instant instant) {
    final PropertyTree propertyTree = new PropertyTree();
    propertyTree.addString("myString", "myStringValue");
    propertyTree.addString("myString", "myStringValue2");
    propertyTree.addString("myEmptyString", "");
    propertyTree.addBoolean("myBoolean", true);
    propertyTree.addDouble("myDouble", 123.1);
    propertyTree.addLong("myLong", 111L);
    propertyTree.addXml("myXml", "<car><color>Arctic Grey<color><car>");
    propertyTree.addString("myHtmlEncoded", "<p><a href=\"/naringsliv/tema/forsikrings-og-pensjonspakker\" data-event=\"{&quot;event_category&quot;: &quot;button&quot;, &quot;event_action&quot;: &quot;click&quot;,&quot;event_label&quot;: &quot;se-php&quot;}\">Se pakkene her</a></p>");
    propertyTree.addGeoPoint("myGeoPoint", GeoPoint.from("8,4"));
    // Date & Time
    propertyTree.addInstant("myInstant", instant);
    propertyTree.addLocalTime("myLocalTime", LocalTime.of(21, 42, 0));
    propertyTree.addLocalDate("myLocalDate", LocalDate.of(2014, 11, 28));
    propertyTree.addLocalDateTime("myLocalDateTime", LocalDateTime.of(2014, 11, 28, 21, 0, 0, 0));
    // Links and ref
    propertyTree.addReference("myRef", Reference.from("abcd"));
    propertyTree.addLink("myLink", Link.from("/root/parent/child"));
    // Binary refs
    propertyTree.addBinaryReference("myBinaryRef1", BinaryReference.from("image.jpg"));
    propertyTree.addBinaryReference("myBinaryRef2", BinaryReference.from("image2.jpg"));
    // Property-set
    final PropertySet mySubset = propertyTree.addSet("mySet");
    mySubset.setString("myString", "myStringValue");
    mySubset.setBoolean("myBoolean", true);
    // Property-set in set
    final PropertySet mySubSubset = mySubset.addSet("mySet");
    mySubSubset.setString("myString", "myStringValue");
    mySubSubset.setBoolean("myBoolean", true);
    // Null values
    propertyTree.addString("myString", null);
    propertyTree.addBoolean("myBoolean", null);
    propertyTree.addDouble("myDouble", null);
    propertyTree.addLong("myLong", null);
    propertyTree.addXml("myXml", null);
    propertyTree.addGeoPoint("myGeoPoint", null);
    propertyTree.addInstant("myInstant", null);
    propertyTree.addLocalTime("myLocalTime", null);
    propertyTree.addLocalDate("myLocalDate", null);
    propertyTree.addLocalDateTime("myLocalDateTime", null);
    propertyTree.addReference("myRef", null);
    propertyTree.addLink("myLink", null);
    propertyTree.addBinaryReference("myBinaryRef2", null);
    propertyTree.addSet("nullSet", null);
    // Index configs
    final IndexConfig indexConfig = IndexConfig.create().enabled(true).fulltext(true).nGram(true).decideByType(false).includeInAllText(true).addIndexValueProcessor(IndexValueProcessors.HTML_STRIPPER).addLanguage("en").build();
    final PatternIndexConfigDocument.Builder indexConfigDocumentBuilder = PatternIndexConfigDocument.create();
    indexConfigDocumentBuilder.analyzer("no");
    indexConfigDocumentBuilder.add("mydata", indexConfig);
    indexConfigDocumentBuilder.addAllTextConfigLanguage("en");
    // Permissions
    final Permission createPermission = Permission.CREATE;
    final Permission publishPermission = Permission.PUBLISH;
    final PrincipalKey systemPrincipalKey = PrincipalKey.from("role:system.admin");
    final PrincipalKey cmsPrincipalKey = PrincipalKey.from("role:cms.admin");
    final AccessControlEntry systemAccessControlEntry = AccessControlEntry.create().principal(systemPrincipalKey).allowAll().build();
    final AccessControlEntry cmsAccessControlEntry = AccessControlEntry.create().principal(cmsPrincipalKey).allow(createPermission).deny(publishPermission).build();
    final AccessControlList accessControlList = AccessControlList.of(systemAccessControlEntry, cmsAccessControlEntry);
    return Node.create().id(NodeId.from("abc")).name(NodeName.from("my-node-name")).parentPath(NodePath.ROOT).childOrder(ChildOrder.manualOrder()).nodeType(NodeType.from("content")).data(propertyTree).indexConfigDocument(indexConfigDocumentBuilder.build()).permissions(accessControlList).inheritPermissions(false).attachedBinaries(AttachedBinaries.create().add(new AttachedBinary(BinaryReference.from("image.jpg"), "a")).add(new AttachedBinary(BinaryReference.from("image2.jpg"), "b")).build()).build();
}
Also used : AccessControlList(com.enonic.xp.security.acl.AccessControlList) IndexConfig(com.enonic.xp.index.IndexConfig) PropertyTree(com.enonic.xp.data.PropertyTree) Permission(com.enonic.xp.security.acl.Permission) PropertySet(com.enonic.xp.data.PropertySet) AccessControlEntry(com.enonic.xp.security.acl.AccessControlEntry) PatternIndexConfigDocument(com.enonic.xp.index.PatternIndexConfigDocument) PrincipalKey(com.enonic.xp.security.PrincipalKey) AttachedBinary(com.enonic.xp.node.AttachedBinary)

Example 4 with AccessControlList

use of com.enonic.xp.security.acl.AccessControlList in project xp by enonic.

the class XmlPermissionsParserTest method testParse.

@Test
public void testParse() throws Exception {
    final Document doc;
    try (Reader reader = new InputStreamReader(getClass().getResourceAsStream("permissions.xml"))) {
        doc = DomHelper.parse(reader);
    }
    final AccessControlList accessControlList = XmlPermissionsParser.parse(DomElement.from(doc.getDocumentElement()));
    final AccessControlEntry entry = accessControlList.getEntry(PrincipalKey.from("role:system.admin"));
    assertNotNull(entry);
    assertTrue(entry.isAllowed(Permission.READ));
    assertTrue(entry.isAllowed(Permission.CREATE));
    assertTrue(entry.isAllowed(Permission.MODIFY));
    assertTrue(entry.isAllowed(Permission.DELETE));
    assertTrue(entry.isAllowed(Permission.PUBLISH));
    assertTrue(entry.isAllowed(Permission.READ_PERMISSIONS));
    assertTrue(entry.isAllowed(Permission.WRITE_PERMISSIONS));
    final AccessControlEntry cmsAdmin = accessControlList.getEntry(PrincipalKey.from("role:cms.admin"));
    assertNotNull(cmsAdmin);
    assertTrue(cmsAdmin.isAllowed(Permission.CREATE));
    assertTrue(cmsAdmin.isDenied(Permission.PUBLISH));
}
Also used : AccessControlList(com.enonic.xp.security.acl.AccessControlList) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) AccessControlEntry(com.enonic.xp.security.acl.AccessControlEntry) Document(org.w3c.dom.Document) Test(org.junit.jupiter.api.Test)

Example 5 with AccessControlList

use of com.enonic.xp.security.acl.AccessControlList in project xp by enonic.

the class CreateNodeCommand method execute.

public Node execute() {
    Preconditions.checkNotNull(params.getParent(), "Path of parent Node must be specified");
    Preconditions.checkArgument(params.getParent().isAbsolute(), "Path to parent Node must be absolute: " + params.getParent());
    NodeHelper.runAsAdmin(this::verifyNotExistsAlready);
    final Node parentNode = NodeHelper.runAsAdmin(this::verifyParentExists);
    if (parentNode == null) {
        throw new NodeNotFoundException("Parent node to node with name '" + params.getName() + "' with parent path '" + params.getParent() + "' not found");
    }
    requireContextUserPermission(Permission.CREATE, parentNode);
    final PrincipalKey user = getCurrentPrincipalKey();
    final AccessControlList permissions = getAccessControlEntries(user);
    final Long manualOrderValue = NodeHelper.runAsAdmin(() -> resolvePotentialManualOrderValue(parentNode));
    final AttachedBinaries attachedBinaries = storeAndAttachBinaries();
    final Node.Builder nodeBuilder = Node.create().id(this.params.getNodeId() != null ? params.getNodeId() : new NodeId()).parentPath(params.getParent()).name(NodeName.from(params.getName())).data(params.getData()).indexConfigDocument(params.getIndexConfigDocument()).childOrder(params.getChildOrder() != null ? params.getChildOrder() : ChildOrder.defaultOrder()).manualOrderValue(manualOrderValue).permissions(permissions).inheritPermissions(params.inheritPermissions()).nodeType(params.getNodeType() != null ? params.getNodeType() : NodeType.DEFAULT_NODE_COLLECTION).attachedBinaries(attachedBinaries).timestamp(this.timestamp != null ? this.timestamp : Instant.now(CLOCK));
    final Node newNode = nodeBuilder.build();
    return StoreNodeCommand.create(this).node(newNode).updateMetadataOnly(false).build().execute();
}
Also used : AccessControlList(com.enonic.xp.security.acl.AccessControlList) NodeNotFoundException(com.enonic.xp.node.NodeNotFoundException) Node(com.enonic.xp.node.Node) NodeId(com.enonic.xp.node.NodeId) AttachedBinaries(com.enonic.xp.node.AttachedBinaries) PrincipalKey(com.enonic.xp.security.PrincipalKey)

Aggregations

AccessControlList (com.enonic.xp.security.acl.AccessControlList)53 Test (org.junit.jupiter.api.Test)35 Node (com.enonic.xp.node.Node)26 PropertyTree (com.enonic.xp.data.PropertyTree)15 PrincipalKey (com.enonic.xp.security.PrincipalKey)8 CreateNodeParams (com.enonic.xp.node.CreateNodeParams)6 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)6 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)6 AccessControlEntry (com.enonic.xp.security.acl.AccessControlEntry)5 Content (com.enonic.xp.content.Content)4 Context (com.enonic.xp.context.Context)4 PropertySet (com.enonic.xp.data.PropertySet)4 ApplyNodePermissionsParams (com.enonic.xp.node.ApplyNodePermissionsParams)4 NodeId (com.enonic.xp.node.NodeId)4 NodePath (com.enonic.xp.node.NodePath)4 FindNodesByParentParams (com.enonic.xp.node.FindNodesByParentParams)3 FindNodesByParentResult (com.enonic.xp.node.FindNodesByParentResult)3 ImportNodeResult (com.enonic.xp.node.ImportNodeResult)3 Nodes (com.enonic.xp.node.Nodes)3 UpdateNodeParams (com.enonic.xp.node.UpdateNodeParams)3