Search in sources :

Example 76 with ApiOperation

use of com.wordnik.swagger.annotations.ApiOperation in project ma-modules-public by infiniteautomation.

the class UserRestController method getNewUser.

@ApiOperation(value = "Get new user", notes = "Returns a new user with default values")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json", "text/csv" }, value = "/new/user")
public ResponseEntity<UserModel> getNewUser(HttpServletRequest request) {
    RestProcessResult<UserModel> result = new RestProcessResult<UserModel>(HttpStatus.OK);
    this.checkUser(request, result);
    if (result.isOk()) {
        return result.createResponseEntity(new UserModel(new User()));
    }
    return result.createResponseEntity();
}
Also used : UserModel(com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 77 with ApiOperation

use of com.wordnik.swagger.annotations.ApiOperation in project ma-modules-public by infiniteautomation.

the class UserRestController method updateMuted.

@ApiOperation(value = "Update a user's audio mute setting", notes = "If you do not provide the mute parameter the current setting will be toggled")
@RequestMapping(method = RequestMethod.PUT, produces = { "application/json", "text/csv" }, value = "/{username}/mute")
public ResponseEntity<UserModel> updateMuted(@ApiParam(value = "Username", required = true, allowMultiple = false) @PathVariable String username, @ApiParam(value = "Mute", required = false, defaultValue = "Toggle the current setting", allowMultiple = false) @RequestParam(required = false) Boolean mute, HttpServletRequest request, Authentication authentication) throws RestValidationFailedException {
    RestProcessResult<UserModel> result = new RestProcessResult<UserModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        User u = UserDao.instance.getUser(username);
        if (Permissions.hasAdmin(user)) {
            if (u == null) {
                result.addRestMessage(getDoesNotExistMessage());
                return result.createResponseEntity();
            }
            if (u.getId() == user.getId() && !(authentication instanceof UsernamePasswordAuthenticationToken)) {
                throw new AccessDeniedException(new TranslatableMessage("rest.error.usernamePasswordOnly"));
            }
            if (mute == null) {
                u.setMuted(!u.isMuted());
            } else {
                u.setMuted(mute);
            }
            UserModel model = new UserModel(u);
            if (!model.validate()) {
                result.addRestMessage(this.getValidationFailedError());
            } else {
                UserDao.instance.saveUser(u);
                sessionRegistry.userUpdated(request, u);
            }
            return result.createResponseEntity(model);
        } else {
            if (u.getId() != user.getId()) {
                LOG.warn("Non admin user: " + user.getUsername() + " attempted to access user : " + u.getUsername());
                result.addRestMessage(this.getUnauthorizedMessage());
                return result.createResponseEntity();
            } else {
                if (mute == null) {
                    // Toggle
                    u.setMuted(!u.isMuted());
                } else {
                    u.setMuted(mute);
                }
                UserModel model = new UserModel(u);
                // Allow users to update themselves
                model.getData().setId(u.getId());
                if (!model.validate()) {
                    result.addRestMessage(this.getValidationFailedError());
                } else {
                    UserDao.instance.saveUser(u);
                    sessionRegistry.userUpdated(request, u);
                }
                return result.createResponseEntity(model);
            }
        }
    }
    return result.createResponseEntity();
}
Also used : UserModel(com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) User(com.serotonin.m2m2.vo.User) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 78 with ApiOperation

use of com.wordnik.swagger.annotations.ApiOperation in project ma-modules-public by infiniteautomation.

the class UserRestController method getUser.

@ApiOperation(value = "Get user by name", notes = "Returns the user specified by the given username")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json", "text/csv" }, value = "/{username}")
public ResponseEntity<UserModel> getUser(@ApiParam(value = "Valid username", required = true, allowMultiple = false) @PathVariable String username, HttpServletRequest request) {
    RestProcessResult<UserModel> result = new RestProcessResult<UserModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        User u = UserDao.instance.getUser(username);
        if (Permissions.hasAdmin(user)) {
            if (u == null) {
                result.addRestMessage(getDoesNotExistMessage());
                return result.createResponseEntity();
            }
            UserModel model = new UserModel(u);
            return result.createResponseEntity(model);
        } else {
            if (u.getId() != user.getId()) {
                LOG.warn("Non admin user: " + user.getUsername() + " attempted to access user : " + u.getUsername());
                result.addRestMessage(this.getUnauthorizedMessage());
                return result.createResponseEntity();
            } else {
                // Allow users to access themselves
                return result.createResponseEntity(new UserModel(u));
            }
        }
    }
    return result.createResponseEntity();
}
Also used : UserModel(com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 79 with ApiOperation

use of com.wordnik.swagger.annotations.ApiOperation in project ma-modules-public by infiniteautomation.

the class UserRestController method getCurrentUser.

@ApiOperation(value = "Get current user", notes = "Returns the logged in user")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json", "text/csv" }, value = "/current")
public ResponseEntity<UserModel> getCurrentUser(HttpServletRequest request) {
    RestProcessResult<UserModel> result = new RestProcessResult<UserModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        UserModel model = new UserModel(user);
        return result.createResponseEntity(model);
    }
    return result.createResponseEntity();
}
Also used : UserModel(com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 80 with ApiOperation

use of com.wordnik.swagger.annotations.ApiOperation in project ma-modules-public by infiniteautomation.

the class UserRestController method queryRQL.

@ApiOperation(value = "Query Users", notes = "", response = UserModel.class, responseContainer = "Array")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Ok", response = UserModel.class), @ApiResponse(code = 403, message = "User does not have access", response = ResponseEntity.class) })
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" })
public ResponseEntity<QueryDataPageStream<User>> queryRQL(HttpServletRequest request) {
    RestProcessResult<QueryDataPageStream<User>> result = new RestProcessResult<QueryDataPageStream<User>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        try {
            // Parse the RQL Query
            ASTNode query = parseRQLtoAST(request.getQueryString());
            if (!user.isAdmin()) {
                query = addAndRestriction(query, new ASTNode("eq", "id", user.getId()));
            }
            return result.createResponseEntity(getPageStream(query));
        } catch (InvalidRQLRestException e) {
            LOG.error(e.getMessage(), e);
            result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
            return result.createResponseEntity();
        }
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) QueryDataPageStream(com.serotonin.m2m2.web.mvc.rest.v1.model.QueryDataPageStream) User(com.serotonin.m2m2.vo.User) InvalidRQLRestException(com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException) ASTNode(net.jazdw.rql.parser.ASTNode) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) ApiResponses(com.wordnik.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ApiOperation (com.wordnik.swagger.annotations.ApiOperation)274 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)212 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)125 User (com.serotonin.m2m2.vo.User)115 URI (java.net.URI)59 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)54 ArrayList (java.util.ArrayList)50 Produces (javax.ws.rs.Produces)49 ResponseEntity (org.springframework.http.ResponseEntity)44 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)42 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)41 Path (javax.ws.rs.Path)40 Response (javax.ws.rs.core.Response)38 ApiResponses (com.wordnik.swagger.annotations.ApiResponses)36 DefaultValue (javax.ws.rs.DefaultValue)35 HeaderParam (javax.ws.rs.HeaderParam)35 ListResponse (org.gluu.oxtrust.model.scim2.ListResponse)34 ASTNode (net.jazdw.rql.parser.ASTNode)31 List (java.util.List)29 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)25