Search in sources :

Example 11 with UserModel

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

the class UserRestController method patchUser.

@ApiOperation(value = "Partially update a User", notes = "Admin or Patch Self only", response = UserModel.class)
@RequestMapping(method = RequestMethod.PATCH, value = "/{username}")
public ResponseEntity<UserModel> patchUser(@PathVariable String username, @ApiParam(value = "User", required = true) @PatchVORequestBody(service = UsersService.class, modelClass = UserModel.class, idType = PatchIdField.OTHER, urlPathVariableName = "username") UserModel model, @AuthenticationPrincipal PermissionHolder user, HttpServletRequest request, UriComponentsBuilder builder, Authentication authentication) {
    User existing = service.get(username);
    User currentUser = user.getUser();
    if (currentUser != null && existing.getId() == currentUser.getId() && !(authentication instanceof UsernamePasswordAuthenticationToken))
        throw new PermissionException(new TranslatableMessage("rest.error.usernamePasswordOnly"), user);
    User update = service.update(existing.getId(), model.toVO());
    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 12 with UserModel

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

the class UserRestController method createUser.

@ApiOperation(value = "Create User", notes = "Superadmin permission required", response = UserModel.class)
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<UserModel> createUser(@ApiParam(value = "User", required = true) @RequestBody UserModel model, UriComponentsBuilder builder) {
    User newUser = service.insert(model.toVO());
    URI location = builder.path("/users/{username}").buildAndExpand(newUser.getUsername()).toUri();
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(location);
    return new ResponseEntity<>(new UserModel(newUser), headers, HttpStatus.OK);
}
Also used : 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) URI(java.net.URI) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with UserModel

use of com.infiniteautomation.mango.rest.latest.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, value = "/{username}/mute")
public ResponseEntity<UserModel> updateMuted(@ApiParam(value = "Username", required = true) @PathVariable String username, @ApiParam(value = "Mute", defaultValue = "Toggle the current setting") @RequestParam(required = false) Boolean mute, HttpServletRequest request, UriComponentsBuilder builder, @AuthenticationPrincipal PermissionHolder user, 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);
    if (mute == null) {
        update.setMuted(!update.isMuted());
    } else {
        update.setMuted(mute);
    }
    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 14 with UserModel

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

the class SystemEventTypeModelMapping method map.

@Override
public SystemEventTypeModel map(Object from, PermissionHolder user, RestModelMapper mapper) {
    SystemEventType type = (SystemEventType) from;
    if (StringUtils.equals(SystemEventType.TYPE_USER_LOGIN, type.getEventSubtype())) {
        UserModel userModel = null;
        User u = UserDao.getInstance().get(type.getReferenceId1());
        if (u != null)
            userModel = new UserModel(u);
        return new SystemEventTypeModel(type, userModel);
    } else
        return new SystemEventTypeModel(type);
}
Also used : UserModel(com.infiniteautomation.mango.rest.latest.model.user.UserModel) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) User(com.serotonin.m2m2.vo.User)

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