Search in sources :

Example 16 with FileStore

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

the class UserFileStoresRestController method updateUserFileStore.

@ApiOperation(value = "Update a user file store")
@RequestMapping(method = RequestMethod.PUT, value = "/{xid}")
public ResponseEntity<FileStoreModel> updateUserFileStore(@ApiParam(value = "File store XID", required = true) @PathVariable("xid") String xid, @ApiParam(value = "Updated file store", required = true) @RequestBody FileStoreModel fileStore, UriComponentsBuilder builder) {
    FileStore updated = this.fileStoreService.update(xid, fileStore.toVO());
    URI location = builder.path("/user-file-stores/{xid}").buildAndExpand(updated.getXid()).toUri();
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(location);
    return new ResponseEntity<>(new FileStoreModel(updated), headers, HttpStatus.OK);
}
Also used : FileStoreModel(com.infiniteautomation.mango.rest.latest.model.filestore.FileStoreModel) FileStore(com.serotonin.m2m2.vo.FileStore) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) URI(java.net.URI) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 17 with FileStore

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

the class UserFileStoresRestController method getUserFileStoreModel.

@ApiOperation(value = "Get a user file store model")
@RequestMapping(method = RequestMethod.GET, value = "/{xid}")
public MappingJacksonValue getUserFileStoreModel(@PathVariable("xid") String xid, @AuthenticationPrincipal PermissionHolder user) {
    FileStore fs = this.fileStoreService.get(xid);
    // Seeing the permissions fields should require write protection
    MappingJacksonValue resultWithView = new MappingJacksonValue(new FileStoreModel(fs));
    if (fileStoreService.hasEditPermission(user, fs)) {
        resultWithView.setSerializationView(RoleViews.ShowRoles.class);
    } else {
        resultWithView.setSerializationView(Object.class);
    }
    return resultWithView;
}
Also used : FileStoreModel(com.infiniteautomation.mango.rest.latest.model.filestore.FileStoreModel) FileStore(com.serotonin.m2m2.vo.FileStore) MappingJacksonValue(org.springframework.http.converter.json.MappingJacksonValue) RoleViews(com.infiniteautomation.mango.rest.latest.model.RoleViews) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 18 with FileStore

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

the class FileStoreDao method mapRecord.

@Override
public FileStore mapRecord(Record record) {
    FileStore result = new FileStore();
    result.setId(record.get(table.id));
    result.setXid(record.get(table.xid));
    result.setName(record.get(table.name));
    result.setReadPermission(new MangoPermission(record.get(table.readPermissionId)));
    result.setWritePermission(new MangoPermission(record.get(table.writePermissionId)));
    return result;
}
Also used : FileStore(com.serotonin.m2m2.vo.FileStore) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Example 19 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 20 with FileStore

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

the class FileStoreServiceTest method updateVO.

@Override
FileStore updateVO(FileStore existing) {
    FileStore edit = new FileStore();
    edit.setId(existing.getId());
    edit.setXid("new store name");
    edit.setName(edit.getXid());
    return edit;
}
Also used : FileStore(com.serotonin.m2m2.vo.FileStore)

Aggregations

FileStore (com.serotonin.m2m2.vo.FileStore)38 Path (java.nio.file.Path)18 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)17 IOException (java.io.IOException)15 TranslatableIllegalArgumentException (com.infiniteautomation.mango.util.exception.TranslatableIllegalArgumentException)10 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)7 TranslatableRuntimeException (com.infiniteautomation.mango.util.exception.TranslatableRuntimeException)6 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)6 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)6 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)6 NoSuchFileException (java.nio.file.NoSuchFileException)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)4 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)4 ApiOperation (io.swagger.annotations.ApiOperation)4 ArrayList (java.util.ArrayList)4 FileStoreModel (com.infiniteautomation.mango.rest.latest.model.filestore.FileStoreModel)3 ConditionSortLimit (com.infiniteautomation.mango.db.query.ConditionSortLimit)2 MangoScript (com.infiniteautomation.mango.spring.script.MangoScript)2 LoadFileStorePermission (com.infiniteautomation.mango.spring.script.permissions.LoadFileStorePermission)2