use of org.structr.core.entity.SchemaMethod 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.SchemaMethod in project structr by structr.
the class StructrSchemaMethodPath method delete.
@Override
public void delete() throws IOException {
final SchemaMethod schemaMethod = getSchemaMethodNode();
if (schemaMethod != null) {
final App app = StructrApp.getInstance(fs.getSecurityContext());
try (final Tx tx = app.tx()) {
app.delete(schemaMethod);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
} else {
throw new NoSuchFileException(name);
}
}
use of org.structr.core.entity.SchemaMethod in project structr by structr.
the class StructrSchemaMethodPath method newFileChannel.
@Override
public FileChannel newFileChannel(final Set<? extends OpenOption> options, final FileAttribute<?>... attrs) throws IOException {
// Possible open options are: CREATE, READ, WRITE, TRUNCATE_EXISTING
// The filesystem code will first try to fetch the attributes of a file, then call this
// method with the CREATE and WRITE OPTIONS (and an optional TRUNCATE_EXISTING)
SchemaMethod method = getSchemaMethodNode();
final boolean read = options.contains(StandardOpenOption.READ);
final boolean create = options.contains(StandardOpenOption.CREATE);
final boolean createNew = options.contains(StandardOpenOption.CREATE_NEW);
final boolean write = options.contains(StandardOpenOption.WRITE);
final boolean truncate = options.contains(StandardOpenOption.TRUNCATE_EXISTING);
final boolean append = options.contains(StandardOpenOption.APPEND);
// creation of a new file requested (=> create a new schema method)
if (create || createNew) {
// if CREATE_NEW, file must not exist, otherwise an error should be thrown
if (createNew && method != null) {
throw new java.nio.file.FileAlreadyExistsException(toString());
}
final App app = StructrApp.getInstance(fs.getSecurityContext());
try (final Tx tx = app.tx()) {
// create a new schema method with an empty source string
method = app.create(SchemaMethod.class, new NodeAttribute<>(SchemaMethod.schemaNode, schemaNode), new NodeAttribute<>(SchemaMethod.virtualFileName, name), new NodeAttribute<>(AbstractNode.name, normalizeFileNameForJavaIdentifier(name)), new NodeAttribute<>(SchemaMethod.source, ""));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
}
return new StructrPropertyValueChannel(fs.getSecurityContext(), method, SchemaMethod.source, truncate, append);
}
use of org.structr.core.entity.SchemaMethod in project structr by structr.
the class StructrSchemaMethodPath method getSchemaMethodNode.
public SchemaMethod getSchemaMethodNode() {
final App app = StructrApp.getInstance(fs.getSecurityContext());
SchemaMethod method = null;
try (final Tx tx = app.tx()) {
for (final SchemaMethod schemaMethod : schemaNode.getProperty(SchemaNode.schemaMethods)) {
final String fileName = schemaMethod.getProperty(SchemaMethod.virtualFileName);
final String methodName = schemaMethod.getProperty(AbstractNode.name);
if (fileName != null && fileName.equals(name)) {
method = schemaMethod;
break;
}
if (methodName != null && methodName.equals(name)) {
method = schemaMethod;
break;
}
}
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
return method;
}
use of org.structr.core.entity.SchemaMethod in project structr by structr.
the class SchemaMethodResource method findMethodSource.
public static String findMethodSource(final Class type, final String methodName) throws IllegalPathException {
try {
final App app = StructrApp.getInstance();
final String typeName = type.getSimpleName();
Class currentType = type;
// first step: schema node or one of its parents
SchemaNode schemaNode = app.nodeQuery(SchemaNode.class).andName(typeName).getFirst();
while (schemaNode != null) {
for (final SchemaMethod method : schemaNode.getProperty(SchemaNode.schemaMethods)) {
if (methodName.equals(method.getName()) && !method.isJava()) {
return method.getProperty(SchemaMethod.source);
}
}
currentType = currentType.getSuperclass();
if (currentType != null) {
// skip non-dynamic types
if (currentType.getSimpleName().equals(typeName) || !currentType.getName().startsWith("org.structr.dynamic.")) {
currentType = currentType.getSuperclass();
}
if (currentType != null && currentType.getName().startsWith("org.structr.dynamic.")) {
schemaNode = app.nodeQuery(SchemaNode.class).andName(currentType.getSimpleName()).getFirst();
} else {
break;
}
} else {
break;
}
}
} catch (FrameworkException fex) {
}
throw new IllegalPathException("Type and method name do not match the given path.");
}
Aggregations