use of org.structr.core.entity.SchemaNode in project structr by structr.
the class ExtendNotionPropertyWithUuid method handleMigration.
@Override
public void handleMigration(final ErrorToken errorToken) throws FrameworkException {
final Object messageObject = errorToken.getDetail();
if (messageObject != null) {
final String message = (String) messageObject;
if (message.startsWith("Invalid notion property expression for property ") && message.endsWith(".")) {
if (errorToken instanceof InvalidPropertySchemaToken) {
final App app = StructrApp.getInstance();
final InvalidPropertySchemaToken token = (InvalidPropertySchemaToken) errorToken;
final String typeName = token.getType();
final String propertyName = token.getProperty();
final SchemaNode type = app.nodeQuery(SchemaNode.class).andName(typeName).getFirst();
if (type != null) {
final SchemaProperty property = app.nodeQuery(SchemaProperty.class).and(SchemaProperty.schemaNode, type).and(SchemaProperty.name, propertyName).getFirst();
if (property != null) {
// load format property
final String format = property.getProperty(SchemaProperty.format);
// store corrected version of format property
property.setProperty(SchemaProperty.format, format + ", id");
}
}
}
}
}
}
use of org.structr.core.entity.SchemaNode in project structr by structr.
the class CustomPermissionQueriesTest method test01SimplePermissionResolutionRead.
@Test
public void test01SimplePermissionResolutionRead() {
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);
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.assertNull("User1 should NOT be able to find instance of type Type1", userApp.nodeQuery(type1).getFirst());
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 true if user exists
user1.setProperty(StructrApp.key(Principal.class, "customPermissionQueryRead"), "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()) {
Assert.assertNotNull("User1 should be able to find instance of type Type1", userApp.nodeQuery(type1).getFirst());
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()) {
Assert.assertNull("User1 should NOT be able to find instance of type Type1", userApp.nodeQuery(type1).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 ScriptingTest method testGrantViaScripting.
@Test
public void testGrantViaScripting() {
Settings.LogSchemaOutput.setValue(true);
// setup phase: create schema nodes
try (final Tx tx = app.tx()) {
// create two nodes and associate them with each other
final SchemaNode sourceNode = createTestNode(SchemaNode.class, "Source");
final SchemaMethod method = createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest01"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.grant(Structr.find('Principal')[0], 'read', 'write'); }"));
sourceNode.setProperty(SchemaNode.schemaMethods, Arrays.asList(new SchemaMethod[] { method }));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
final ConfigurationProvider config = StructrApp.getConfiguration();
final Class sourceType = config.getNodeEntityClass("Source");
Principal testUser = null;
// create test node as superuser
try (final Tx tx = app.tx()) {
app.create(sourceType);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// create test user
try (final Tx tx = app.tx()) {
testUser = app.create(Principal.class, new NodeAttribute<>(Principal.name, "test"), new NodeAttribute<>(StructrApp.key(Principal.class, "password"), "test"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
final App userApp = StructrApp.getInstance(SecurityContext.getInstance(testUser, AccessMode.Backend));
// first test without grant, expect no test object to be found using the user context
try (final Tx tx = userApp.tx()) {
assertEquals("Invalid grant() scripting result", 0, userApp.nodeQuery(sourceType).getAsList().size());
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// grant read access to test user
try (final Tx tx = app.tx()) {
app.nodeQuery(sourceType).getFirst().invokeMethod("doTest01", Collections.EMPTY_MAP, true);
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
// first test without grant, expect no test object to be found using the user context
try (final Tx tx = userApp.tx()) {
assertEquals("Invalid grant() scripting result", 1, userApp.nodeQuery(sourceType).getAsList().size());
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 SchemaTest method testJavaSchemaMethod.
@Test
public void testJavaSchemaMethod() {
final Class groupType = StructrApp.getConfiguration().getNodeEntityClass("Group");
NodeInterface group = null;
try (final Tx tx = app.tx()) {
final SchemaNode schemaNode = app.nodeQuery(SchemaNode.class).andName("Group").getFirst();
assertNotNull("Schema node Group should exist", schemaNode);
final StringBuilder source = new StringBuilder();
source.append("final Set<String> test = new LinkedHashSet<>();\n");
source.append("\t\ttest.add(\"one\");\n");
source.append("\t\ttest.add(\"two\");\n");
source.append("\t\ttest.add(\"three\");\n");
source.append("\t\treturn test;\n\n");
app.create(SchemaMethod.class, new NodeAttribute<>(SchemaMethod.schemaNode, schemaNode), new NodeAttribute<>(SchemaMethod.name, "testJavaMethod"), new NodeAttribute<>(SchemaMethod.source, source.toString()), new NodeAttribute<>(SchemaMethod.codeType, "java"));
group = app.create(groupType, "test");
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
final Object result = Actions.execute(securityContext, null, "${first(find('Group')).testJavaMethod}", "test");
assertTrue("Result should be of type Set", result instanceof Set);
final Set<String> set = (Set) result;
final String[] array = set.toArray(new String[0]);
assertEquals("Invalid Java schema method result", "one", array[0]);
assertEquals("Invalid Java schema method result", "two", array[1]);
assertEquals("Invalid Java schema method result", "three", array[2]);
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
}
use of org.structr.core.entity.SchemaNode in project structr by structr.
the class PropertyTest method testNotionProperty.
// ----- notion property tests -----
/**
* This test creates a new type "Test" with different Notion properties.
*/
@Test
public void testNotionProperty() {
// schema setup
try (final Tx tx = app.tx()) {
final SchemaNode test = app.create(SchemaNode.class, new NodeAttribute<>(SchemaNode.name, "Test"));
app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.name, "ownerName"), new NodeAttribute<>(SchemaProperty.propertyType, "Notion"), new NodeAttribute<>(SchemaProperty.format, "owner, name"), new NodeAttribute<>(SchemaProperty.schemaNode, test));
app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.name, "ownerEmail"), new NodeAttribute<>(SchemaProperty.propertyType, "Notion"), new NodeAttribute<>(SchemaProperty.format, "owner, eMail"), new NodeAttribute<>(SchemaProperty.schemaNode, test));
app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.name, "ownerPrincipalEmail"), new NodeAttribute<>(SchemaProperty.propertyType, "Notion"), new NodeAttribute<>(SchemaProperty.format, "owner, Principal.eMail"), new NodeAttribute<>(SchemaProperty.schemaNode, test));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
}
Aggregations