Search in sources :

Example 31 with Principal

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

the class SystemTest method testCallbacksWithNormalContext.

@Test
public void testCallbacksWithNormalContext() {
    try {
        Principal person = this.createTestNode(Principal.class);
        final SecurityContext securityContext = SecurityContext.getInstance(person, null, AccessMode.Backend);
        testCallbacks(securityContext);
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) Principal(org.structr.core.entity.Principal) Test(org.junit.Test)

Example 32 with Principal

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

the class ScriptingTest method testGrantViaScripting.

@Test
public void testGrantViaScripting() {
    Settings.LogSchemaOutput.setValue(true);
    // setup phase: create schema nodes
    try (final Tx tx = app.tx()) {
        // create two nodes and associate them with each other
        final SchemaNode sourceNode = createTestNode(SchemaNode.class, "Source");
        final SchemaMethod method = createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest01"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.grant(Structr.find('Principal')[0], 'read', 'write'); }"));
        sourceNode.setProperty(SchemaNode.schemaMethods, Arrays.asList(new SchemaMethod[] { method }));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    final ConfigurationProvider config = StructrApp.getConfiguration();
    final Class sourceType = config.getNodeEntityClass("Source");
    Principal testUser = null;
    // create test node as superuser
    try (final Tx tx = app.tx()) {
        app.create(sourceType);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    // create test user
    try (final Tx tx = app.tx()) {
        testUser = app.create(Principal.class, new NodeAttribute<>(Principal.name, "test"), new NodeAttribute<>(StructrApp.key(Principal.class, "password"), "test"));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    final App userApp = StructrApp.getInstance(SecurityContext.getInstance(testUser, AccessMode.Backend));
    // first test without grant, expect no test object to be found using the user context
    try (final Tx tx = userApp.tx()) {
        assertEquals("Invalid grant() scripting result", 0, userApp.nodeQuery(sourceType).getAsList().size());
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    // grant read access to test user
    try (final Tx tx = app.tx()) {
        app.nodeQuery(sourceType).getFirst().invokeMethod("doTest01", Collections.EMPTY_MAP, true);
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    // first test without grant, expect no test object to be found using the user context
    try (final Tx tx = userApp.tx()) {
        assertEquals("Invalid grant() scripting result", 1, userApp.nodeQuery(sourceType).getAsList().size());
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) SchemaNode(org.structr.core.entity.SchemaNode) NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) SchemaMethod(org.structr.core.entity.SchemaMethod) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) Principal(org.structr.core.entity.Principal) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 33 with Principal

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

the class ScriptingTest method testSystemProperties.

@Test
public void testSystemProperties() {
    try {
        final Principal user = createTestNode(Principal.class);
        // create new node
        TestOne t1 = createTestNode(TestOne.class, user);
        final SecurityContext userContext = SecurityContext.getInstance(user, AccessMode.Frontend);
        final App userApp = StructrApp.getInstance(userContext);
        try (final Tx tx = userApp.tx()) {
            final ActionContext userActionContext = new ActionContext(userContext, null);
            assertEquals("node should be of type TestOne", "TestOne", Scripting.replaceVariables(userActionContext, t1, "${(get(this, 'type'))}"));
            try {
                assertEquals("setting the type should fail", "TestTwo", Scripting.replaceVariables(userActionContext, t1, "${(set(this, 'type', 'TestThree'), get(this, 'type'))}"));
                fail("setting a system property should fail");
            } catch (FrameworkException fx) {
            }
            assertEquals("setting the type should work after setting it with unlock_system_properties_once", "TestFour", Scripting.replaceVariables(userActionContext, t1, "${(unlock_system_properties_once(this), set(this, 'type', 'TestFour'), get(this, 'type'))}"));
            tx.success();
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) SecurityContext(org.structr.common.SecurityContext) TestOne(org.structr.core.entity.TestOne) ActionContext(org.structr.schema.action.ActionContext) Principal(org.structr.core.entity.Principal) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 34 with Principal

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

the class GraphQLTest method testBasics.

@Test
public void testBasics() {
    RestAssured.basePath = "/structr/graphql";
    Group group = null;
    Principal tester = null;
    try (final Tx tx = app.tx()) {
        final PropertyKey<List> membersKey = StructrApp.key(Group.class, "members");
        tester = app.create(Principal.class, new NodeAttribute<>(Principal.name, "tester"));
        group = app.create(Group.class, new NodeAttribute<>(Group.name, "TestGroup"), new NodeAttribute<>(membersKey, Arrays.asList(tester)));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    final String query1 = "{ Group { id, type, name, members { id, type, name } }, Principal(_pageSize: 1) { id, type name }}";
    final String query2 = "{ Group { id, type, name, members { } }}";
    final String query3 = "{ Group(id: \"" + group.getUuid() + "\") { id, type, name }}";
    RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseTo(System.out)).body(query1).expect().statusCode(200).body("Group", hasSize(1)).body("Principal", hasSize(1)).body("Group[0].id", equalTo(group.getUuid())).body("Group[0].type", equalTo("Group")).body("Group[0].name", equalTo("TestGroup")).body("Group[0].members[0].id", equalTo(tester.getUuid())).body("Group[0].members[0].type", equalTo("Principal")).body("Group[0].members[0].name", equalTo("tester")).body("Principal[0].id", equalTo(group.getUuid())).body("Principal[0].type", equalTo("Group")).body("Principal[0].name", equalTo("TestGroup")).when().post("/");
    RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseTo(System.out)).body(query2).expect().statusCode(422).body("message", equalTo("Parse error at } in line 1, column 36")).body("code", equalTo(422)).body("query", equalTo(query2)).when().post("/");
    RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseTo(System.out)).body(query3).expect().statusCode(200).body("Group", hasSize(1)).body("Group[0].id", equalTo(group.getUuid())).body("Group[0].type", equalTo("Group")).body("Group[0].name", equalTo("TestGroup")).when().post("/");
}
Also used : Group(org.structr.core.entity.Group) NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) List(java.util.List) LinkedList(java.util.LinkedList) Principal(org.structr.core.entity.Principal) Test(org.junit.Test) StructrGraphQLTest(org.structr.rest.common.StructrGraphQLTest)

Example 35 with Principal

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

the class GraphQLTest method testAdvancedQueries.

@Test
public void testAdvancedQueries() {
    final List<MailTemplate> templates = new LinkedList<>();
    final List<Principal> team = new LinkedList<>();
    Group group = null;
    try (final Tx tx = app.tx()) {
        final PropertyKey<List> membersKey = StructrApp.key(Group.class, "members");
        team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Axel")));
        team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Christian")));
        team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Christian")));
        team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Inès")));
        team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Kai")));
        team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Lukas")));
        team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Michael")));
        team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Susanne")));
        team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Tobias")));
        group = app.create(Group.class, new NodeAttribute<>(Group.name, "Structr Team"), new NodeAttribute<>(membersKey, team));
        app.create(Group.class, new NodeAttribute<>(Group.name, "All teams"), new NodeAttribute<>(membersKey, Arrays.asList(group)));
        templates.add(app.create(MailTemplate.class, new NodeAttribute<>(StructrApp.key(MailTemplate.class, "text"), "MailTemplate4"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "locale"), "de_DE"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "name"), "zrtsga"), new NodeAttribute<>(AbstractNode.owner, team.get(2))));
        templates.add(app.create(MailTemplate.class, new NodeAttribute<>(StructrApp.key(MailTemplate.class, "text"), "MailTemplate2"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "locale"), "de_DE"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "name"), "lertdf"), new NodeAttribute<>(AbstractNode.owner, team.get(0))));
        templates.add(app.create(MailTemplate.class, new NodeAttribute<>(StructrApp.key(MailTemplate.class, "text"), "MailTemplate5"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "locale"), "de_DE"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "name"), "tzegsg"), new NodeAttribute<>(AbstractNode.owner, team.get(3))));
        templates.add(app.create(MailTemplate.class, new NodeAttribute<>(StructrApp.key(MailTemplate.class, "text"), "MailTemplate3"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "locale"), "de_DE"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "name"), "asgw"), new NodeAttribute<>(AbstractNode.owner, team.get(1))));
        templates.add(app.create(MailTemplate.class, new NodeAttribute<>(StructrApp.key(MailTemplate.class, "text"), "MailTemplate6"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "locale"), "de_DE"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "name"), "dfjgr"), new NodeAttribute<>(AbstractNode.owner, team.get(4))));
        templates.add(app.create(MailTemplate.class, new NodeAttribute<>(StructrApp.key(MailTemplate.class, "text"), "MailTemplate1"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "locale"), "de_DE"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "name"), "abcdef"), new NodeAttribute<>(AbstractNode.owner, team.get(0))));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    RestAssured.basePath = "/structr/graphql";
    {
        final Map<String, Object> result = fetchGraphQL("{ Principal(id: \"" + team.get(0).getUuid() + "\") { id, type, name } }");
        assertMapPathValueIs(result, "Principal.#", 1);
        assertMapPathValueIs(result, "Principal.0.id", team.get(0).getUuid());
        assertMapPathValueIs(result, "Principal.0.type", "Principal");
        assertMapPathValueIs(result, "Principal.0.name", "Axel");
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Principal(name: \"Axel\") { name } }");
        assertMapPathValueIs(result, "Principal.#", 1);
        assertMapPathValueIs(result, "Principal.0.name", "Axel");
        assertMapPathValueIs(result, "Principal.0.type", null);
        assertMapPathValueIs(result, "Principal.0.id", null);
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Principal { name(_equals: \"Axel\") } }");
        assertMapPathValueIs(result, "Principal.#", 1);
        assertMapPathValueIs(result, "Principal.0.name", "Axel");
        assertMapPathValueIs(result, "Principal.0.type", null);
        assertMapPathValueIs(result, "Principal.0.id", null);
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Principal { name(_contains: \"a\", _contains: \"l\", _conj: \"and\") } }");
        assertMapPathValueIs(result, "Principal.#", 4);
        assertMapPathValueIs(result, "Principal.0.name", "All teams");
        assertMapPathValueIs(result, "Principal.1.name", "Axel");
        assertMapPathValueIs(result, "Principal.2.name", "Lukas");
        assertMapPathValueIs(result, "Principal.3.name", "Michael");
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Group(_pageSize: 1) { name }, Principal(_pageSize: 2, _page: 2) { name(_contains: \"i\") } }");
        assertMapPathValueIs(result, "Group.#", 1);
        assertMapPathValueIs(result, "Group.0.name", "All teams");
        assertMapPathValueIs(result, "Principal.#", 2);
        assertMapPathValueIs(result, "Principal.0.name", "Inès");
        assertMapPathValueIs(result, "Principal.1.name", "Kai");
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Group { name, members(_pageSize: 2, _page: 2) { name }}}");
        assertMapPathValueIs(result, "Group.#", 2);
        assertMapPathValueIs(result, "Group.0.name", "All teams");
        assertMapPathValueIs(result, "Group.0.members", new LinkedList<>());
        assertMapPathValueIs(result, "Group.1.name", "Structr Team");
        assertMapPathValueIs(result, "Group.1.members.#", 2);
        assertMapPathValueIs(result, "Group.1.members.0.name", "Christian");
        assertMapPathValueIs(result, "Group.1.members.1.name", "Inès");
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Group { name, members(_sort: \"name\") { name }}}");
        assertMapPathValueIs(result, "Group.#", 2);
        assertMapPathValueIs(result, "Group.0.name", "All teams");
        assertMapPathValueIs(result, "Group.0.members.#", 1);
        assertMapPathValueIs(result, "Group.1.name", "Structr Team");
        assertMapPathValueIs(result, "Group.1.members.#", 9);
        assertMapPathValueIs(result, "Group.1.members.0.name", "Axel");
        assertMapPathValueIs(result, "Group.1.members.1.name", "Christian");
        assertMapPathValueIs(result, "Group.1.members.2.name", "Christian");
        assertMapPathValueIs(result, "Group.1.members.3.name", "Inès");
        assertMapPathValueIs(result, "Group.1.members.4.name", "Kai");
        assertMapPathValueIs(result, "Group.1.members.5.name", "Lukas");
        assertMapPathValueIs(result, "Group.1.members.6.name", "Michael");
        assertMapPathValueIs(result, "Group.1.members.7.name", "Susanne");
        assertMapPathValueIs(result, "Group.1.members.8.name", "Tobias");
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Group { name, members(_sort: \"name\", _desc: true) { name }}}");
        assertMapPathValueIs(result, "Group.#", 2);
        assertMapPathValueIs(result, "Group.0.name", "All teams");
        assertMapPathValueIs(result, "Group.0.members.#", 1);
        assertMapPathValueIs(result, "Group.1.name", "Structr Team");
        assertMapPathValueIs(result, "Group.1.members.#", 9);
        assertMapPathValueIs(result, "Group.1.members.8.name", "Axel");
        assertMapPathValueIs(result, "Group.1.members.7.name", "Christian");
        assertMapPathValueIs(result, "Group.1.members.6.name", "Christian");
        assertMapPathValueIs(result, "Group.1.members.5.name", "Inès");
        assertMapPathValueIs(result, "Group.1.members.4.name", "Kai");
        assertMapPathValueIs(result, "Group.1.members.3.name", "Lukas");
        assertMapPathValueIs(result, "Group.1.members.2.name", "Michael");
        assertMapPathValueIs(result, "Group.1.members.1.name", "Susanne");
        assertMapPathValueIs(result, "Group.1.members.0.name", "Tobias");
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Group { members { name(_contains: \"k\", _contains: \"l\", _conj: \"and\") }}}");
        assertMapPathValueIs(result, "Group.#", 2);
        assertMapPathValueIs(result, "Group.0.name", null);
        assertMapPathValueIs(result, "Group.0.members", new LinkedList<>());
        assertMapPathValueIs(result, "Group.1.members.0.name", "Lukas");
        assertMapPathValueIs(result, "Group.1.members.1", null);
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Group { members { name(_contains: \"k\", _contains: \"l\", _conj: \"or\") }}}");
        assertMapPathValueIs(result, "Group.#", 2);
        assertMapPathValueIs(result, "Group.0.name", null);
        assertMapPathValueIs(result, "Group.0.members", new LinkedList<>());
        assertMapPathValueIs(result, "Group.1.members.#", 4);
        assertMapPathValueIs(result, "Group.1.members.0.name", "Axel");
        assertMapPathValueIs(result, "Group.1.members.1.name", "Kai");
        assertMapPathValueIs(result, "Group.1.members.2.name", "Lukas");
        assertMapPathValueIs(result, "Group.1.members.3.name", "Michael");
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ MailTemplate { id, type, text(_contains: \"2\"), owner(_equals: { name: \"Axel\"}) { name } }}");
        assertMapPathValueIs(result, "MailTemplate.#", 1);
        assertMapPathValueIs(result, "MailTemplate.0.id", templates.get(1).getUuid());
        assertMapPathValueIs(result, "MailTemplate.0.type", "MailTemplate");
        assertMapPathValueIs(result, "MailTemplate.0.text", "MailTemplate2");
        assertMapPathValueIs(result, "MailTemplate.0.name", null);
        assertMapPathValueIs(result, "MailTemplate.0.owner.name", "Axel");
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ MailTemplate(_sort: \"owner.name\") { name, owner { name }}}");
        assertMapPathValueIs(result, "MailTemplate.#", 6);
        assertMapPathValueIs(result, "MailTemplate.0.name", "abcdef");
        assertMapPathValueIs(result, "MailTemplate.0.owner.name", "Axel");
        assertMapPathValueIs(result, "MailTemplate.1.name", "lertdf");
        assertMapPathValueIs(result, "MailTemplate.1.owner.name", "Axel");
        assertMapPathValueIs(result, "MailTemplate.2.name", "asgw");
        assertMapPathValueIs(result, "MailTemplate.2.owner.name", "Christian");
        assertMapPathValueIs(result, "MailTemplate.3.name", "zrtsga");
        assertMapPathValueIs(result, "MailTemplate.3.owner.name", "Christian");
        assertMapPathValueIs(result, "MailTemplate.4.name", "tzegsg");
        assertMapPathValueIs(result, "MailTemplate.4.owner.name", "Inès");
        assertMapPathValueIs(result, "MailTemplate.5.name", "dfjgr");
        assertMapPathValueIs(result, "MailTemplate.5.owner.name", "Kai");
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Group { name, members(_sort: \"name\", _desc: true) { name }}}");
        assertMapPathValueIs(result, "Group.#", 2);
        assertMapPathValueIs(result, "Group.0.name", "All teams");
        assertMapPathValueIs(result, "Group.0.members.#", 1);
        assertMapPathValueIs(result, "Group.1.name", "Structr Team");
        assertMapPathValueIs(result, "Group.1.members.#", 9);
        assertMapPathValueIs(result, "Group.1.members.8.name", "Axel");
        assertMapPathValueIs(result, "Group.1.members.7.name", "Christian");
        assertMapPathValueIs(result, "Group.1.members.6.name", "Christian");
        assertMapPathValueIs(result, "Group.1.members.5.name", "Inès");
        assertMapPathValueIs(result, "Group.1.members.4.name", "Kai");
        assertMapPathValueIs(result, "Group.1.members.3.name", "Lukas");
        assertMapPathValueIs(result, "Group.1.members.2.name", "Michael");
        assertMapPathValueIs(result, "Group.1.members.1.name", "Susanne");
        assertMapPathValueIs(result, "Group.1.members.0.name", "Tobias");
    }
}
Also used : Group(org.structr.core.entity.Group) NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) LinkedList(java.util.LinkedList) MailTemplate(org.structr.core.entity.MailTemplate) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map) Principal(org.structr.core.entity.Principal) Test(org.junit.Test) StructrGraphQLTest(org.structr.rest.common.StructrGraphQLTest)

Aggregations

Principal (org.structr.core.entity.Principal)112 FrameworkException (org.structr.common.error.FrameworkException)68 Tx (org.structr.core.graph.Tx)65 Test (org.junit.Test)41 App (org.structr.core.app.App)31 StructrApp (org.structr.core.app.StructrApp)31 TestOne (org.structr.core.entity.TestOne)16 Group (org.structr.core.entity.Group)14 NodeAttribute (org.structr.core.graph.NodeAttribute)13 PropertyMap (org.structr.core.property.PropertyMap)13 SecurityContext (org.structr.common.SecurityContext)10 LinkedList (java.util.LinkedList)9 Result (org.structr.core.Result)8 User (org.structr.web.entity.User)8 AbstractNode (org.structr.core.entity.AbstractNode)7 SuperUser (org.structr.core.entity.SuperUser)7 StructrUiTest (org.structr.web.StructrUiTest)7 Page (org.structr.web.entity.dom.Page)7 IOException (java.io.IOException)6 List (java.util.List)6