Guest User

Untitled

a guest
Jul 24th, 2016
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. package model;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.HashSet;
  5. import java.util.Set;
  6.  
  7. import javax.persistence.*;
  8. import javax.validation.constraints.NotNull;
  9.  
  10. import org.springframework.security.core.GrantedAuthority;
  11. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  12. import org.springframework.security.core.userdetails.UserDetails;
  13. /**
  14. * Created by Karol on 24.07.2016.
  15. */
  16. @Entity(name = "users")
  17. @Table(name = "users")
  18. public class UserEntity extends AbstractEntity implements UserDetails {
  19.  
  20. @NotNull
  21. private String username;
  22.  
  23. @NotNull
  24. private String password;
  25.  
  26. @ManyToMany(mappedBy = "users", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  27. private Set<RoleEntity> userRoleEntity = new HashSet<>();
  28.  
  29. public UserEntity(String username, String password, Set<RoleEntity> userRoleEntity) {
  30. super();
  31. this.username = username;
  32. this.password = password;
  33. this.userRoleEntity = userRoleEntity;
  34. }
  35.  
  36. public Set<RoleEntity> getUserRoleEntity() {
  37. return userRoleEntity;
  38. }
  39.  
  40. public void setUserRoleEntity(Set<RoleEntity> userRoleEntity) {
  41. this.userRoleEntity = userRoleEntity;
  42. }
  43.  
  44. public UserEntity() {
  45. super();
  46. // TODO Auto-generated constructor stub
  47. }
  48.  
  49. public String getUsername() {
  50. return username;
  51. }
  52.  
  53. public void setUsername(String username) {
  54. this.username = username;
  55. }
  56.  
  57. public String getPassword() {
  58. return password;
  59. }
  60.  
  61. public void setPassword(String password) {
  62. this.password = password;
  63. }
  64.  
  65. @Transient
  66. @Override
  67. public Collection<? extends GrantedAuthority> getAuthorities() {
  68.  
  69. Collection<GrantedAuthority> authorities = new ArrayList<>();
  70. Set<RoleEntity> userRoleEntities = this.getUserRoleEntity();
  71.  
  72. if (userRoleEntities != null) {
  73. for (RoleEntity roleEntity : userRoleEntities) {
  74. SimpleGrantedAuthority authority = new SimpleGrantedAuthority(roleEntity.getRoleName());
  75. authorities.add(authority);
  76. }
  77. }
  78. return authorities;
  79. }
  80.  
  81. @Transient
  82. @Override
  83. public boolean isAccountNonExpired() {
  84. // TODO Auto-generated method stub
  85. return true;
  86. }
  87.  
  88. @Transient
  89. @Override
  90. public boolean isAccountNonLocked() {
  91. // TODO Auto-generated method stub
  92. return true;
  93. }
  94.  
  95. @Transient
  96. @Override
  97. public boolean isCredentialsNonExpired() {
  98. // TODO Auto-generated method stub
  99. return true;
  100. }
  101.  
  102. @Transient
  103. @Override
  104. public boolean isEnabled() {
  105. // TODO Auto-generated method stub
  106. return true;
  107. }
  108.  
  109. @Override
  110. public boolean equals(Object o) {
  111. if (this == o) return true;
  112. if (o == null || getClass() != o.getClass()) return false;
  113. if (!super.equals(o)) return false;
  114.  
  115. UserEntity that = (UserEntity) o;
  116.  
  117. if (username != null ? !username.equals(that.username) : that.username != null) return false;
  118. return password != null ? password.equals(that.password) : that.password == null;
  119.  
  120. }
  121.  
  122. @Override
  123. public int hashCode() {
  124. int result = super.hashCode();
  125. result = 31 * result + (username != null ? username.hashCode() : 0);
  126. result = 31 * result + (password != null ? password.hashCode() : 0);
  127. return result;
  128. }
  129. }
Add Comment
Please, Sign In to add comment