use of org.structr.core.entity.SchemaRelationshipNode in project structr by structr.
the class StructrSchemaPath method getDirectoryStream.
@Override
public DirectoryStream<Path> getDirectoryStream(final DirectoryStream.Filter<? super Path> filter) {
return new DirectoryStream() {
boolean closed = false;
@Override
public Iterator iterator() {
if (!closed) {
final App app = StructrApp.getInstance(fs.getSecurityContext());
final List<StructrPath> nodes = new LinkedList<>();
try (final Tx tx = app.tx()) {
for (final SchemaNode schemaNode : app.nodeQuery(SchemaNode.class).getAsList()) {
nodes.add(new StructrSchemaNodePath(fs, StructrSchemaPath.this, schemaNode.getName()));
}
for (final SchemaRelationshipNode rel : app.nodeQuery(SchemaRelationshipNode.class).getAsList()) {
nodes.add(new StructrSchemaNodePath(fs, StructrSchemaPath.this, rel.getName()));
}
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
return nodes.iterator();
}
return Collections.emptyIterator();
}
@Override
public void close() throws IOException {
closed = true;
}
};
}
use of org.structr.core.entity.SchemaRelationshipNode 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.SchemaRelationshipNode in project structr by structr.
the class StructrRelationshipTypeDefinition method createSchemaNode.
@Override
SchemaRelationshipNode createSchemaNode(final App app, final PropertyMap createProperties) throws FrameworkException {
final PropertyMap properties = new PropertyMap();
SchemaRelationshipNode _schemaNode = app.nodeQuery(SchemaRelationshipNode.class).andName(getName()).getFirst();
if (_schemaNode == null) {
_schemaNode = app.create(SchemaRelationshipNode.class, getName());
}
properties.put(SchemaRelationshipNode.relationshipType, getRelationship());
properties.put(SchemaRelationshipNode.sourceJsonName, sourcePropertyName);
properties.put(SchemaRelationshipNode.targetJsonName, targetPropertyName);
properties.put(SchemaRelationshipNode.sourceMultiplicity, getSourceMultiplicity(cardinality));
properties.put(SchemaRelationshipNode.targetMultiplicity, getTargetMultiplicity(cardinality));
properties.put(SchemaRelationshipNode.cascadingDeleteFlag, getCascadingFlag(cascadingDelete));
properties.put(SchemaRelationshipNode.autocreationFlag, getCascadingFlag(cascadingCreate));
if (aclResolution != null) {
properties.put(SchemaRelationshipNode.permissionPropagation, SchemaRelationshipNode.Direction.valueOf(aclResolution));
}
if (aclReadMask != null) {
properties.put(SchemaRelationshipNode.readPropagation, SchemaRelationshipNode.Propagation.valueOf(aclReadMask));
}
if (aclWriteMask != null) {
properties.put(SchemaRelationshipNode.writePropagation, SchemaRelationshipNode.Propagation.valueOf(aclWriteMask));
}
if (aclDeleteMask != null) {
properties.put(SchemaRelationshipNode.deletePropagation, SchemaRelationshipNode.Propagation.valueOf(aclDeleteMask));
}
if (aclAccessControlMask != null) {
properties.put(SchemaRelationshipNode.accessControlPropagation, SchemaRelationshipNode.Propagation.valueOf(aclAccessControlMask));
}
if (aclHiddenProperties != null) {
properties.put(SchemaRelationshipNode.propertyMask, aclHiddenProperties);
}
if (root != null) {
if (SchemaService.DynamicSchemaRootURI.equals(root.getId())) {
properties.put(SchemaRelationshipNode.isPartOfBuiltInSchema, true);
}
}
_schemaNode.setProperties(SecurityContext.getSuperUserInstance(), properties);
return _schemaNode;
}
use of org.structr.core.entity.SchemaRelationshipNode in project structr by structr.
the class StructrSchema method replaceDatabaseSchema.
/**
* Replaces the current Structr schema with the given new schema. This
* method is the reverse of createFromDatabase above.
*
* @param app
* @param newSchema the new schema to replace the current Structr schema
*
* @throws FrameworkException
* @throws URISyntaxException
*/
public static void replaceDatabaseSchema(final App app, final JsonSchema newSchema) throws FrameworkException, URISyntaxException {
Services.getInstance().setOverridingSchemaTypesAllowed(true);
try (final Tx tx = app.tx()) {
for (final SchemaRelationshipNode schemaRelationship : app.nodeQuery(SchemaRelationshipNode.class).getAsList()) {
app.delete(schemaRelationship);
}
for (final SchemaNode schemaNode : app.nodeQuery(SchemaNode.class).getAsList()) {
app.delete(schemaNode);
}
for (final SchemaMethod schemaMethod : app.nodeQuery(SchemaMethod.class).getAsList()) {
app.delete(schemaMethod);
}
for (final SchemaMethodParameter schemaMethodParameter : app.nodeQuery(SchemaMethodParameter.class).getAsList()) {
app.delete(schemaMethodParameter);
}
for (final SchemaProperty schemaProperty : app.nodeQuery(SchemaProperty.class).getAsList()) {
app.delete(schemaProperty);
}
for (final SchemaView schemaView : app.nodeQuery(SchemaView.class).getAsList()) {
app.delete(schemaView);
}
newSchema.createDatabaseSchema(app, JsonSchema.ImportMode.replace);
tx.success();
}
}
use of org.structr.core.entity.SchemaRelationshipNode in project structr by structr.
the class SchemaTest method test00DeleteSchemaRelationshipInView.
@Test
public void test00DeleteSchemaRelationshipInView() {
SchemaRelationshipNode rel = null;
try (final Tx tx = app.tx()) {
// create source and target node
final SchemaNode fooNode = app.create(SchemaNode.class, "Foo");
final SchemaNode barNode = app.create(SchemaNode.class, "Bar");
// create relationship
rel = app.create(SchemaRelationshipNode.class, new NodeAttribute<>(SchemaRelationshipNode.sourceNode, fooNode), new NodeAttribute<>(SchemaRelationshipNode.targetNode, barNode), new NodeAttribute<>(SchemaRelationshipNode.relationshipType, "narf"));
// create "public" view that contains the related property
app.create(SchemaView.class, new NodeAttribute<>(SchemaView.name, "public"), new NodeAttribute<>(SchemaView.schemaNode, fooNode), new NodeAttribute<>(SchemaView.nonGraphProperties, "type, id, narfBars"));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
app.delete(rel);
tx.success();
} catch (Throwable t) {
// deletion of relationship should not fail
logger.warn("", t);
fail("Unexpected exception");
}
}
Aggregations