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;
}
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;
}
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;
}
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;
}
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);
}
Aggregations