Search in sources :

Example 1 with User

use of org.structr.web.entity.User in project structr by structr.

the class DeploymentTest method test21ExportGrants.

@Test
public void test21ExportGrants() {
    Principal user1 = null;
    Principal user2 = null;
    try (final Tx tx = app.tx()) {
        user1 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user1"));
        user2 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user2"));
        tx.success();
    } catch (FrameworkException ex) {
        fail("Unexpected exception.");
    }
    Assert.assertNotNull("User was not created, test cannot continue", user1);
    Assert.assertNotNull("User was not created, test cannot continue", user2);
    // setup
    final SecurityContext context1 = SecurityContext.getInstance(user1, AccessMode.Backend);
    final App app1 = StructrApp.getInstance(context1);
    try (final Tx tx = app1.tx()) {
        final Page page = Page.createNewPage(context1, "test21");
        final Html html = createElement(page, page, "html");
        final Head head = createElement(page, html, "head");
        createElement(page, head, "title", "test21");
        final Body body = createElement(page, html, "body");
        final Div div1 = createElement(page, body, "div");
        final Content content = createContent(page, div1, "<b>Test</b>");
        content.setProperty(StructrApp.key(Content.class, "contentType"), "text/html");
        // create grants
        page.grant(Permission.read, user2);
        div1.grant(Permission.read, user2);
        content.grant(Permission.read, user2);
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception.");
    }
    // test
    compare(calculateHash(), true, false);
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) NodeAttribute(org.structr.core.graph.NodeAttribute) Head(org.structr.web.entity.html.Head) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Html(org.structr.web.entity.html.Html) Page(org.structr.web.entity.dom.Page) Div(org.structr.web.entity.html.Div) Content(org.structr.web.entity.dom.Content) SecurityContext(org.structr.common.SecurityContext) Body(org.structr.web.entity.html.Body) Principal(org.structr.core.entity.Principal) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 2 with User

use of org.structr.web.entity.User in project structr by structr.

the class DeploymentTest method test23FileOwnershipAndGrants.

@Test
public void test23FileOwnershipAndGrants() {
    Principal user1 = null;
    Principal user2 = null;
    try (final Tx tx = app.tx()) {
        user1 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user1"));
        user2 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user2"));
        tx.success();
    } catch (FrameworkException ex) {
        fail("Unexpected exception.");
    }
    Assert.assertNotNull("User was not created, test cannot continue", user1);
    Assert.assertNotNull("User was not created, test cannot continue", user2);
    // setup
    try (final Tx tx = app.tx()) {
        // create some files and folders
        final Folder folder1 = app.create(Folder.class, new NodeAttribute<>(Folder.name, "Folder1"), new NodeAttribute<>(StructrApp.key(Folder.class, "includeInFrontendExport"), true));
        final Folder folder2 = app.create(Folder.class, new NodeAttribute<>(Folder.name, "Folder2"), new NodeAttribute<>(StructrApp.key(Folder.class, "parent"), folder1));
        final File file1 = FileHelper.createFile(securityContext, "test".getBytes(), "text/plain", File.class, "test1.txt");
        final File file2 = FileHelper.createFile(securityContext, "test".getBytes(), "text/plain", File.class, "test2.txt");
        file1.setParent(folder2);
        file2.setParent(folder2);
        folder1.setProperty(Folder.owner, user1);
        folder1.grant(Permission.read, user2);
        folder2.setProperty(Folder.owner, user2);
        folder2.grant(Permission.write, user1);
        file1.setProperty(File.owner, user1);
        file2.setProperty(File.owner, user2);
        file1.setProperty(Folder.owner, user1);
        file1.grant(Permission.read, user2);
        file2.setProperty(Folder.owner, user2);
        file2.grant(Permission.write, user1);
        tx.success();
    } catch (IOException | FrameworkException fex) {
        fail("Unexpected exception.");
    }
    // test
    doImportExportRoundtrip(true, true, new Function() {

        @Override
        public Object apply(Object t) {
            try (final Tx tx = app.tx()) {
                createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user1"));
                createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user2"));
                tx.success();
            } catch (FrameworkException ex) {
                fail("Unexpected exception.");
            }
            return null;
        }
    });
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) IOException(java.io.IOException) Folder(org.structr.web.entity.Folder) Function(java.util.function.Function) GraphObject(org.structr.core.GraphObject) File(org.structr.web.entity.File) Principal(org.structr.core.entity.Principal) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 3 with User

use of org.structr.web.entity.User in project structr by structr.

the class DeploymentTest method test20ExportOwnership.

@Test
public void test20ExportOwnership() {
    Principal user1 = null;
    Principal user2 = null;
    try (final Tx tx = app.tx()) {
        user1 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user1"));
        user2 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user2"));
        tx.success();
    } catch (FrameworkException ex) {
        fail("Unexpected exception.");
    }
    Assert.assertNotNull("User was not created, test cannot continue", user1);
    Assert.assertNotNull("User was not created, test cannot continue", user2);
    // setup
    final SecurityContext context1 = SecurityContext.getInstance(user1, AccessMode.Backend);
    final App app1 = StructrApp.getInstance(context1);
    try (final Tx tx = app1.tx()) {
        final Page page = Page.createNewPage(context1, "test20");
        final Html html = createElement(page, page, "html");
        final Head head = createElement(page, html, "head");
        createElement(page, head, "title", "test20");
        final Body body = createElement(page, html, "body");
        final Div div1 = createElement(page, body, "div");
        final Content content = createContent(page, div1, "<b>Test</b>");
        content.setProperty(StructrApp.key(Content.class, "contentType"), "text/html");
        // set owner to different user
        div1.setProperty(AbstractNode.owner, user2);
        content.setProperty(AbstractNode.owner, user2);
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception.");
    }
    // test
    compare(calculateHash(), true, false);
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) NodeAttribute(org.structr.core.graph.NodeAttribute) Head(org.structr.web.entity.html.Head) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Html(org.structr.web.entity.html.Html) Page(org.structr.web.entity.dom.Page) Div(org.structr.web.entity.html.Div) Content(org.structr.web.entity.dom.Content) SecurityContext(org.structr.common.SecurityContext) Body(org.structr.web.entity.html.Body) Principal(org.structr.core.entity.Principal) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 4 with User

use of org.structr.web.entity.User in project structr by structr.

the class UserSelfRegistrationTest method testUserSelfRegistrationWithRedirect.

@Test
public void testUserSelfRegistrationWithRedirect() {
    // since we cannot test the mail confirmation workflow, we just disable sending an e-mail
    Settings.SmtpTesting.setValue(true);
    // enable self-registration and auto-login
    Settings.RestUserAutocreate.setValue(true);
    Settings.RestUserAutologin.setValue(true);
    final SessionFilter sessionFilter = new SessionFilter();
    final String eMail = "test@structr.com";
    String id = null;
    String confKey = null;
    // switch to REST servlet
    RestAssured.basePath = restUrl;
    grant("_registration", UiAuthenticator.NON_AUTH_USER_POST, true);
    grant("_login", UiAuthenticator.NON_AUTH_USER_POST, false);
    // verify self registration
    RestAssured.given().filter(sessionFilter).body("{ name: '" + eMail + "',  eMail: '" + eMail + "' }").expect().statusCode(201).when().post("/registration");
    try (final Tx tx = app.tx()) {
        final User user = app.nodeQuery(User.class).getFirst();
        assertNotNull("User was not created", user);
        // store ID for later user
        id = user.getProperty(StructrApp.key(User.class, "id"));
        confKey = user.getProperty(StructrApp.key(User.class, "confirmationKey"));
        assertNotNull("Confirmation key was not set", confKey);
        tx.success();
    } catch (FrameworkException t) {
        fail("Unexpected exception.");
    }
    // create redirect page
    try (final Tx tx = app.tx()) {
        makeVisible(Page.createSimplePage(securityContext, "error"), true);
        makeVisible(Page.createSimplePage(securityContext, "success"), false);
        tx.success();
    } catch (FrameworkException fex) {
    }
    // switch to HTML servlet
    RestAssured.basePath = htmlUrl;
    // expect 404 Not Found when logging in because Jetty or
    // RestAssured don't preserve the session ID
    RestAssured.given().filter(sessionFilter).param(HtmlServlet.CONFIRM_KEY_KEY, confKey).param(HtmlServlet.TARGET_PAGE_KEY, "success").expect().statusCode(200).body("html.head.title", Matchers.equalTo("Success")).body("html.body.h1", Matchers.equalTo("Success")).body("html.body.div", Matchers.equalTo("Initial body text")).when().get(HtmlServlet.CONFIRM_REGISTRATION_PAGE);
    // verify that the user has no confirmation key
    try (final Tx tx = app.tx()) {
        final User user = app.nodeQuery(User.class).getFirst();
        assertNotNull("User was not created", user);
        assertNull("Confirmation key was set after confirmation", user.getProperty(StructrApp.key(User.class, "confirmationKey")));
        final String[] sessionIds = user.getProperty(StructrApp.key(User.class, "sessionIds"));
        Assert.assertEquals("Invalid number of sessions after user confirmation", 1, sessionIds.length);
        Assert.assertEquals("Invalid session ID after user confirmation", StringUtils.substringBeforeLast(sessionFilter.getSessionId(), "."), sessionIds[0]);
        tx.success();
    } catch (FrameworkException t) {
        fail("Unexpected exception.");
    }
}
Also used : User(org.structr.web.entity.User) SessionFilter(com.jayway.restassured.filter.session.SessionFilter) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 5 with User

use of org.structr.web.entity.User 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)

Aggregations

User (org.structr.web.entity.User)32 Tx (org.structr.core.graph.Tx)27 FrameworkException (org.structr.common.error.FrameworkException)23 Test (org.junit.Test)21 StructrUiTest (org.structr.web.StructrUiTest)16 PropertyMap (org.structr.core.property.PropertyMap)14 Principal (org.structr.core.entity.Principal)10 App (org.structr.core.app.App)8 StructrApp (org.structr.core.app.StructrApp)8 NodeAttribute (org.structr.core.graph.NodeAttribute)7 SecurityContext (org.structr.common.SecurityContext)5 Folder (org.structr.web.entity.Folder)5 SchemaMethod (org.structr.core.entity.SchemaMethod)4 SchemaNode (org.structr.core.entity.SchemaNode)4 Page (org.structr.web.entity.dom.Page)4 GraphObject (org.structr.core.GraphObject)3 RenderContext (org.structr.web.common.RenderContext)3 File (org.structr.web.entity.File)3 Body (org.structr.web.entity.html.Body)3 Div (org.structr.web.entity.html.Div)3