Search in sources :

Example 1 with UserModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel in project ma-modules-public by infiniteautomation.

the class UserAccessRestController method getDataSourceAccess.

@ApiOperation(value = "Get Data Source Access List", notes = "Returns a list of users and thier access")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json", "text/csv" }, value = "/data-source/{xid}")
public ResponseEntity<List<UserAccessModel>> getDataSourceAccess(@ApiParam(value = "Valid data point xid", required = true, allowMultiple = false) @PathVariable String xid, HttpServletRequest request) {
    RestProcessResult<List<UserAccessModel>> result = new RestProcessResult<List<UserAccessModel>>(HttpStatus.OK);
    this.checkUser(request, result);
    if (result.isOk()) {
        DataSourceVO<?> vo = DataSourceDao.instance.getByXid(xid);
        if (vo != null) {
            List<UserAccessModel> models = new ArrayList<UserAccessModel>();
            List<User> allUsers = UserDao.instance.getUsers();
            for (User mangoUser : allUsers) {
                if (Permissions.hasDataSourcePermission(mangoUser, vo)) {
                    models.add(new UserAccessModel(Permissions.ACCESS_TYPE_CODES.getCode(DataPointAccessTypes.DATA_SOURCE), new UserModel(mangoUser)));
                }
            }
            return result.createResponseEntity(models);
        }
    }
    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) UserAccessModel(com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserAccessModel) User(com.serotonin.m2m2.vo.User) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with UserModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel in project ma-modules-public by infiniteautomation.

the class UserAccessRestController method getDataPointAccess.

@ApiOperation(value = "Get Data Point Access List", notes = "Returns a list of users and thier access")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json", "text/csv" }, value = "/data-point/{xid}")
public ResponseEntity<List<UserAccessModel>> getDataPointAccess(@ApiParam(value = "Valid data point xid", required = true, allowMultiple = false) @PathVariable String xid, HttpServletRequest request) {
    RestProcessResult<List<UserAccessModel>> result = new RestProcessResult<List<UserAccessModel>>(HttpStatus.OK);
    this.checkUser(request, result);
    if (result.isOk()) {
        DataPointVO vo = DataPointDao.instance.getByXid(xid);
        if (vo != null) {
            List<UserAccessModel> models = new ArrayList<UserAccessModel>();
            List<User> allUsers = UserDao.instance.getUsers();
            int accessType;
            for (User mangoUser : allUsers) {
                accessType = Permissions.getDataPointAccessType(mangoUser, vo);
                if (accessType != Permissions.DataPointAccessTypes.NONE) {
                    models.add(new UserAccessModel(Permissions.ACCESS_TYPE_CODES.getCode(accessType), new UserModel(mangoUser)));
                }
            }
            return result.createResponseEntity(models);
        }
    }
    return result.createResponseEntity();
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) UserModel(com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) UserAccessModel(com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserAccessModel) User(com.serotonin.m2m2.vo.User) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with UserModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel 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 4 with UserModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel 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 5 with UserModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel 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)

Aggregations

UserModel (com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel)21 User (com.serotonin.m2m2.vo.User)17 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)14 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)14 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)11 ArrayList (java.util.ArrayList)7 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)5 AccessDeniedException (com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException)4 List (java.util.List)4 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)3 ResponseEntity (org.springframework.http.ResponseEntity)3 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)3 AuthenticationException (org.springframework.security.core.AuthenticationException)3 MvcResult (org.springframework.test.web.servlet.MvcResult)3 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)2 JsonViews (com.serotonin.m2m2.web.mvc.rest.v1.mapping.JsonViews)2 UserAccessModel (com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserAccessModel)2 URI (java.net.URI)2 InvalidRQLRestException (com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException)1 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)1