View Javadoc
1   package cn.home1.oss.lib.security.api;
2   
3   import com.google.common.base.MoreObjects;
4   import com.google.common.collect.ImmutableMap;
5   
6   import com.fasterxml.jackson.annotation.JsonIgnore;
7   
8   import java.util.Map;
9   
10  /**
11   * Created by zhanghaolun on 16/10/28.
12   */
13  public abstract class AbstractUser implements User {
14  
15    /**
16     * default user type.
17     */
18    public static final String USER_TYPE_USER = "UT_USER";
19  
20    public void disable() {
21      this.setEnabled(false);
22    }
23  
24    @Override
25    public void enable() {
26      this.setEnabled(true);
27    }
28  
29    @Override
30    public void eraseCredentials() {
31      this.setPassword(null);
32    }
33  
34    @Override
35    public Map<String, String> getProperties() {
36      return ImmutableMap.of();
37    }
38  
39    @Override
40    public String getType() {
41      return USER_TYPE_USER;
42    }
43  
44    @Override
45    public String getUsername() {
46      return this.getName();
47    }
48  
49    @JsonIgnore
50    @Override
51    public boolean isAccountNonExpired() {
52      return this.isEnabled();
53    }
54  
55    @JsonIgnore
56    @Override
57    public boolean isAccountNonLocked() {
58      return this.isEnabled();
59    }
60  
61    @JsonIgnore
62    @Override
63    public boolean isCredentialsNonExpired() {
64      return this.isEnabled();
65    }
66  
67    protected abstract void setEnabled(boolean enabled);
68  
69    protected abstract void setPassword(String password);
70  
71    @Override
72    public boolean equals(final Object obj) {
73      final boolean result;
74      if (obj != null) {
75        if (this.getClass() == obj.getClass() || AbstractUser.class.isAssignableFrom(obj.getClass())) {
76          final AbstractUser rhs = (AbstractUser) obj;
77          result = this.getName().equals(rhs.getName());
78        } else {
79          result = false;
80        }
81      } else {
82        result = false;
83      }
84      return result;
85    }
86  
87    @Override
88    public int hashCode() {
89      return this.getName().hashCode();
90    }
91  
92    @Override
93    public String toString() {
94      return MoreObjects.toStringHelper(this.getClass()) //
95        .add("name", this.getName()) //
96        .add("enabled", this.isEnabled()) //
97        .toString();
98    }
99  }