Guest User

Untitled

a guest
Oct 2nd, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. @Transactional(readOnly = true)
  2. public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
  3. return new UserDTO(
  4. userRepository.findByEmailAddress(email)
  5. .orElseThrow(() -> new UsernameNotFoundException("User '" + email + "' not found."))
  6. );
  7. }
  8.  
  9. @Entity
  10. @Table(name = "USERS")
  11. @NoArgsConstructor
  12. @AllArgsConstructor
  13. @Getter
  14. @Setter
  15. @Builder
  16. public class User extends BaseEntity {
  17.  
  18. private String firstName;
  19. private String lastName;
  20. private String emailAddress;
  21. private String nickname;
  22. private String password;
  23. private boolean accountNonExpired;
  24. private boolean accountNonLocked;
  25. private boolean credentialsNonExpired;
  26. private boolean enabled;
  27.  
  28. @ManyToMany
  29. @JoinTable(
  30. name = "USER_ROLE",
  31. joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
  32. inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
  33. private Set<Role> roles;
  34.  
  35. public Set<Role> getRoles() {
  36. return roles;
  37. }
  38. }
  39.  
  40. @Getter
  41. @Setter
  42. @NoArgsConstructor
  43. public class UserDTO implements UserDetails {
  44. private Long id;
  45. private String firstName;
  46. private String lastName;
  47. private String username;
  48. private String nickname;
  49. private String password;
  50. private Set<RoleDTO> authorities;
  51. private boolean accountNonExpired;
  52. private boolean accountNonLocked;
  53. private boolean credentialsNonExpired;
  54. private boolean enabled;
  55.  
  56. public UserDTO(User user){
  57. this.id = user.getId();
  58. this.firstName = user.getFirstName();
  59. this.lastName = user.getLastName();
  60. this.username = user.getEmailAddress();
  61. this.nickname = user.getNickname();
  62. this.password = user.getPassword();
  63. this.authorities = user.getRoles().stream().map(RoleDTO::new).collect(Collectors.toSet());
  64. this.accountNonExpired = user.isAccountNonExpired();
  65. this.accountNonLocked = user.isAccountNonLocked();
  66. this.credentialsNonExpired = user.isCredentialsNonExpired();
  67. this.enabled = user.isEnabled();
  68. }
  69.  
  70. }
  71.  
  72. INSERT INTO PUBLIC.ROLES(ID, VERSION, AUTHORITY) VALUES
  73. (1, 0, 'ROLE_USER'),
  74. (2, 0, 'ROLE_MODERATOR'),
  75. (3, 0, 'ROLE_ADMIN');
  76.  
  77. @PostMapping
  78. @ResponseStatus(HttpStatus.CREATED)
  79. public UserSimpleDTO createUser(@RequestBody UserDTO user){
  80. return userService.createUser(user);
  81. }
  82.  
  83. @GetMapping("/{id}")
  84. @ResponseStatus(HttpStatus.OK)
  85. public UserSimpleDTO getUserGeneralData(@PathVariable long id){
  86. return userService.getUserGeneralData(id);
  87. }
  88.  
  89. @GetMapping("/{id}/details")
  90. @ResponseStatus(HttpStatus.OK)
  91. @RolesAllowed("ROLE_MODERATOR")
  92. public UserDTO getUserDetailedInfo(@PathVariable long id) {
  93. return userService.getUserDetailedInfo(id);
  94. }
  95.  
  96. @Entity
  97. @Table(name = "ROLES")
  98. @Getter
  99. @NoArgsConstructor
  100. public class Role extends BaseEntity implements GrantedAuthority {
  101.  
  102. private String authority;
  103.  
  104. }
  105.  
  106. @Getter
  107. @NoArgsConstructor
  108. public class RoleDTO implements GrantedAuthority {
  109.  
  110. private String authority;
  111.  
  112. public RoleDTO(Role role){
  113. this.authority = role.getAuthority();
  114. }
  115. }
Add Comment
Please, Sign In to add comment