Search in sources :

Example 6 with UserModel

use of com.infiniteautomation.mango.rest.latest.model.user.UserModel 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;
}
Also used : UserModel(com.infiniteautomation.mango.rest.latest.model.user.UserModel) VoAction(com.infiniteautomation.mango.rest.latest.bulk.VoAction) UserIndividualResponse(com.infiniteautomation.mango.rest.latest.model.user.UserIndividualResponse) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) AbstractRestException(com.infiniteautomation.mango.rest.latest.exception.AbstractRestException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException)

Example 7 with UserModel

use of com.infiniteautomation.mango.rest.latest.model.user.UserModel in project ma-modules-public by infiniteautomation.

the class UserRestController method bulkUserOperationCSV.

@ApiOperation(value = "Bulk get/create/update/delete users", notes = "User must have read/edit permission for the user", consumes = MediaTypes.CSV_VALUE)
@RequestMapping(method = RequestMethod.POST, value = "/bulk", consumes = MediaTypes.CSV_VALUE)
public ResponseEntity<TemporaryResource<UserBulkResponse, AbstractRestException>> bulkUserOperationCSV(@RequestBody List<UserActionAndModel> users, HttpServletRequest servletRequest, UriComponentsBuilder builder, Authentication authentication) {
    UserBulkRequest bulkRequest = new UserBulkRequest();
    bulkRequest.setRequests(users.stream().map(actionAndModel -> {
        UserModel u = actionAndModel.getModel();
        VoAction action = actionAndModel.getAction();
        String originalUsername = actionAndModel.getOriginalUsername();
        if (originalUsername == null && u != null) {
            originalUsername = u.getUsername();
        }
        UserIndividualRequest request = new UserIndividualRequest();
        request.setAction(action == null ? VoAction.UPDATE : action);
        request.setUsername(originalUsername);
        request.setBody(u);
        return request;
    }).collect(Collectors.toList()));
    return this.bulkUserOperation(bulkRequest, servletRequest, authentication, builder);
}
Also used : UserModel(com.infiniteautomation.mango.rest.latest.model.user.UserModel) VoAction(com.infiniteautomation.mango.rest.latest.bulk.VoAction) UserIndividualRequest(com.infiniteautomation.mango.rest.latest.model.user.UserIndividualRequest) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with UserModel

use of com.infiniteautomation.mango.rest.latest.model.user.UserModel 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);
}
Also used : PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) UserModel(com.infiniteautomation.mango.rest.latest.model.user.UserModel) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) User(com.serotonin.m2m2.vo.User) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) URI(java.net.URI) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with UserModel

use of com.infiniteautomation.mango.rest.latest.model.user.UserModel in project ma-modules-public by infiniteautomation.

the class EmailVerificationController method publicRegisterUser.

/**
 * CAUTION: This method is public!
 * However the token's signature is cryptographically verified.
 */
@ApiOperation(value = "Registers a new user if the token's signature can be verified", notes = "The new user is created disabled and must be approved by an administrator.")
@RequestMapping(method = RequestMethod.POST, value = "/public/register")
@AnonymousAccess
public ResponseEntity<UserModel> publicRegisterUser(@RequestBody PublicRegistrationRequest body) {
    body.ensureValid();
    User newUser = body.getUser().toVO();
    try {
        User created = emailVerificationService.publicRegisterNewUser(body.getToken(), newUser);
        return new ResponseEntity<>(new UserModel(created), HttpStatus.OK);
    } catch (ExpiredJwtException | UnsupportedJwtException | MalformedJwtException | IllegalArgumentException | SignatureException | MissingClaimException | IncorrectClaimException e) {
        throw new BadRequestException(new TranslatableMessage("rest.error.invalidEmailVerificationToken"), e);
    } catch (ValidationException e) {
        e.getValidationResult().prefixContextKey("user");
        throw e;
    }
}
Also used : User(com.serotonin.m2m2.vo.User) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ExpiredJwtException(io.jsonwebtoken.ExpiredJwtException) SignatureException(io.jsonwebtoken.SignatureException) IncorrectClaimException(io.jsonwebtoken.IncorrectClaimException) UserModel(com.infiniteautomation.mango.rest.latest.model.user.UserModel) ResponseEntity(org.springframework.http.ResponseEntity) MissingClaimException(io.jsonwebtoken.MissingClaimException) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) MalformedJwtException(io.jsonwebtoken.MalformedJwtException) UnsupportedJwtException(io.jsonwebtoken.UnsupportedJwtException) AnonymousAccess(com.serotonin.m2m2.web.mvc.spring.security.permissions.AnonymousAccess) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with UserModel

use of com.infiniteautomation.mango.rest.latest.model.user.UserModel in project ma-modules-public by infiniteautomation.

the class UserRestController method bulkUserOperation.

@ApiOperation(value = "Bulk get/create/update/delete users", notes = "User must have read/edit permission for the user")
@RequestMapping(method = RequestMethod.POST, value = "/bulk")
public ResponseEntity<TemporaryResource<UserBulkResponse, AbstractRestException>> bulkUserOperation(@RequestBody UserBulkRequest requestBody, HttpServletRequest servletRequest, Authentication authentication, UriComponentsBuilder builder) {
    VoAction defaultAction = requestBody.getAction();
    UserModel defaultBody = requestBody.getBody();
    List<UserIndividualRequest> requests = requestBody.getRequests();
    if (requests == null) {
        throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "requests"));
    } else if (requests.isEmpty()) {
        throw new BadRequestException(new TranslatableMessage("rest.error.cantBeEmpty", "requests"));
    }
    String resourceId = requestBody.getId();
    Long expiration = requestBody.getExpiration();
    Long timeout = requestBody.getTimeout();
    TemporaryResource<UserBulkResponse, AbstractRestException> responseBody = bulkResourceManager.newTemporaryResource(RESOURCE_TYPE_BULK_USER, resourceId, expiration, timeout, (resource) -> {
        UserBulkResponse bulkResponse = new UserBulkResponse();
        int i = 0;
        resource.progressOrSuccess(bulkResponse, i++, requests.size());
        for (UserIndividualRequest request : requests) {
            UriComponentsBuilder reqBuilder = UriComponentsBuilder.newInstance();
            PermissionHolder resourceUser = Common.getUser();
            UserIndividualResponse individualResponse = doIndividualRequest(request, defaultAction, defaultBody, resourceUser, servletRequest, authentication, reqBuilder);
            bulkResponse.addResponse(individualResponse);
            resource.progressOrSuccess(bulkResponse, i++, requests.size());
        }
        return null;
    });
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(builder.path("/users/bulk/{id}").buildAndExpand(responseBody.getId()).toUri());
    return new ResponseEntity<>(responseBody, headers, HttpStatus.CREATED);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) VoAction(com.infiniteautomation.mango.rest.latest.bulk.VoAction) UserIndividualResponse(com.infiniteautomation.mango.rest.latest.model.user.UserIndividualResponse) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) UserModel(com.infiniteautomation.mango.rest.latest.model.user.UserModel) ResponseEntity(org.springframework.http.ResponseEntity) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) UserIndividualRequest(com.infiniteautomation.mango.rest.latest.model.user.UserIndividualRequest) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) AbstractRestException(com.infiniteautomation.mango.rest.latest.exception.AbstractRestException) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

UserModel (com.infiniteautomation.mango.rest.latest.model.user.UserModel)14 ApiOperation (io.swagger.annotations.ApiOperation)12 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)12 ResponseEntity (org.springframework.http.ResponseEntity)11 User (com.serotonin.m2m2.vo.User)8 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)7 HttpHeaders (org.springframework.http.HttpHeaders)6 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)5 URI (java.net.URI)5 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)4 VoAction (com.infiniteautomation.mango.rest.latest.bulk.VoAction)3 BadRequestException (com.infiniteautomation.mango.rest.latest.exception.BadRequestException)3 LoginUriInfo (com.infiniteautomation.mango.spring.components.pageresolver.LoginUriInfo)3 AuthenticationException (org.springframework.security.core.AuthenticationException)3 AbstractRestException (com.infiniteautomation.mango.rest.latest.exception.AbstractRestException)2 UserIndividualRequest (com.infiniteautomation.mango.rest.latest.model.user.UserIndividualRequest)2 UserIndividualResponse (com.infiniteautomation.mango.rest.latest.model.user.UserIndividualResponse)2 AnonymousAccess (com.serotonin.m2m2.web.mvc.spring.security.permissions.AnonymousAccess)2 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)1 SystemEventType (com.serotonin.m2m2.rt.event.type.SystemEventType)1