Search in sources :

Example 1 with Group

use of org.structr.core.entity.Group in project structr by structr.

the class RenderContextTest method testAnyAllAndNoneFunctions1.

@Test
public void testAnyAllAndNoneFunctions1() {
    final ActionContext ctx = new ActionContext(securityContext, null);
    Principal user = null;
    TestOne test = null;
    try (final Tx tx = app.tx()) {
        user = app.create(User.class, "user1");
        test = app.create(TestOne.class, "test1");
        app.create(Group.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "group1"), new NodeAttribute<>(StructrApp.key(Group.class, "members"), Arrays.asList(new Principal[] { user })));
        final Group group2 = app.create(Group.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "group2"), new NodeAttribute<>(StructrApp.key(Group.class, "members"), Arrays.asList(new Principal[] { user })));
        app.create(Group.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "group3"), new NodeAttribute<>(StructrApp.key(Group.class, "members"), Arrays.asList(new Principal[] { user })));
        test.setProperty(AbstractNode.owner, group2);
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        ctx.setConstant("user", user);
        ctx.setConstant("test", test);
        assertEquals("Invalid any() result", "true", Scripting.replaceVariables(ctx, null, "${any(user.groups, is_allowed(data, test, 'read'))}"));
        assertEquals("Invalid all() result", "false", Scripting.replaceVariables(ctx, null, "${all(user.groups, is_allowed(data, test, 'read'))}"));
        assertEquals("Invalid none() result", "false", Scripting.replaceVariables(ctx, null, "${none(user.groups, is_allowed(data, test, 'read'))}"));
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
}
Also used : Group(org.structr.core.entity.Group) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.web.entity.TestOne) ActionContext(org.structr.schema.action.ActionContext) Principal(org.structr.core.entity.Principal) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 2 with Group

use of org.structr.core.entity.Group in project structr by structr.

the class UiScriptingTest method testGroupFunctions.

@Test
public void testGroupFunctions() {
    Group group = null;
    User tester = null;
    try (final Tx tx = app.tx()) {
        // create test user
        tester = createTestNode(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "tester"), new NodeAttribute<>(StructrApp.key(User.class, "password"), "test"));
        // create test group
        group = createTestNode(Group.class, new NodeAttribute<>(StructrApp.key(Group.class, "name"), "test"));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    final RenderContext renderContext = new RenderContext(securityContext, new RequestMockUp(), new ResponseMockUp(), RenderContext.EditMode.NONE);
    try (final Tx tx = app.tx()) {
        // check that the user is not in the group at first
        assertFalse("User should not be in the test group before testing", group.getMembers().contains(tester));
        // check that is_in_group returns the correct result
        assertEquals("Function is_in_group should return false.", false, Scripting.evaluate(renderContext, null, "${is_in_group(first(find('Group')), first(find('User')))}", "test"));
        // add user to group
        Scripting.evaluate(renderContext, null, "${add_to_group(first(find('Group')), first(find('User')))}", "test");
        // check that the user is in the group after the call to add_to_group
        final List<Principal> members = group.getMembers();
        assertTrue("User should be in the test group now", members.contains(tester));
        // check that is_in_group returns the correct result
        assertEquals("Function is_in_group should return true.", true, Scripting.evaluate(renderContext, null, "${is_in_group(first(find('Group')), first(find('User')))}", "test"));
        // remove user from group
        Scripting.evaluate(renderContext, null, "${remove_from_group(first(find('Group')), first(find('User')))}", "test");
        // check that the user is not in the group any more after the call to remove_from_group
        assertFalse("User should not be in the test group before testing", group.getMembers().contains(tester));
        // check that is_in_group returns the correct result
        assertEquals("Function is_in_group should return false.", false, Scripting.evaluate(renderContext, null, "${is_in_group(first(find('Group')), first(find('User')))}", "test"));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
}
Also used : Group(org.structr.core.entity.Group) NodeAttribute(org.structr.core.graph.NodeAttribute) RenderContext(org.structr.web.common.RenderContext) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Principal(org.structr.core.entity.Principal) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 3 with Group

use of org.structr.core.entity.Group in project structr by structr.

the class StructrFileAttributes method group.

@Override
public GroupPrincipal group() {
    if (file == null) {
        return null;
    }
    final List<Group> groups = new LinkedList<>();
    try (Tx tx = StructrApp.getInstance(securityContext).tx()) {
        final Principal owner = file.getOwnerNode();
        if (owner != null) {
            groups.addAll(owner.getGroups());
        }
        tx.success();
    } catch (FrameworkException fex) {
        logger.error("", fex);
    }
    return groups.size() > 0 ? groups.get(0)::getName : null;
}
Also used : Group(org.structr.core.entity.Group) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) LinkedList(java.util.LinkedList) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) UserPrincipal(java.nio.file.attribute.UserPrincipal) Principal(org.structr.core.entity.Principal)

Example 4 with Group

use of org.structr.core.entity.Group in project structr by structr.

the class AccessControlTest method testGroupMembershipVisibility.

@Test
public void testGroupMembershipVisibility() {
    Principal user1 = null;
    Principal user2 = null;
    Group group = null;
    try (final Tx tx = app.tx()) {
        user1 = createTestNode(Principal.class, "user1");
        user2 = createTestNode(Principal.class, "user2");
        tx.success();
    } catch (FrameworkException t) {
        logger.warn("", t);
        fail("Unexpected exception.");
    }
    final SecurityContext user1Context = SecurityContext.getInstance(user1, AccessMode.Backend);
    final App user1App = StructrApp.getInstance(user1Context);
    try (final Tx tx = user1App.tx()) {
        group = user1App.create(Group.class, "group");
        user1App.create(TestOne.class, "testone");
        assertEquals("Invalid group owner", user1, group.getOwnerNode());
        tx.success();
    } catch (FrameworkException t) {
        logger.warn("", t);
        fail("Unexpected exception.");
    }
    try (final Tx tx = user1App.tx()) {
        final TestOne test = user1App.nodeQuery(TestOne.class).getFirst();
        assertNotNull(test);
        test.grant(Permission.read, group);
        tx.success();
    } catch (FrameworkException t) {
        logger.warn("", t);
        fail("Unexpected exception.");
    }
    // ################################################################################################################
    // user2 is not yet member of the group, so
    // it should not be possible to access the object
    final SecurityContext user2Context = SecurityContext.getInstance(user2, AccessMode.Backend);
    final App user2App = StructrApp.getInstance(user2Context);
    try (final Tx tx = user2App.tx()) {
        final TestOne test = user2App.nodeQuery(TestOne.class).getFirst();
        assertNull(test);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    try (final Tx tx = user1App.tx()) {
        group.addMember(user2);
        tx.success();
    } catch (FrameworkException t) {
        logger.warn("", t);
        fail("Unexpected exception.");
    }
    try (final Tx tx = user2App.tx()) {
        final TestOne test = user2App.nodeQuery(TestOne.class).getFirst();
        assertNotNull("Group should be readable for members", test);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    try (final Tx tx = user2App.tx()) {
        final TestOne test = user2App.nodeQuery(TestOne.class).getFirst();
        assertNotNull("Group should be readable for members", test);
        test.setProperty(TestOne.name, "newname");
        tx.success();
        fail("User should not be able to write an object that it doesn't own.");
    } catch (FrameworkException fex) {
        assertEquals("Invalid group permissions result", 403, fex.getStatus());
        assertEquals("Invalid group permissions result", "Modification not permitted.", fex.getMessage());
    }
    try (final Tx tx = user1App.tx()) {
        final TestOne test = app.nodeQuery(TestOne.class).getFirst();
        assertNotNull("Group should be readable for members", test);
        test.grant(Permission.write, group);
        tx.success();
    } catch (FrameworkException t) {
        logger.warn("", t);
        fail("Unexpected exception.");
    }
    try (final Tx tx = user2App.tx()) {
        final TestOne test = user2App.nodeQuery(TestOne.class).getFirst();
        assertNotNull("Group should be readable for members", test);
        test.setProperty(TestOne.name, "newname");
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Group(org.structr.core.entity.Group) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) Principal(org.structr.core.entity.Principal) Test(org.junit.Test)

Example 5 with Group

use of org.structr.core.entity.Group in project structr by structr.

the class AccessControlTest method testGroupVisibilityForMembers.

@Test
public void testGroupVisibilityForMembers() {
    Principal user1 = null;
    Principal user2 = null;
    Group group = null;
    try (final Tx tx = app.tx()) {
        user1 = createTestNode(Principal.class, "user1");
        user2 = createTestNode(Principal.class, "user2");
        tx.success();
    } catch (FrameworkException t) {
        logger.warn("", t);
        fail("Unexpected exception.");
    }
    final SecurityContext user1Context = SecurityContext.getInstance(user1, AccessMode.Backend);
    final SecurityContext user2Context = SecurityContext.getInstance(user2, AccessMode.Backend);
    final App user1App = StructrApp.getInstance(user1Context);
    final App user2App = StructrApp.getInstance(user2Context);
    try (final Tx tx = user1App.tx()) {
        group = user1App.create(Group.class, "group");
        assertEquals("Invalid group owner", user1, group.getOwnerNode());
        // add user2 to group
        group.addMember(user2);
        tx.success();
    } catch (FrameworkException t) {
        logger.warn("", t);
        fail("Unexpected exception.");
    }
    try (final Tx tx = user2App.tx()) {
        final Group testGroup = user2App.nodeQuery(Group.class).andName("group").getFirst();
        assertNotNull("Group should be readable for members", testGroup);
        assertEquals("Group name should be readable for members", "group", testGroup.getName());
        tx.success();
    } catch (FrameworkException t) {
        logger.warn("", t);
        fail("Unexpected exception.");
    }
    try (final Tx tx = user2App.tx()) {
        final Group testGroup = user2App.nodeQuery(Group.class).andName("group").getFirst();
        assertNotNull("Group should be readable for members", testGroup);
        assertEquals("Group name should be readable for members", "group", testGroup.getName());
        testGroup.setProperty(Group.name, "dontchangeme");
        fail("Griup name should not be writable for members");
        tx.success();
    } catch (FrameworkException t) {
        assertEquals(403, t.getStatus());
        assertEquals("Modification not permitted.", t.getMessage());
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Group(org.structr.core.entity.Group) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Principal(org.structr.core.entity.Principal) Test(org.junit.Test)

Aggregations

Group (org.structr.core.entity.Group)16 Principal (org.structr.core.entity.Principal)13 FrameworkException (org.structr.common.error.FrameworkException)12 Tx (org.structr.core.graph.Tx)12 Test (org.junit.Test)11 LinkedList (java.util.LinkedList)4 TestOne (org.structr.core.entity.TestOne)4 List (java.util.List)3 StructrTest (org.structr.common.StructrTest)3 NodeAttribute (org.structr.core.graph.NodeAttribute)3 ActionContext (org.structr.schema.action.ActionContext)3 UnlicensedException (org.structr.common.error.UnlicensedException)2 App (org.structr.core.app.App)2 StructrApp (org.structr.core.app.StructrApp)2 StructrGraphQLTest (org.structr.rest.common.StructrGraphQLTest)2 StructrUiTest (org.structr.web.StructrUiTest)2 User (org.structr.web.entity.User)2 GroupPrincipal (java.nio.file.attribute.GroupPrincipal)1 UserPrincipal (java.nio.file.attribute.UserPrincipal)1 LinkedHashSet (java.util.LinkedHashSet)1