TestUser user1 = null;
TestUser user2 = null;
TestOne t1 = null;
Class type = TestOne.class;
final App superUserApp = StructrApp.getInstance();
try (final Tx tx = app.tx()) {
List<TestUser> users = createTestNodes(TestUser.class, 2);
user1 = (TestUser) users.get(0);
user1.setProperty(AbstractNode.name, "user1");
user2 = (TestUser) users.get(1);
user2.setProperty(AbstractNode.name, "user2");
t1 = createTestNode(TestOne.class);
t1.setProperty(AbstractNode.owner, user1);
tx.success();
} catch (FrameworkException ex) {
logger.log(Level.SEVERE, ex.toString());
}
try (final Tx tx = app.tx()) {
assertEquals(user1, t1.getProperty(AbstractNode.owner));
// Switch user context to user1
final App user1App = StructrApp.getInstance(SecurityContext.getInstance(user1, AccessMode.Backend));
// Check if user1 can see t1
assertEquals(t1, user1App.nodeQuery(type, false).getFirst());
}
try (final Tx tx = app.tx()) {
// As superuser, make another user the owner
t1.setProperty(AbstractNode.owner, user2);
tx.success();
} catch (FrameworkException ex) {
logger.log(Level.SEVERE, ex.toString());
}
try (final Tx tx = app.tx()) {
// Switch user context to user2
final App user2App = StructrApp.getInstance(SecurityContext.getInstance(user2, AccessMode.Backend));
// Check if user2 can see t1
assertEquals(t1, user2App.nodeQuery(type, false).getFirst());
// Check if user2 is owner of t1
assertEquals(user2, t1.getProperty(AbstractNode.owner));
}