UserController.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package it.bgates.remotebe.controller;
  2. import it.bgates.remotebe.config.BGatesUserDetails;
  3. import it.bgates.remotebe.controller.auth.beans.NewUserBean;
  4. import it.bgates.remotebe.entities.User;
  5. import it.bgates.remotebe.exception.PermissionDeniedException;
  6. import it.bgates.remotebe.exception.UserNotFoundException;
  7. import it.bgates.remotebe.service.UserService;
  8. import it.bgates.remotebe.service.auth.AuthService;
  9. import jakarta.validation.Valid;
  10. import lombok.RequiredArgsConstructor;
  11. import org.springframework.http.ResponseEntity;
  12. import org.springframework.web.bind.annotation.*;
  13. import java.security.Principal;
  14. import java.util.List;
  15. import static org.springframework.http.HttpStatus.*;
  16. @RestController
  17. @RequiredArgsConstructor
  18. @RequestMapping("/users")
  19. public class UserController {
  20. private final AuthService authService;
  21. private final UserService userService;
  22. /***
  23. *
  24. * @param principal
  25. * @return return the user information for the currently logged user
  26. */
  27. @GetMapping("/current-user")
  28. public ResponseEntity<BGatesUserDetails> getCurrentUser(Principal principal) {
  29. BGatesUserDetails user = authService.getCurrentUser();
  30. return ResponseEntity
  31. .status(OK)
  32. .body(user);
  33. }
  34. @GetMapping("")
  35. public ResponseEntity<List<User>> getUsers(Principal principal) {
  36. try {
  37. List<User> users = userService.getUsers(principal);
  38. return ResponseEntity
  39. .status(OK)
  40. .body(users);
  41. } catch (UserNotFoundException e) {
  42. return ResponseEntity.status(PRECONDITION_FAILED).build();
  43. }
  44. }
  45. @PostMapping("")
  46. public ResponseEntity<User> saveUser(@Valid @RequestBody NewUserBean newUser, Principal principal) {
  47. if (newUser.getId() == null && !userService.canCreateUsers(principal)) {
  48. return ResponseEntity.status(FORBIDDEN).build();
  49. }
  50. try {
  51. User savedUser = userService.save(newUser, principal);
  52. return ResponseEntity
  53. .status(OK)
  54. .body(savedUser);
  55. } catch (PermissionDeniedException e) {
  56. return ResponseEntity.status(FORBIDDEN).build();
  57. } catch (UserNotFoundException e) {
  58. return ResponseEntity.status(PRECONDITION_FAILED).build();
  59. }
  60. }
  61. @PostMapping("disable/{id}")
  62. public ResponseEntity<Boolean> disableUser(@PathVariable() Integer id, Principal principal) {
  63. Boolean result = null;
  64. try {
  65. result = userService.disableUser(id, principal);
  66. return ResponseEntity
  67. .status(OK)
  68. .body(result);
  69. } catch (UserNotFoundException e) {
  70. return ResponseEntity.status(PRECONDITION_FAILED).build();
  71. } catch (PermissionDeniedException e) {
  72. return ResponseEntity.status(FORBIDDEN).build();
  73. }
  74. }
  75. }