Search in sources :

Example 1 with UserIndividualResponse

use of com.infiniteautomation.mango.rest.latest.model.user.UserIndividualResponse 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 2 with UserIndividualResponse

use of com.infiniteautomation.mango.rest.latest.model.user.UserIndividualResponse 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

VoAction (com.infiniteautomation.mango.rest.latest.bulk.VoAction)2 AbstractRestException (com.infiniteautomation.mango.rest.latest.exception.AbstractRestException)2 BadRequestException (com.infiniteautomation.mango.rest.latest.exception.BadRequestException)2 UserIndividualResponse (com.infiniteautomation.mango.rest.latest.model.user.UserIndividualResponse)2 UserModel (com.infiniteautomation.mango.rest.latest.model.user.UserModel)2 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2 UserIndividualRequest (com.infiniteautomation.mango.rest.latest.model.user.UserIndividualRequest)1 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)1 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)1 ApiOperation (io.swagger.annotations.ApiOperation)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)1