package net.okjsp.user.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityCoreVersion;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.Assert;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.*;
/**
* User: langerhans
* Date: 2013. 12. 31.
* Time: 오후 5:09
* Description : User
*/
public class User implements UserDetails, CredentialsContainer {
private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
private int userId;
@NotBlank @Email
private String email;
// @NotBlank @Pattern(regexp = "^(?=.*\\d)(?=.*[A-Za-z]).{6,20}$")
private String password;
private String passwordConfirm;
@NotBlank @Size(min = 2, max = 50)
private String name;
@NotBlank @Size(max = 50)
private String nickName;
@Pattern(regexp = "^[\\d]{2,4}\\-[\\d]{2,4}\\-[\\d]{4}$")
private String tel;
private String profileImg;
private String letterAgr;
private String grade;
@DateTimeFormat(style = "MM", pattern = "yyyy-MM-dd")
private Date joinDate;
@DateTimeFormat(style = "MM", pattern = "yyyy-MM-dd")
private Date updateDate;
@DateTimeFormat(style = "MM", pattern = "yyyy-MM-dd")
private Date delDate;
private Set<GrantedAuthority> authorities;
private boolean accountNonExpired = true;
private boolean accountNonLocked = true;
private boolean credentialsNonExpired = true;
private boolean enabled = true;
private static final String salt = "!okjspPwSalt*";
@JsonIgnore
public String getSalt() {
return salt;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return authorities;
}
@Override
@JsonIgnore
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return email;
}
@Override
@JsonIgnore
public boolean isAccountNonExpired() {
return accountNonExpired;
}
@Override
@JsonIgnore
public boolean isAccountNonLocked() {
return accountNonLocked;
}
@Override
@JsonIgnore
public boolean isCredentialsNonExpired() {
return credentialsNonExpired;
}
@Override
@JsonIgnore
public boolean isEnabled() {
return accountNonExpired;
}
public void setPassword(String password) {
this.password = password;
}
@JsonIgnore
public String getPasswordConfirm() {
return passwordConfirm;
}
public void setPasswordConfirm(String passwordConfirm) {
this.passwordConfirm = passwordConfirm;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getProfileImg() {
return profileImg;
}
public void setProfileImg(String profileImg) {
this.profileImg = profileImg;
}
public String getLetterAgr() {
return letterAgr;
}
public void setLetterAgr(String letterAgr) {
this.letterAgr = letterAgr;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public Date getJoinDate() {
return joinDate;
}
public void setJoinDate(Date joinDate) {
this.joinDate = joinDate;
}
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
public Date getDelDate() {
return delDate;
}
public void setDelDate(Date delDate) {
this.delDate = delDate;
}
public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
this.authorities = Collections.unmodifiableSet(sortAuthorities(authorities));;
}
public void setAuthorities(Set<GrantedAuthority> authorities) {
this.authorities = Collections.unmodifiableSet(sortAuthorities(authorities));;
}
public void eraseCredentials() {
password = null;
}
private static SortedSet<GrantedAuthority> sortAuthorities(Collection<? extends GrantedAuthority> authorities) {
Assert.notNull(authorities, "Cannot pass a null GrantedAuthority collection");
// Ensure array iteration order is predictable (as per UserDetails.getAuthorities() contract and SEC-717)
SortedSet<GrantedAuthority> sortedAuthorities =
new TreeSet<GrantedAuthority>(new AuthorityComparator());
for (GrantedAuthority grantedAuthority : authorities) {
Assert.notNull(grantedAuthority, "GrantedAuthority list cannot contain any null elements");
sortedAuthorities.add(grantedAuthority);
}
return sortedAuthorities;
}
private static class AuthorityComparator implements Comparator<GrantedAuthority>, Serializable {
private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
public int compare(GrantedAuthority g1, GrantedAuthority g2) {
// Neither should ever be null as each entry is checked before adding it to the set.
// If the authority is null, it is a custom authority and should precede others.
if (g2.getAuthority() == null) {
return -1;
}
if (g1.getAuthority() == null) {
return 1;
}
return g1.getAuthority().compareTo(g2.getAuthority());
}
}
/**
* Returns {@code true} if the supplied object is a {@code User} instance with the
* same {@code username} value.
* <p>
* In other words, the objects are equal if they have the same username, representing the
* same principal.
*/
@Override
public boolean equals(Object rhs) {
if (rhs instanceof User) {
return email.equals(((User) rhs).email);
}
return false;
}
/**
* Returns the hashcode of the {@code username}.
*/
@Override
public int hashCode() {
return email.hashCode();
}
}