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");
}
}
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.");
}
}
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;
}
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.");
}
}
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());
}
}
Aggregations