PageRenderTime 108ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/PollBox/src/main/java/org/pollbox/poll/auth/User.java

https://github.com/oburakevych/Integration-Payments
Java | 257 lines | 152 code | 42 blank | 63 comment | 20 complexity | 6b1b51a2e6a33e94956b98f8cafd2d72 MD5 | raw file
  1. package org.pollbox.poll.auth;
  2. import java.io.Serializable;
  3. import java.util.Collection;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6. import java.util.Locale;
  7. import java.util.Set;
  8. import java.util.SortedSet;
  9. import java.util.TreeSet;
  10. import org.pollbox.poll.owners.Owner;
  11. import org.springframework.security.core.GrantedAuthority;
  12. import org.springframework.security.core.CredentialsContainer;
  13. import org.springframework.security.core.userdetails.UserDetails;
  14. import org.springframework.util.Assert;
  15. public class User implements UserDetails, CredentialsContainer {
  16. private static final long serialVersionUID = 1L;
  17. //~ Instance fields of JDBC User implementation ====================================================================
  18. private String password;
  19. private final String username;
  20. private final Set<GrantedAuthority> authorities;
  21. private final boolean accountNonExpired;
  22. private final boolean accountNonLocked;
  23. private final boolean credentialsNonExpired;
  24. private final boolean enabled;
  25. private Locale locale;
  26. private Owner owner;
  27. // Custom fields to match to the current implementation
  28. private Long accountId;
  29. //~ Constructors ===================================================================================================
  30. /**
  31. * Construct the <code>User</code> with the details required by
  32. * {@link org.springframework.security.authentication.dao.DaoAuthenticationProvider}.
  33. *
  34. * @param username the username presented to the
  35. * <code>DaoAuthenticationProvider</code>
  36. * @param password the password that should be presented to the
  37. * <code>DaoAuthenticationProvider</code>
  38. * @param enabled set to <code>true</code> if the user is enabled
  39. * @param accountNonExpired set to <code>true</code> if the account has not
  40. * expired
  41. * @param credentialsNonExpired set to <code>true</code> if the credentials
  42. * have not expired
  43. * @param accountNonLocked set to <code>true</code> if the account is not
  44. * locked
  45. * @param authorities the authorities that should be granted to the caller
  46. * if they presented the correct username and password and the user
  47. * is enabled. Not null.
  48. *
  49. * @throws IllegalArgumentException if a <code>null</code> value was passed
  50. * either as a parameter or as an element in the
  51. * <code>GrantedAuthority</code> collection
  52. */
  53. public User(String username, String password, boolean enabled, boolean accountNonExpired,
  54. boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
  55. if (((username == null) || "".equals(username)) || (password == null)) {
  56. throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
  57. }
  58. this.username = username;
  59. this.password = password;
  60. this.enabled = enabled;
  61. this.accountNonExpired = accountNonExpired;
  62. this.credentialsNonExpired = credentialsNonExpired;
  63. this.accountNonLocked = accountNonLocked;
  64. this.authorities = Collections.unmodifiableSet(sortAuthorities(authorities));
  65. }
  66. /**
  67. * Construct the <code>User</code> with the details required by
  68. * {@link org.springframework.security.authentication.dao.DaoAuthenticationProvider}.
  69. *
  70. * @param username the username presented to the
  71. * <code>DaoAuthenticationProvider</code>
  72. * @param password the password that should be presented to the
  73. * <code>DaoAuthenticationProvider</code>
  74. * @param enabled set to <code>true</code> if the user is enabled
  75. * @param accountNonExpired set to <code>true</code> if the account has not
  76. * expired
  77. * @param credentialsNonExpired set to <code>true</code> if the credentials
  78. * have not expired
  79. * @param accountNonLocked set to <code>true</code> if the account is not
  80. * locked
  81. * @param authorities the authorities that should be granted to the caller
  82. * if they presented the correct username and password and the user
  83. * is enabled. Not null.
  84. *
  85. * @throws IllegalArgumentException if a <code>null</code> value was passed
  86. * either as a parameter or as an element in the
  87. * <code>GrantedAuthority</code> collection
  88. */
  89. public User(String username, String password, boolean enabled, boolean accountNonExpired,
  90. boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities, Long account, Owner owner, Locale locale) {
  91. if (((username == null) || "".equals(username)) || (password == null)) {
  92. throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
  93. }
  94. this.username = username;
  95. this.password = password;
  96. this.enabled = enabled;
  97. this.accountNonExpired = accountNonExpired;
  98. this.credentialsNonExpired = credentialsNonExpired;
  99. this.accountNonLocked = accountNonLocked;
  100. this.authorities = Collections.unmodifiableSet(sortAuthorities(authorities));
  101. this.accountId = account;
  102. this.owner = owner;
  103. this.locale = locale;
  104. }
  105. //~ Methods ========================================================================================================
  106. public Collection<GrantedAuthority> getAuthorities() {
  107. return authorities;
  108. }
  109. public String getPassword() {
  110. return password;
  111. }
  112. public String getUsername() {
  113. return username;
  114. }
  115. public boolean isEnabled() {
  116. return enabled;
  117. }
  118. public boolean isAccountNonExpired() {
  119. return accountNonExpired;
  120. }
  121. public boolean isAccountNonLocked() {
  122. return accountNonLocked;
  123. }
  124. public boolean isCredentialsNonExpired() {
  125. return credentialsNonExpired;
  126. }
  127. public void eraseCredentials() {
  128. password = null;
  129. }
  130. private static SortedSet<GrantedAuthority> sortAuthorities(Collection<? extends GrantedAuthority> authorities) {
  131. Assert.notNull(authorities, "Cannot pass a null GrantedAuthority collection");
  132. // Ensure array iteration order is predictable (as per UserDetails.getAuthorities() contract and SEC-717)
  133. SortedSet<GrantedAuthority> sortedAuthorities =
  134. new TreeSet<GrantedAuthority>(new AuthorityComparator());
  135. for (GrantedAuthority grantedAuthority : authorities) {
  136. Assert.notNull(grantedAuthority, "GrantedAuthority list cannot contain any null elements");
  137. sortedAuthorities.add(grantedAuthority);
  138. }
  139. return sortedAuthorities;
  140. }
  141. private static class AuthorityComparator implements Comparator<GrantedAuthority>, Serializable {
  142. public int compare(GrantedAuthority g1, GrantedAuthority g2) {
  143. // Neither should ever be null as each entry is checked before adding it to the set.
  144. // If the authority is null, it is a custom authority and should precede others.
  145. if (g2.getAuthority() == null) {
  146. return -1;
  147. }
  148. if (g1.getAuthority() == null) {
  149. return 1;
  150. }
  151. return g1.getAuthority().compareTo(g2.getAuthority());
  152. }
  153. }
  154. /**
  155. * Returns {@code true} if the supplied object is a {@code User} instance with the
  156. * same {@code username} value.
  157. * <p>
  158. * In other words, the objects are equal if they have the same username, representing the
  159. * same principal.
  160. */
  161. @Override
  162. public boolean equals(Object rhs) {
  163. if (rhs instanceof User) {
  164. return username.equals(((User) rhs).username);
  165. }
  166. return false;
  167. }
  168. /**
  169. * Returns the hashcode of the {@code username}.
  170. */
  171. @Override
  172. public int hashCode() {
  173. return username.hashCode();
  174. }
  175. @Override
  176. public String toString() {
  177. StringBuilder sb = new StringBuilder();
  178. sb.append(super.toString()).append(": ");
  179. sb.append("Username: ").append(this.username).append("; ");
  180. sb.append("Password: [PROTECTED]; ");
  181. sb.append("Enabled: ").append(this.enabled).append("; ");
  182. sb.append("AccountNonExpired: ").append(this.accountNonExpired).append("; ");
  183. sb.append("credentialsNonExpired: ").append(this.credentialsNonExpired).append("; ");
  184. sb.append("AccountNonLocked: ").append(this.accountNonLocked).append("; ");
  185. if (!authorities.isEmpty()) {
  186. sb.append("Granted Authorities: ");
  187. boolean first = true;
  188. for (GrantedAuthority auth : authorities) {
  189. if (!first) {
  190. sb.append(",");
  191. }
  192. first = false;
  193. sb.append(auth);
  194. }
  195. } else {
  196. sb.append("Not granted any authorities");
  197. }
  198. return sb.toString();
  199. }
  200. public Long getAccountId() {
  201. return accountId;
  202. }
  203. public void setOwner(Owner owner) {
  204. this.owner = owner;
  205. }
  206. public Owner getOwner() {
  207. return owner;
  208. }
  209. public void setLocale(Locale locale) {
  210. this.locale = locale;
  211. }
  212. public Locale getLocale() {
  213. return locale;
  214. }
  215. }