Exemplo n.º 1
0
 /** PUT /users -> Updates an existing User. */
 @RequestMapping(
     value = "/users",
     method = RequestMethod.PUT,
     produces = MediaType.APPLICATION_JSON_VALUE)
 @Timed
 @Transactional
 @Secured(AuthoritiesConstants.ADMIN)
 public ResponseEntity<ManagedUserDTO> updateUser(@RequestBody ManagedUserDTO managedUserDTO)
     throws URISyntaxException {
   log.debug("REST request to update User : {}", managedUserDTO);
   return Optional.of(userRepository.findOne(managedUserDTO.getId()))
       .map(
           user -> {
             user.setLogin(managedUserDTO.getLogin());
             user.setFirstName(managedUserDTO.getFirstName());
             user.setLastName(managedUserDTO.getLastName());
             user.setEmail(managedUserDTO.getEmail());
             user.setActivated(managedUserDTO.isActivated());
             user.setLangKey(managedUserDTO.getLangKey());
             Set<Authority> authorities = user.getAuthorities();
             authorities.clear();
             managedUserDTO
                 .getAuthorities()
                 .stream()
                 .forEach(authority -> authorities.add(authorityRepository.findOne(authority)));
             return ResponseEntity.ok()
                 .headers(HeaderUtil.createEntityUpdateAlert("user", managedUserDTO.getLogin()))
                 .body(new ManagedUserDTO(userRepository.findOne(managedUserDTO.getId())));
           })
       .orElseGet(() -> new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR));
 }
 /**
  * PUT /users : Updates an existing User.
  *
  * @param managedUserDTO the user to update
  * @return the ResponseEntity with status 200 (OK) and with body the updated user, or with status
  *     400 (Bad Request) if the login or email is already in use, or with status 500 (Internal
  *     Server Error) if the user couldnt be updated
  */
 @RequestMapping(
     value = "/users",
     method = RequestMethod.PUT,
     produces = MediaType.APPLICATION_JSON_VALUE)
 @Timed
 @Transactional
 @Secured(AuthoritiesConstants.ADMIN)
 public ResponseEntity<ManagedUserDTO> updateUser(@RequestBody ManagedUserDTO managedUserDTO) {
   log.debug("REST request to update User : {}", managedUserDTO);
   Optional<User> existingUser = userRepository.findOneByEmail(managedUserDTO.getEmail());
   if (existingUser.isPresent() && (!existingUser.get().getId().equals(managedUserDTO.getId()))) {
     return ResponseEntity.badRequest()
         .headers(
             HeaderUtil.createFailureAlert(
                 "userManagement", "emailexists", "E-mail already in use"))
         .body(null);
   }
   existingUser = userRepository.findOneByLogin(managedUserDTO.getLogin().toLowerCase());
   if (existingUser.isPresent() && (!existingUser.get().getId().equals(managedUserDTO.getId()))) {
     return ResponseEntity.badRequest()
         .headers(
             HeaderUtil.createFailureAlert("userManagement", "userexists", "Login already in use"))
         .body(null);
   }
   return userRepository
       .findOneById(managedUserDTO.getId())
       .map(
           user -> {
             user.setLogin(managedUserDTO.getLogin());
             user.setFirstName(managedUserDTO.getFirstName());
             user.setLastName(managedUserDTO.getLastName());
             user.setEmail(managedUserDTO.getEmail());
             user.setActivated(managedUserDTO.isActivated());
             user.setLangKey(managedUserDTO.getLangKey());
             Set<Authority> authorities = user.getAuthorities();
             authorities.clear();
             managedUserDTO
                 .getAuthorities()
                 .stream()
                 .forEach(authority -> authorities.add(authorityRepository.findOne(authority)));
             return ResponseEntity.ok()
                 .headers(
                     HeaderUtil.createAlert("userManagement.updated", managedUserDTO.getLogin()))
                 .body(new ManagedUserDTO(userRepository.findOne(managedUserDTO.getId())));
           })
       .orElseGet(() -> new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR));
 }