use of org.structr.core.entity.SchemaNode in project structr by structr.
the class AdvancedSearchTest method testSearchWithOwnerAndEnumOnDynamicNodes.
@Test
public void testSearchWithOwnerAndEnumOnDynamicNodes() {
createTestUserType();
try {
// create 3 users
final String user1 = createEntity("/test_users", "{ name: user1 }");
final String user2 = createEntity("/test_users", "{ name: user2 }");
final String user3 = createEntity("/test_users", "{ name: user3 }");
SchemaNode node = null;
// setup schema
try (final Tx tx = app.tx()) {
node = app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "TestType"));
node.setProperty(new StringProperty("_status"), "Enum(one, two, three)");
node.setProperty(new StringProperty("___ui"), "status");
tx.success();
}
// create 9 test entities
final String test1 = createEntity("/test_types", "{ name: test1, owner: " + user1 + ", status: one }");
final String test2 = createEntity("/test_types", "{ name: test2, owner: " + user2 + ", status: two }");
final String test3 = createEntity("/test_types", "{ name: test3, owner: " + user3 + ", status: one }");
final String test4 = createEntity("/test_types", "{ name: test4, owner: " + user1 + ", status: two }");
final String test5 = createEntity("/test_types", "{ name: test5, owner: " + user2 + ", status: three }");
final String test6 = createEntity("/test_types", "{ name: test6, owner: " + user3 + ", status: one }");
final String test7 = createEntity("/test_types", "{ name: test7, owner: " + user1 + ", status: two }");
final String test8 = createEntity("/test_types", "{ name: test8, owner: " + user2 + ", status: three }");
final String test9 = createEntity("/test_types", "{ name: test9, owner: " + user3 + ", status: one }");
// check that all entities are there
RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body("result", hasSize(9)).body("result_count", equalTo(9)).body("result[0].id", equalTo(test1)).body("result[1].id", equalTo(test2)).body("result[2].id", equalTo(test3)).body("result[3].id", equalTo(test4)).body("result[4].id", equalTo(test5)).body("result[5].id", equalTo(test6)).body("result[6].id", equalTo(test7)).body("result[7].id", equalTo(test8)).body("result[8].id", equalTo(test9)).when().get("/test_types/ui?sort=name");
// check entities of user1 are there
RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body("result", hasSize(3)).body("result_count", equalTo(3)).body("result[0].id", equalTo(test1)).body("result[1].id", equalTo(test4)).body("result[2].id", equalTo(test7)).when().get("/test_types/ui?sort=createdDate&owner=" + user1);
// check entities of user2 are there
RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body("result", hasSize(3)).body("result_count", equalTo(3)).body("result[0].id", equalTo(test2)).body("result[1].id", equalTo(test5)).body("result[2].id", equalTo(test8)).when().get("/test_types/ui?sort=createdDate&owner=" + user2);
// check entities of user3 are there
RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body("result", hasSize(3)).body("result_count", equalTo(3)).body("result[0].id", equalTo(test3)).body("result[1].id", equalTo(test6)).body("result[2].id", equalTo(test9)).when().get("/test_types/ui?sort=createdDate&owner=" + user3);
// check entities of user1 with a given enum are there
RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body("result", hasSize(1)).body("result_count", equalTo(1)).body("result[0].id", equalTo(test1)).when().get("/test_types/ui?sort=createdDate&owner=" + user1 + "&status=one");
// check entities of user1 with a given enum are there
RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body("result", hasSize(2)).body("result_count", equalTo(2)).body("result[0].id", equalTo(test4)).body("result[1].id", equalTo(test7)).when().get("/test_types/ui?sort=createdDate&owner=" + user1 + "&status=two");
// check entities of user1 with a given enum are there
RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body("result", hasSize(2)).body("result_count", equalTo(2)).body("result[0].id", equalTo(test5)).body("result[1].id", equalTo(test8)).when().get("/test_types/ui?sort=createdDate&owner=" + user2 + "&status=three");
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
}
use of org.structr.core.entity.SchemaNode in project structr by structr.
the class PermissionResolutionTest method test01SimplePermissionResolution.
@Test
public void test01SimplePermissionResolution() {
SchemaRelationshipNode rel = null;
PropertyKey key = null;
Principal user1 = null;
Class type1 = null;
Class type2 = null;
try (final Tx tx = app.tx()) {
// create a test user
user1 = app.create(Principal.class, "user1");
// create schema setup with permission propagation
final SchemaNode t1 = app.create(SchemaNode.class, "Type1");
final SchemaNode t2 = app.create(SchemaNode.class, "Type2");
rel = app.create(SchemaRelationshipNode.class, new NodeAttribute<>(SchemaRelationshipNode.sourceNode, t1), new NodeAttribute<>(SchemaRelationshipNode.targetNode, t2), new NodeAttribute<>(SchemaRelationshipNode.relationshipType, "RELATED"), new NodeAttribute<>(SchemaRelationshipNode.sourceMultiplicity, "1"), new NodeAttribute<>(SchemaRelationshipNode.targetMultiplicity, "1"), new NodeAttribute<>(SchemaRelationshipNode.sourceJsonName, "source"), new NodeAttribute<>(SchemaRelationshipNode.targetJsonName, "target"));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
Assert.assertNotNull("User should have been created", user1);
// expect object of type 2 to be visible as well
try (final Tx tx = app.tx()) {
type1 = StructrApp.getConfiguration().getNodeEntityClass("Type1");
type2 = StructrApp.getConfiguration().getNodeEntityClass("Type2");
key = StructrApp.key(type1, "target");
Assert.assertNotNull("Node type Type1 should exist.", type1);
Assert.assertNotNull("Node type Type2 should exist.", type2);
Assert.assertNotNull("Property key \"target\" should exist.", key);
final NodeInterface instance1 = app.create(type1, "instance1OfType1");
final NodeInterface instance2 = app.create(type2, "instance1OfType2");
Assert.assertNotNull("Instance of type Type1 should exist", instance1);
Assert.assertNotNull("Instance of type Type2 should exist", instance2);
instance1.setProperty(key, instance2);
// make instance1 visible to user1
instance1.grant(Permission.read, user1);
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
// check access for user1 on instance1
final App userApp = StructrApp.getInstance(SecurityContext.getInstance(user1, AccessMode.Backend));
try (final Tx tx = userApp.tx()) {
Assert.assertNotNull("User1 should be able to find instance of type Type1", userApp.nodeQuery(type1).getFirst());
Assert.assertNull("User1 should NOT be able to find instance of type Type2", userApp.nodeQuery(type2).getFirst());
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
// because the resolution direction is wrong.
try (final Tx tx = app.tx()) {
rel.setProperty(SchemaRelationshipNode.permissionPropagation, Direction.In);
rel.setProperty(SchemaRelationshipNode.readPropagation, Propagation.Add);
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
// check access for user1 on instance1
try (final Tx tx = userApp.tx()) {
Assert.assertNotNull("User1 should be able to find instance of type Type1", userApp.nodeQuery(type1).getFirst());
Assert.assertNull("User1 should NOT be able to find instance of type Type2", userApp.nodeQuery(type2).getFirst());
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
// because the resolution direction is correct
try (final Tx tx = app.tx()) {
rel.setProperty(SchemaRelationshipNode.permissionPropagation, Direction.Out);
rel.setProperty(SchemaRelationshipNode.readPropagation, Propagation.Add);
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
// check access for user1 on instance1
try (final Tx tx = userApp.tx()) {
Assert.assertNotNull("User1 should be able to find instance of type Type1", userApp.nodeQuery(type1).getFirst());
Assert.assertNotNull("User1 should be able to find instance of type Type2", userApp.nodeQuery(type2).getFirst());
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
// because both resolution directions are enabled
try (final Tx tx = app.tx()) {
rel.setProperty(SchemaRelationshipNode.permissionPropagation, Direction.Both);
rel.setProperty(SchemaRelationshipNode.readPropagation, Propagation.Add);
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
// check access for user1 on instance1
try (final Tx tx = userApp.tx()) {
Assert.assertNotNull("User1 should be able to find instance of type Type1", userApp.nodeQuery(type1).getFirst());
Assert.assertNotNull("User1 should be able to find instance of type Type2", userApp.nodeQuery(type2).getFirst());
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
// object invisible again.
try (final Tx tx = app.tx()) {
rel.setProperty(SchemaRelationshipNode.permissionPropagation, Direction.None);
rel.setProperty(SchemaRelationshipNode.readPropagation, Propagation.Add);
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
// check access for user1 on instance1
try (final Tx tx = userApp.tx()) {
Assert.assertNotNull("User1 should be able to find instance of type Type1", userApp.nodeQuery(type1).getFirst());
Assert.assertNull("User1 should NOT be able to find instance of type Type2", userApp.nodeQuery(type2).getFirst());
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
}
use of org.structr.core.entity.SchemaNode in project structr by structr.
the class SystemTest method testFlawedParallelInstantiation.
/**
* disabled, failing test to check for (existing, confirmed) flaw in parallel node instantiation)
*/
@Test
public void testFlawedParallelInstantiation() {
final int nodeCount = 1000;
SchemaNode createTestType = null;
// setup: create dynamic type with onCreate() method
try (final Tx tx = app.tx()) {
createTestType = createTestNode(SchemaNode.class, "CreateTest");
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
Class testType = StructrApp.getConfiguration().getNodeEntityClass("CreateTest");
assertNotNull("Type CreateTest should have been created", testType);
// second step: create 1000 test nodes
try (final Tx tx = app.tx()) {
createTestNodes(testType, nodeCount);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
try (final Tx tx = app.tx()) {
createTestType.setProperty(new StringProperty("_testCount"), "Integer");
createTestType.setProperty(new StringProperty("___onCreate"), "set(this, 'testCount', size(find('CreateTest')))");
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
testType = StructrApp.getConfiguration().getNodeEntityClass("CreateTest");
NodeInterface node = null;
// third step: create a single node in a separate transaction
try (final Tx tx = app.tx()) {
node = createTestNode(testType, "Tester");
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// fourth step: check property value
try (final Tx tx = app.tx()) {
final Integer testCount = node.getProperty(new IntProperty("testCount"));
assertEquals("Invalid node count, check parallel instantiation!", (int) nodeCount + 1, (int) testCount);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
use of org.structr.core.entity.SchemaNode in project structr by structr.
the class CustomPermissionQueriesTest method test02SimplePermissionResolutionWrite.
@Test
public void test02SimplePermissionResolutionWrite() {
final Class<Principal> principalType = StructrApp.getConfiguration().getNodeEntityClass("Principal");
Principal user1 = null;
Class type1 = null;
try (final Tx tx = app.tx()) {
// create a test user
user1 = app.create(principalType, "user1");
final SchemaNode t1 = app.create(SchemaNode.class, "Type1");
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
Assert.assertNotNull("User should have been created", user1);
try (final Tx tx = app.tx()) {
type1 = StructrApp.getConfiguration().getNodeEntityClass("Type1");
Assert.assertNotNull("Node type Type1 should exist.", type1);
final NodeInterface instance1 = app.create(type1, "instance1OfType1");
Assert.assertNotNull("Instance of type Type1 should exist", instance1);
// make instance1 visible to user1
instance1.grant(Permission.read, user1);
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
// check access for user1 on instance1
final App userApp = StructrApp.getInstance(SecurityContext.getInstance(user1, AccessMode.Backend));
try (final Tx tx = userApp.tx()) {
userApp.nodeQuery(type1).getFirst().setProperty(GraphObject.visibleToPublicUsers, true);
tx.success();
} catch (FrameworkException fex) {
Assert.assertEquals("User1 should NOT be able to modify instance of type Type1", 403, fex.getStatus());
}
// set custom permission query on user
try (final Tx tx = userApp.tx()) {
// query returns always true if user exists
user1.setProperty(StructrApp.key(Principal.class, "customPermissionQueryWrite"), "MATCH (p:Principal {id: {principalUuid}}) RETURN p IS NOT NULL");
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
// check access for user1 on instance1
try (final Tx tx = userApp.tx()) {
userApp.nodeQuery(type1).getFirst().setProperty(GraphObject.visibleToPublicUsers, true);
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
// set custom permission query on user
try (final Tx tx = userApp.tx()) {
// query returns always false if user exists
user1.setProperty(StructrApp.key(Principal.class, "customPermissionQueryRead"), "MATCH (p:Principal {id: {principalUuid}}) RETURN p IS NULL");
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
// check access for user1 on instance1
try (final Tx tx = userApp.tx()) {
userApp.nodeQuery(type1).getFirst().setProperty(GraphObject.visibleToPublicUsers, true);
tx.success();
} catch (FrameworkException fex) {
Assert.assertEquals("User1 should NOT be able to modify instance of type Type1", 403, fex.getStatus());
}
}
use of org.structr.core.entity.SchemaNode in project structr by structr.
the class StructrRelationshipTypeDefinition method deserialize.
@Override
void deserialize(final SchemaRelationshipNode schemaNode) {
super.deserialize(schemaNode);
final SchemaNode sourceNode = schemaNode.getProperty(SchemaRelationshipNode.sourceNode);
final SchemaNode targetNode = schemaNode.getProperty(SchemaRelationshipNode.targetNode);
final String sourceNodeType = sourceNode.getClassName();
final String targetNodeType = targetNode.getClassName();
this.sourceType = root.getId().resolve("definitions/" + sourceNodeType);
this.targetType = root.getId().resolve("definitions/" + targetNodeType);
this.relationshipType = schemaNode.getProperty(SchemaRelationshipNode.relationshipType);
this.sourcePropertyName = schemaNode.getProperty(SchemaRelationshipNode.sourceJsonName);
this.targetPropertyName = schemaNode.getProperty(SchemaRelationshipNode.targetJsonName);
this.aclResolution = schemaNode.getProperty(SchemaRelationshipNode.permissionPropagation).name();
this.aclReadMask = schemaNode.getProperty(SchemaRelationshipNode.readPropagation).name();
this.aclWriteMask = schemaNode.getProperty(SchemaRelationshipNode.writePropagation).name();
this.aclDeleteMask = schemaNode.getProperty(SchemaRelationshipNode.deletePropagation).name();
this.aclAccessControlMask = schemaNode.getProperty(SchemaRelationshipNode.accessControlPropagation).name();
this.aclHiddenProperties = schemaNode.getProperty(SchemaRelationshipNode.propertyMask);
if (sourcePropertyName == null) {
sourcePropertyName = schemaNode.getPropertyName(sourceNodeType, root.getExistingPropertyNames(), false);
}
if (targetPropertyName == null) {
targetPropertyName = schemaNode.getPropertyName(targetNodeType, root.getExistingPropertyNames(), true);
}
final Long cascadingDeleteFlag = schemaNode.getProperty(SchemaRelationshipNode.cascadingDeleteFlag);
if (cascadingDeleteFlag != null) {
this.cascadingDelete = getCascadingString(cascadingDeleteFlag.intValue());
}
final Long cascadingCreateFlag = schemaNode.getProperty(SchemaRelationshipNode.autocreationFlag);
if (cascadingCreateFlag != null) {
this.cascadingCreate = getCascadingString(cascadingCreateFlag.intValue());
}
final String sourceMultiplicity = schemaNode.getProperty(SchemaRelationshipNode.sourceMultiplicity);
final String targetMultiplicity = schemaNode.getProperty(SchemaRelationshipNode.targetMultiplicity);
if ("1".equals(sourceMultiplicity)) {
if ("1".equals(targetMultiplicity)) {
this.cardinality = Cardinality.OneToOne;
} else {
this.cardinality = Cardinality.OneToMany;
}
} else {
if ("1".equals(targetMultiplicity)) {
this.cardinality = Cardinality.ManyToOne;
} else {
this.cardinality = Cardinality.ManyToMany;
}
}
}
Aggregations