Search in sources :

Example 11 with SchemaMethod

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.");
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) SchemaNode(org.structr.core.entity.SchemaNode) NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) SchemaMethod(org.structr.core.entity.SchemaMethod) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) Principal(org.structr.core.entity.Principal) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 12 with SchemaMethod

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);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) SchemaMethod(org.structr.core.entity.SchemaMethod) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) NoSuchFileException(java.nio.file.NoSuchFileException)

Example 13 with SchemaMethod

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);
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) NodeAttribute(org.structr.core.graph.NodeAttribute) StructrPropertyValueChannel(org.structr.files.ssh.filesystem.path.graph.StructrPropertyValueChannel) SchemaMethod(org.structr.core.entity.SchemaMethod) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException)

Example 14 with SchemaMethod

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;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) SchemaMethod(org.structr.core.entity.SchemaMethod) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException)

Example 15 with SchemaMethod

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.");
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) SchemaNode(org.structr.core.entity.SchemaNode) IllegalPathException(org.structr.rest.exception.IllegalPathException) SchemaMethod(org.structr.core.entity.SchemaMethod) FrameworkException(org.structr.common.error.FrameworkException)

Aggregations

SchemaMethod (org.structr.core.entity.SchemaMethod)24 Tx (org.structr.core.graph.Tx)17 FrameworkException (org.structr.common.error.FrameworkException)16 App (org.structr.core.app.App)10 StructrApp (org.structr.core.app.StructrApp)10 SchemaNode (org.structr.core.entity.SchemaNode)10 PropertyMap (org.structr.core.property.PropertyMap)10 Test (org.junit.Test)9 SchemaProperty (org.structr.core.entity.SchemaProperty)4 NodeAttribute (org.structr.core.graph.NodeAttribute)4 User (org.structr.web.entity.User)4 LinkedList (java.util.LinkedList)3 TreeMap (java.util.TreeMap)3 AbstractSchemaNode (org.structr.core.entity.AbstractSchemaNode)3 SchemaView (org.structr.core.entity.SchemaView)3 TreeSet (java.util.TreeSet)2 StructrTest (org.structr.common.StructrTest)2 ConfigurationProvider (org.structr.schema.ConfigurationProvider)2 URI (java.net.URI)1 DirectoryStream (java.nio.file.DirectoryStream)1