use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.
the class ScriptRestController method evalScript.
@Async
@ApiOperation(value = "Evaluate a filestore file as a script on the backend using a scripting engine")
@RequestMapping(method = RequestMethod.POST, value = "/eval-file-store/{fileStoreName}/**")
public CompletableFuture<Void> evalScript(@ApiParam(value = "File store name", required = true) @PathVariable(required = true) String fileStoreName, @ApiParam(value = "Script engine name", required = false) @RequestParam(required = false) String engineName, @ApiParam(value = "Script file character set", required = false, defaultValue = "UTF-8") @RequestParam(required = false, defaultValue = "UTF-8") String fileCharset, @ApiParam(value = "Script roles", required = false, allowMultiple = true) @RequestParam(required = false) String[] roles, @ApiIgnore @RemainingPath String path, @AuthenticationPrincipal PermissionHolder user, HttpServletRequest request, HttpServletResponse response) throws IOException {
Path filePath = fileStoreService.getPathForRead(fileStoreName, path);
if (!Files.exists(filePath)) {
throw new NotFoundException();
}
if (engineName == null) {
engineName = scriptService.findEngineForFile(filePath);
}
Charset fileCharsetParsed = Charset.forName(fileCharset);
Set<Role> roleSet;
if (roles != null) {
roleSet = Arrays.stream(roles).map(xid -> this.roleService.get(xid).getRole()).collect(Collectors.toSet());
} else {
roleSet = user.getRoles();
}
EvalContext evalContext = new EvalContext();
Reader reader = new BufferedReader(new InputStreamReader(request.getInputStream(), Charset.forName(request.getCharacterEncoding())));
Writer writer = new OutputStreamWriter(response.getOutputStream(), Charset.forName(response.getCharacterEncoding()));
evalContext.setReader(reader);
evalContext.setWriter(writer);
evalContext.addBinding("reader", reader);
evalContext.addBinding("writer", writer);
if (permissionService.hasPermission(user, requestResponsePermission.getPermission())) {
evalContext.addBinding("request", request);
evalContext.addBinding("response", response);
}
this.scriptService.eval(new PathMangoScript(engineName, roleSet, filePath, fileCharsetParsed), evalContext);
return CompletableFuture.completedFuture(null);
}
use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.
the class ServerRestController method acceptLicenseAgreement.
@ApiOperation(value = "Accept the current license agreement.", notes = "Only valid if the current license agreement has not been accepted. If you do not accept, Mango will restart in 15 seconds, giving you a 2nd chance in case you change your mind.")
@ApiResponses({ @ApiResponse(code = 400, message = "License already accepted.") })
@RequestMapping(method = { RequestMethod.POST }, value = "/accept-license-agreement")
public void acceptLicenseAgreement(@ApiParam(value = "Agree or not", required = true, allowMultiple = false) @RequestParam Boolean agree, @AuthenticationPrincipal PermissionHolder user) {
if (agree) {
systemSettingsDao.setIntValue(SystemSettingsDao.LICENSE_AGREEMENT_VERSION, Common.getLicenseAgreementVersion());
} else {
if (Common.getLicenseAgreementVersion() == systemSettingsDao.getIntValue(SystemSettingsDao.LICENSE_AGREEMENT_VERSION))
throw new BadRequestException(new TranslatableMessage("systemSettings.licenseAlreadyAgreed"));
// Start shutdown timer
log.error("Mango will restart in 15 seconds.");
Providers.get(IMangoLifecycle.class).scheduleShutdown(15000L, true, user);
}
}
use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.
the class ServerRestController method restart.
@PreAuthorize("isAdmin()")
@ApiOperation(value = "Restart Mango", notes = "Returns location url in header for status updates while web interface is still active")
@RequestMapping(method = RequestMethod.PUT, value = "/restart")
public ResponseEntity<Void> restart(@RequestParam(value = "delay", required = false) Long delay, @AuthenticationPrincipal PermissionHolder user, UriComponentsBuilder builder, HttpServletRequest request) {
IMangoLifecycle lifecycle = Providers.get(IMangoLifecycle.class);
lifecycle.scheduleShutdown(delay, true, user);
URI location = builder.path("/status/mango").buildAndExpand().toUri();
return getResourceCreated(null, location);
}
use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.
the class DataSourcesRestController method copy.
@ApiOperation(value = "Copy data source", notes = "Copy the data source and its points with optional new XID and Name.")
@RequestMapping(method = RequestMethod.PUT, value = "/copy/{xid}")
public ResponseEntity<AbstractDataSourceModel<?>> copy(@PathVariable String xid, @ApiParam(value = "Copy's new XID", required = false, defaultValue = "null", allowMultiple = false) @RequestParam(required = false, defaultValue = "null") String copyXid, @ApiParam(value = "Copy's name", required = false, defaultValue = "null", allowMultiple = false) @RequestParam(required = false, defaultValue = "null") String copyName, @ApiParam(value = "Device name for copied points", required = false, defaultValue = "null", allowMultiple = false) @RequestParam(required = false, defaultValue = "null") String copyDeviceName, @ApiParam(value = "Enable/disabled state of data source", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean enabled, @ApiParam(value = "Copy data points", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean copyPoints, @AuthenticationPrincipal PermissionHolder user, UriComponentsBuilder builder) {
DataSourceVO copy = service.copy(xid, copyXid, copyName, copyDeviceName, enabled, copyPoints);
URI location = builder.path("/data-sources/{xid}").buildAndExpand(copy.getXid()).toUri();
HttpHeaders headers = new HttpHeaders();
headers.setLocation(location);
return new ResponseEntity<>(map.apply(copy, user), headers, HttpStatus.OK);
}
use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.
the class EventDetectorsRestController method update.
@ApiOperation(value = "Update an Event Detector", notes = "User must have data source edit permission for source of the point", response = AbstractEventDetectorModel.class)
@RequestMapping(method = RequestMethod.PUT, value = "/{xid}")
public ResponseEntity<AbstractEventDetectorModel<?>> update(@ApiParam(value = "XID of Event Handler to update", required = true, allowMultiple = false) @PathVariable String xid, @ApiParam(value = "Event Handler of update", required = true, allowMultiple = false) @RequestBody AbstractEventDetectorModel<? extends AbstractEventDetectorVO> model, @ApiParam(value = "Restart the source to load in the changes", required = false, defaultValue = "true", allowMultiple = false) @RequestParam(required = false, defaultValue = "true") boolean restart, @AuthenticationPrincipal PermissionHolder user, UriComponentsBuilder builder) {
AbstractEventDetectorVO vo = service.updateAndReload(xid, model.toVO(), restart);
URI location = builder.path("/event-detectors/{xid}").buildAndExpand(vo.getXid()).toUri();
HttpHeaders headers = new HttpHeaders();
headers.setLocation(location);
return new ResponseEntity<>(map.apply(vo, user), headers, HttpStatus.OK);
}
Aggregations