use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.
the class UserRestController method updateHomeUrl.
@ApiOperation(value = "Update a user's home url")
@RequestMapping(method = RequestMethod.PUT, value = "/{username}/homepage")
public ResponseEntity<UserModel> updateHomeUrl(@ApiParam(value = "Username", required = true) @PathVariable String username, @ApiParam(value = "Home Url", required = true) @RequestParam String url, @AuthenticationPrincipal PermissionHolder user, HttpServletRequest request, UriComponentsBuilder builder, Authentication authentication) {
User update = service.get(username);
User currentUser = user.getUser();
if (currentUser != null && update.getId() == currentUser.getId() && !(authentication instanceof UsernamePasswordAuthenticationToken))
throw new PermissionException(new TranslatableMessage("rest.error.usernamePasswordOnly"), user);
update.setHomeUrl(url);
update = service.update(username, update);
sessionRegistry.userUpdated(request, update);
URI location = builder.path("/users/{username}").buildAndExpand(update.getUsername()).toUri();
HttpHeaders headers = new HttpHeaders();
headers.setLocation(location);
return new ResponseEntity<>(new UserModel(update), headers, HttpStatus.OK);
}
use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.
the class UserRestController method updateLinkedAccounts.
@RequestMapping(method = RequestMethod.PUT, value = "/linked-accounts/{username}")
public void updateLinkedAccounts(@PathVariable String username, @RequestBody List<LinkedAccountModel> linkedAccountModels, @AuthenticationPrincipal PermissionHolder currentUser) {
User userToUpdate = service.get(username);
List<LinkedAccount> linkedAccounts = linkedAccountModels.stream().map(a -> mapper.unMap(a, LinkedAccount.class, currentUser)).collect(Collectors.toList());
service.updateLinkedAccounts(userToUpdate.getId(), linkedAccounts);
}
use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.
the class UserRestController method doIndividualRequest.
private UserIndividualResponse doIndividualRequest(UserIndividualRequest request, VoAction defaultAction, UserModel defaultBody, PermissionHolder user, HttpServletRequest servletRequest, Authentication authentication, UriComponentsBuilder builder) {
UserIndividualResponse result = new UserIndividualResponse();
try {
String username = request.getUsername();
result.setUsername(username);
VoAction action = request.getAction() == null ? defaultAction : request.getAction();
if (action == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "action"));
}
result.setAction(action);
UserModel body = request.getBody() == null ? defaultBody : request.getBody();
switch(action) {
case GET:
if (username == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
}
result.setBody(this.getUser(username));
break;
case CREATE:
if (body == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
}
result.setBody(body);
result.setBody(this.createUser(body, builder).getBody());
break;
case UPDATE:
if (username == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
}
if (body == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
}
result.setBody(body);
result.setBody(this.updateUser(username, body, user, servletRequest, builder, authentication).getBody());
break;
case DELETE:
if (username == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
}
result.setBody(this.deleteUser(username));
break;
}
} catch (Exception e) {
result.exceptionCaught(e);
}
return result;
}
use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.
the class SystemActionRestController method performAction.
@ApiOperation(value = "Perform an Action", notes = "Kicks off action and returns temporary URL for status")
@ApiResponses({ @ApiResponse(code = 500, message = "Internal error", response = ResponseEntity.class), @ApiResponse(code = 404, message = "Not Found", response = ResponseEntity.class) })
@RequestMapping(method = RequestMethod.PUT, value = "/trigger/{action}")
public ResponseEntity<SystemActionTemporaryResource> performAction(@ApiParam(value = "Valid System Action", required = true, allowMultiple = false) @PathVariable String action, @ApiParam(value = "Input for task", required = false, allowMultiple = false) @RequestBody(required = false) JsonNode input, @RequestParam(required = false, defaultValue = "12000000") Long timeout, @AuthenticationPrincipal PermissionHolder user, UriComponentsBuilder builder) {
// Kick off action
SystemActionDefinition def = ModuleRegistry.getSystemActionDefinition(action);
if (def == null)
throw new NotFoundRestException();
String resourceId = resources.generateResourceId();
SystemActionTemporaryResource resource = new SystemActionTemporaryResource(resourceId, def.getTask(user, input), resources, new Date(System.currentTimeMillis() + timeout));
// Resource can live for up to 10 minutes (TODO Configurable?)
resources.put(resourceId, resource);
URI location = builder.path("/actions/status/{resourceId}").buildAndExpand(resourceId).toUri();
HttpHeaders headers = new HttpHeaders();
headers.setLocation(location);
return new ResponseEntity<>(resource, headers, HttpStatus.OK);
}
use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.
the class WorkItemRestController method getWorkItemsByPriority.
@ApiOperation(value = "Get list of work items by classname", notes = "Returns the Work Item specified by the given classname and priority")
@RequestMapping(method = RequestMethod.GET, value = "/by-priority/{priority}")
public List<WorkItemInfo> getWorkItemsByPriority(@ApiParam(value = "priority", required = true, allowMultiple = false) @PathVariable String priority, @RequestParam(value = "classname", required = false, defaultValue = "") String classname, @AuthenticationPrincipal PermissionHolder user) {
permissionService.ensureAdminRole(user);
List<WorkItemInfo> list;
if (priority.equalsIgnoreCase("HIGH")) {
list = Common.backgroundProcessing.getHighPriorityServiceItems();
} else if (priority.equalsIgnoreCase("MEDIUM")) {
list = Common.backgroundProcessing.getMediumPriorityServiceQueueItems();
} else if (priority.equalsIgnoreCase("LOW")) {
list = Common.backgroundProcessing.getLowPriorityServiceQueueItems();
} else {
ProcessResult result = new ProcessResult();
result.addContextualMessage("priority", "validate.invalidValue");
throw new ValidationException(result);
}
// Filter if we need to
if (StringUtils.isNotEmpty(classname)) {
List<WorkItemInfo> modelList = new ArrayList<>();
for (WorkItemInfo model : list) {
if (model.getClassname().equalsIgnoreCase(classname)) {
modelList.add(model);
}
}
return modelList;
} else {
return list;
}
}
Aggregations