Advertisement
Guest User

Untitled

a guest
May 31st, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.91 KB | None | 0 0
  1. package pl.ekstrastats.api.user;
  2.  
  3. import com.fasterxml.jackson.annotation.JsonIgnore;
  4. import com.fasterxml.jackson.annotation.JsonProperty;
  5. import org.springframework.security.core.userdetails.UserDetails;
  6.  
  7. import javax.persistence.*;
  8. import javax.validation.constraints.NotNull;
  9. import javax.validation.constraints.Size;
  10. import java.util.Date;
  11. import java.util.EnumSet;
  12. import java.util.HashSet;
  13. import java.util.Set;
  14.  
  15. @Entity
  16. @Table(name = "users")
  17. public class User implements UserDetails {
  18.  
  19.     public User() {
  20.     }
  21.  
  22.     public User(String username) {
  23.         this.username = username;
  24.     }
  25.  
  26.     public User(String username, Date expires) {
  27.         this.username = username;
  28.         this.expires = expires.getTime();
  29.     }
  30.  
  31.     @Id
  32.     @GeneratedValue(strategy = GenerationType.TABLE)
  33.     private Long id;
  34.  
  35.     @NotNull
  36.     @Size(min = 4, max = 30)
  37.     private String username;
  38.  
  39.     @NotNull
  40.     @Size(min = 4, max = 100)
  41.     private String password;
  42.  
  43.     @Transient
  44.     private long expires;
  45.  
  46.     @NotNull
  47.     private boolean accountExpired;
  48.  
  49.     @NotNull
  50.     private boolean accountLocked;
  51.  
  52.     @NotNull
  53.     private boolean credentialsExpired;
  54.  
  55.     @NotNull
  56.     private boolean accountEnabled;
  57.  
  58.     @Transient
  59.     private String newPassword;
  60.  
  61.     @OneToMany(cascade = CascadeType.ALL, mappedBy = "user", fetch = FetchType.EAGER, orphanRemoval = true)
  62.     private Set<UserAuthority> authorities;
  63.  
  64.     public Long getId() {
  65.         return id;
  66.     }
  67.  
  68.     public void setId(Long id) {
  69.         this.id = id;
  70.     }
  71.  
  72.     @Override
  73.     public String getUsername() {
  74.         return username;
  75.     }
  76.  
  77.     public void setUsername(String username) {
  78.         this.username = username;
  79.     }
  80.  
  81.     @Override
  82.     @JsonIgnore
  83.     public String getPassword() {
  84.         return password;
  85.     }
  86.  
  87.     @JsonProperty
  88.     public void setPassword(String password) {
  89.         this.password = password;
  90.     }
  91.  
  92.     @JsonIgnore
  93.     public String getNewPassword() {
  94.         return newPassword;
  95.     }
  96.  
  97.     @JsonProperty
  98.     public void setNewPassword(String newPassword) {
  99.         this.newPassword = newPassword;
  100.     }
  101.  
  102.     @Override
  103.     @JsonIgnore
  104.     public Set<UserAuthority> getAuthorities() {
  105.         return authorities;
  106.     }
  107.  
  108.     public Set<UserRole> getRoles() {
  109.         Set<UserRole> roles = EnumSet.noneOf(UserRole.class);
  110.         if (authorities != null) {
  111.             for (UserAuthority authority : authorities) {
  112.                 roles.add(UserRole.valueOf(authority));
  113.             }
  114.         }
  115.         return roles;
  116.     }
  117.  
  118.     public void setRoles(Set<UserRole> roles) {
  119.         for (UserRole role : roles) {
  120.             grantRole(role);
  121.         }
  122.     }
  123.  
  124.     public void grantRole(UserRole role) {
  125.         if (authorities == null) {
  126.             authorities = new HashSet<UserAuthority>();
  127.         }
  128.         authorities.add(role.asAuthorityFor(this));
  129.     }
  130.  
  131.     public void revokeRole(UserRole role) {
  132.         if (authorities != null) {
  133.             authorities.remove(role.asAuthorityFor(this));
  134.         }
  135.     }
  136.  
  137.     public boolean hasRole(UserRole role) {
  138.         return authorities.contains(role.asAuthorityFor(this));
  139.     }
  140.  
  141.     @Override
  142.     @JsonIgnore
  143.     public boolean isAccountNonExpired() {
  144.         return !accountExpired;
  145.     }
  146.  
  147.     @Override
  148.     @JsonIgnore
  149.     public boolean isAccountNonLocked() {
  150.         return !accountLocked;
  151.     }
  152.  
  153.     @Override
  154.     @JsonIgnore
  155.     public boolean isCredentialsNonExpired() {
  156.         return !credentialsExpired;
  157.     }
  158.  
  159.     @Override
  160.     @JsonIgnore
  161.     public boolean isEnabled() {
  162.         return !accountEnabled;
  163.     }
  164.  
  165.     public long getExpires() {
  166.         return expires;
  167.     }
  168.  
  169.     public void setExpires(long expires) {
  170.         this.expires = expires;
  171.     }
  172.  
  173.     @Override
  174.     public String toString() {
  175.         return getClass().getSimpleName() + ": " + getUsername();
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement