Search in sources :

Example 1 with Ownership

use of org.structr.core.entity.relationship.Ownership in project structr by structr.

the class AccessControlTest method test02SetDifferentPrincipalTypesAsOwner.

@Test
public void test02SetDifferentPrincipalTypesAsOwner() {
    try (final Tx tx = app.tx()) {
        final List<Principal> users = createTestNodes(Principal.class, 2);
        final Principal user1 = (Principal) users.get(0);
        final Group group1 = createTestNode(Group.class, "test group");
        final TestOne t1 = createTestNode(TestOne.class);
        t1.setProperty(AbstractNode.owner, user1);
        t1.setProperty(AbstractNode.owner, group1);
        assertEquals(group1, t1.getProperty(AbstractNode.owner));
        Ownership ownerRel = t1.getIncomingRelationship(PrincipalOwnsNode.class);
        assertNotNull(ownerRel);
        // Do additional low-level check here to ensure cardinality!
        List<Relationship> incomingRels = Iterables.toList(t1.getNode().getRelationships(Direction.INCOMING, new PrincipalOwnsNode()));
        assertEquals(1, incomingRels.size());
        tx.success();
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Group(org.structr.core.entity.Group) Ownership(org.structr.core.entity.relationship.Ownership) PrincipalOwnsNode(org.structr.core.entity.relationship.PrincipalOwnsNode) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Relationship(org.structr.api.graph.Relationship) TestOne(org.structr.core.entity.TestOne) Principal(org.structr.core.entity.Principal) Test(org.junit.Test)

Example 2 with Ownership

use of org.structr.core.entity.relationship.Ownership in project structr by structr.

the class AbstractNode method getOwnerNode.

/**
 * Returns the owner node of this node, following an INCOMING OWNS
 * relationship.
 *
 * @return the owner node of this node
 */
@Override
public final Principal getOwnerNode() {
    if (cachedOwnerNode == null) {
        final Ownership ownership = getIncomingRelationshipAsSuperUser(PrincipalOwnsNode.class);
        if (ownership != null) {
            Principal principal = ownership.getSourceNode();
            cachedOwnerNode = (Principal) principal;
        }
    }
    return cachedOwnerNode;
}
Also used : Ownership(org.structr.core.entity.relationship.Ownership)

Aggregations

Ownership (org.structr.core.entity.relationship.Ownership)2 Test (org.junit.Test)1 Relationship (org.structr.api.graph.Relationship)1 FrameworkException (org.structr.common.error.FrameworkException)1 Group (org.structr.core.entity.Group)1 Principal (org.structr.core.entity.Principal)1 TestOne (org.structr.core.entity.TestOne)1 PrincipalOwnsNode (org.structr.core.entity.relationship.PrincipalOwnsNode)1 Tx (org.structr.core.graph.Tx)1