use of com.serotonin.m2m2.vo.role.Role in project ma-modules-public by infiniteautomation.
the class RoleRestController method update.
@ApiOperation(value = "Update a Role List", notes = "Admin only")
@RequestMapping(method = RequestMethod.PUT, value = "/{xid}")
public ResponseEntity<RoleModel> update(@ApiParam(value = "XID of Role to update", required = true, allowMultiple = false) @PathVariable String xid, @ApiParam(value = "Role List of update", required = true, allowMultiple = false) @RequestBody RoleModel model, @AuthenticationPrincipal PermissionHolder user, UriComponentsBuilder builder) {
RoleVO vo = service.update(xid, mapping.unmap(model, user, mapper));
URI location = builder.path("/roles/{xid}").buildAndExpand(vo.getXid()).toUri();
HttpHeaders headers = new HttpHeaders();
headers.setLocation(location);
return new ResponseEntity<>(mapping.map(vo, user, mapper), headers, HttpStatus.OK);
}
use of com.serotonin.m2m2.vo.role.Role in project ma-modules-public by infiniteautomation.
the class RoleRestController method partialUpdate.
@ApiOperation(value = "Partially update a Role", notes = "Admin only")
@RequestMapping(method = RequestMethod.PATCH, value = "/{xid}")
public ResponseEntity<RoleModel> partialUpdate(@PathVariable String xid, @ApiParam(value = "Updated role", required = true) @PatchVORequestBody(service = RoleService.class, modelClass = RoleModel.class) RoleModel model, @AuthenticationPrincipal PermissionHolder user, UriComponentsBuilder builder) {
RoleVO vo = service.update(xid, mapping.unmap(model, user, mapper));
URI location = builder.path("/roles/{xid}").buildAndExpand(vo.getXid()).toUri();
HttpHeaders headers = new HttpHeaders();
headers.setLocation(location);
return new ResponseEntity<>(mapping.map(vo, user, mapper), headers, HttpStatus.OK);
}
use of com.serotonin.m2m2.vo.role.Role 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.role.Role in project ma-modules-public by infiniteautomation.
the class Upgrade4 method upgrade.
@Override
protected void upgrade() throws Exception {
try (OutputStream out = createUpdateLogOutputStream()) {
// Get a reference to the superadmin permission in DB
MangoPermission superadmin = getOrCreatePermissionNoCache(MangoPermission.superadminOnly());
ejt.query("SELECT id, typeName, typeRef1 FROM events WHERE typeName=?", new Object[] { MaintenanceEventType.TYPE_NAME }, rs -> {
int eventId = rs.getInt(1);
Integer voId = rs.getInt(3);
// Find the permission id for this me
Integer permissionId = maintenanceEventPermissionMap.computeIfAbsent(voId, (k) -> {
// Build the permission for this event
Set<Role> allRequired = new HashSet<>();
List<Integer> dataPointIds = ejt.queryForList(SELECT_POINT_IDS, new Object[] { k }, Integer.class);
for (Integer dpId : dataPointIds) {
MangoPermission dataPointPermission = dataPointPermissionMap.computeIfAbsent(dpId, (pointId) -> {
Integer id = ejt.queryForInt("SELECT readPermissionId from dataPoints where id=?", new Object[] { pointId }, Common.NEW_ID);
if (id == Common.NEW_ID) {
return superadmin;
} else {
return permissionMap.computeIfAbsent(id, (pId) -> {
MangoPermission p = get(pId);
if (p == null) {
return superadmin;
} else {
return p;
}
});
}
});
dataPointPermission.getRoles().stream().forEach(minterm -> allRequired.addAll(minterm));
}
List<Integer> dataSourceIds = ejt.queryForList(SELECT_DATA_SOURCE_IDS, new Object[] { k }, Integer.class);
for (Integer dsId : dataSourceIds) {
MangoPermission dataSourcePermission = dataSourcePermissionMap.computeIfAbsent(dsId, (sourceId) -> {
Integer id = ejt.queryForInt("SELECT readPermissionId from dataSources where id=?", new Object[] { sourceId }, Common.NEW_ID);
if (id == Common.NEW_ID) {
return superadmin;
} else {
return permissionMap.computeIfAbsent(id, (pId) -> {
MangoPermission p = get(pId);
if (p == null) {
return superadmin;
} else {
return p;
}
});
}
});
dataSourcePermission.getRoles().stream().forEach(minterm -> allRequired.addAll(minterm));
}
MangoPermission permission;
if (allRequired.size() == 0) {
permission = MangoPermission.superadminOnly();
} else {
permission = MangoPermission.requireAllRoles(allRequired);
}
return getOrCreatePermission(permission).getId();
});
ejt.update("UPDATE events SET readPermissionId=? WHERE id=?", permissionId, eventId);
});
}
}
use of com.serotonin.m2m2.vo.role.Role in project ma-modules-public by infiniteautomation.
the class RoleModelMapping method map.
@Override
public RoleModel map(Object from, PermissionHolder user, RestModelMapper mapper) {
RoleVO role = (RoleVO) from;
RoleModel model = new RoleModel(role);
if (role.getInherited() != null) {
Set<String> inherited = new HashSet<>(role.getInherited().size());
model.setInherited(inherited);
for (Role inheritedRole : role.getInherited()) {
inherited.add(inheritedRole.getXid());
}
}
return model;
}
Aggregations