use of org.structr.core.entity.Principal 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);
}
use of org.structr.core.entity.Principal 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;
}
});
}
use of org.structr.core.entity.Principal 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);
}
use of org.structr.core.entity.Principal 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");
}
}
use of org.structr.core.entity.Principal 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.");
}
}
Aggregations