Search in sources :

Example 6 with FileStore

use of com.serotonin.m2m2.vo.FileStore in project ma-core-public by infiniteautomation.

the class NashornScriptEngineDefinition method createEngine.

@Override
public ScriptEngine createEngine(ScriptEngineFactory engineFactory, MangoScript script) {
    ScriptEngine engine = createScriptEngine(engineFactory, permissionService.hasAdminRole(script) ? null : c -> false);
    Bindings engineBindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    Object originalLoad = engineBindings.get("load");
    if (!permissionService.hasAdminRole(script)) {
        // remove exit and quit functions from bindings
        for (String key : KEYS_TO_REMOVE) {
            engineBindings.remove(key);
        }
        // make the engine and context inaccessible
        try {
            engine.eval("Object.defineProperty(this, 'engine', {}); Object.defineProperty(this, 'context', {});");
        } catch (ScriptException e) {
            throw new RuntimeException(e);
        }
    }
    Function<Object, Object> replacementLoad = source -> {
        URL url = null;
        if (source instanceof URL) {
            url = (URL) source;
        } else if (source instanceof String && ((String) source).indexOf(':') >= 0) {
            try {
                url = new URL((String) source);
            } catch (MalformedURLException e) {
            // ignore
            }
        }
        if (url != null) {
            String protocol = url.getProtocol();
            boolean isFileStore = "filestore".equals(protocol);
            boolean isWeb = "http".equals(protocol) || "https".equals(protocol);
            if (isFileStore && permissionService.hasPermission(script, loadFileStorePermission.getPermission()) || isWeb && permissionService.hasPermission(script, loadWebPermission.getPermission())) {
                return callFunction(originalLoad, null, source);
            }
        }
        permissionService.ensurePermission(script, loadOtherPermission.getPermission());
        return callFunction(originalLoad, null, source);
    };
    engineBindings.put("load", replacementLoad);
    return engine;
}
Also used : ScriptEngineDefinition(com.serotonin.m2m2.module.ScriptEngineDefinition) Bindings(javax.script.Bindings) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) LoadFileStorePermission(com.infiniteautomation.mango.spring.script.permissions.LoadFileStorePermission) FileStoreService(com.infiniteautomation.mango.spring.service.FileStoreService) Autowired(org.springframework.beans.factory.annotation.Autowired) MangoScript(com.infiniteautomation.mango.spring.script.MangoScript) LoadWebPermission(com.infiniteautomation.mango.spring.script.permissions.LoadWebPermission) ScriptEngineFactory(javax.script.ScriptEngineFactory) Function(java.util.function.Function) NashornPermission(com.infiniteautomation.mango.spring.script.permissions.NashornPermission) ScriptContext(javax.script.ScriptContext) ScriptEngine(javax.script.ScriptEngine) LoadOtherPermission(com.infiniteautomation.mango.spring.script.permissions.LoadOtherPermission) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) ScriptException(javax.script.ScriptException) ScriptException(javax.script.ScriptException) MalformedURLException(java.net.MalformedURLException) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine) URL(java.net.URL)

Example 7 with FileStore

use of com.serotonin.m2m2.vo.FileStore in project ma-core-public by infiniteautomation.

the class FileStoreService method delete.

@Override
protected FileStore delete(FileStore vo) throws PermissionException, NotFoundException {
    if (ModuleRegistry.getFileStoreDefinitions().containsKey(vo.getXid())) {
        throw new UnsupportedOperationException("Deleting a built in filestore is not supported");
    }
    FileStore deleted = super.delete(vo);
    Path root = getFileStoreRoot(deleted);
    if (Files.exists(root)) {
        try {
            FileUtils.deleteDirectory(root.toFile());
        } catch (IOException e) {
            throw new FileStoreException(new TranslatableMessage("filestore.failedToDeleteFiles", deleted.getXid()), e);
        }
    }
    return deleted;
}
Also used : Path(java.nio.file.Path) FileStore(com.serotonin.m2m2.vo.FileStore) IOException(java.io.IOException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 8 with FileStore

use of com.serotonin.m2m2.vo.FileStore in project ma-core-public by infiniteautomation.

the class FileStoreService method update.

@Override
protected FileStore update(FileStore existing, FileStore vo) throws PermissionException, ValidationException {
    if (ModuleRegistry.getFileStoreDefinitions().containsKey(existing.getXid())) {
        throw new UnsupportedOperationException("Updating a built in filestore is not supported");
    }
    FileStore updated = super.update(existing, vo);
    // move files to the new location if the XID changed
    if (!updated.getXid().equals(existing.getXid())) {
        Path existingPath = getFileStoreRoot(existing);
        Path newPath = getFileStoreRoot(updated);
        if (Files.exists(existingPath)) {
            try {
                Files.move(existingPath, newPath);
            } catch (IOException e) {
                throw new FileStoreException(new TranslatableMessage("filestore.failedToMoveFiles", updated.getXid()), e);
            }
        }
    }
    return updated;
}
Also used : Path(java.nio.file.Path) FileStore(com.serotonin.m2m2.vo.FileStore) IOException(java.io.IOException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 9 with FileStore

use of com.serotonin.m2m2.vo.FileStore in project ma-core-public by infiniteautomation.

the class FileStoreService method get.

/**
 * @param xid xid of the user file store, or the storeName of the {@link FileStoreDefinition}
 */
@Override
public FileStore get(String xid) throws PermissionException, NotFoundException {
    FileStore vo = getWithoutPermissionCheck(xid);
    ensureReadPermission(Common.getUser(), vo);
    return vo;
}
Also used : FileStore(com.serotonin.m2m2.vo.FileStore)

Example 10 with FileStore

use of com.serotonin.m2m2.vo.FileStore in project ma-core-public by infiniteautomation.

the class FileStoreService method ensureWriteAccess.

/**
 * @throws IllegalArgumentException if path is not located inside the filestore root
 * @throws NotFoundException        if filestore was not found
 * @throws PermissionException      filestore exists but user does not have write access
 */
public void ensureWriteAccess(Path path) throws IllegalArgumentException, NotFoundException, PermissionException {
    FileStore fileStore = resolveFileStore(path).getFileStore();
    ensureEditPermission(Common.getUser(), fileStore);
}
Also used : FileStore(com.serotonin.m2m2.vo.FileStore)

Aggregations

FileStore (com.serotonin.m2m2.vo.FileStore)21 Path (java.nio.file.Path)10 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)9 IOException (java.io.IOException)8 TranslatableIllegalArgumentException (com.infiniteautomation.mango.util.exception.TranslatableIllegalArgumentException)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)4 ApiOperation (io.swagger.annotations.ApiOperation)4 FileStoreModel (com.infiniteautomation.mango.rest.latest.model.filestore.FileStoreModel)3 TranslatableRuntimeException (com.infiniteautomation.mango.util.exception.TranslatableRuntimeException)3 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)3 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)3 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)3 NoSuchFileException (java.nio.file.NoSuchFileException)3 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)2 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 ConditionSortLimit (com.infiniteautomation.mango.db.query.ConditionSortLimit)1 RoleViews (com.infiniteautomation.mango.rest.latest.model.RoleViews)1