fr.smallcrew.security.domain.DomainUser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of smallcrew-security Show documentation
Show all versions of smallcrew-security Show documentation
Foundation of all smallcrew's projects needing authenticated users and role management
The newest version!
package fr.smallcrew.security.domain;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Objects;
import com.google.common.base.Strings;
import fr.smallcrew.security.exception.InvalidEmailException;
import fr.smallcrew.security.exception.InvalidPasswordException;
import fr.smallcrew.security.exception.InvalidUsernameException;
import org.hibernate.validator.internal.constraintvalidators.EmailValidator;
import org.springframework.data.jpa.domain.AbstractPersistable;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.util.StringUtils;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Date;
import static com.google.common.base.Objects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
@Entity
@XmlRootElement
public class DomainUser extends AbstractPersistable {
private static final long serialVersionUID = -2624558074839859986L;
public static final String ID = "id";
public static final String USERNAME = "username";
public static final String EMAIL = "email";
public static final String PASSWORD = "password";
@NotNull
@Column(length = 64, unique = true)
private String username;
@NotNull
@Column(length = 96, unique = true)
private String email;
@NotNull
@Column(length = 512)
private String password;
@NotNull
@Column(length = 48)
private String passwordSalt;
@Column(length = 48)
private String passwordRequestToken;
@Temporal(TemporalType.TIMESTAMP)
private Date passwordRequestDate;
public DomainUser() {
}
@VisibleForTesting
public DomainUser(String username, String email, String password) {
this.username = username;
this.email = email;
this.password = password;
}
@VisibleForTesting
public DomainUser(Integer id, String username, String email, String newPassword) {
this(username, email, newPassword);
this.setId(id);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPasswordSalt() {
return passwordSalt;
}
public void setPasswordSalt(String passwordSalt) {
this.passwordSalt = passwordSalt;
}
public String getPasswordRequestToken() {
return passwordRequestToken;
}
public void setPasswordRequestToken(String passwordRequestToken) {
this.passwordRequestToken = passwordRequestToken;
}
public Date getPasswordRequestDate() {
return passwordRequestDate;
}
public void setPasswordRequestDate(Date passwordRequestDate) {
this.passwordRequestDate = passwordRequestDate;
}
@Override
public String toString() {
return toStringHelper(this)
.add(ID, getId())
.add(USERNAME, username)
.add(EMAIL, email)
.toString();
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof DomainUser)) {
return false;
}
DomainUser that = (DomainUser) obj;
return Objects.equal(getId(), that.getId()) &&
Objects.equal(username, that.username) &&
Objects.equal(email, that.email) &&
Objects.equal(password, that.password) &&
Objects.equal(passwordSalt, that.passwordSalt);
}
@Override
public int hashCode() {
return Objects.hashCode(getId(), password, passwordSalt, username, email);
}
public static class Builder {
private static final EmailValidator EMAIL_VALIDATOR = new EmailValidator();
private PasswordEncoder passwordEncoder;
private String username;
private String email;
private String password;
private String passwordSalt;
public Builder username(String username) {
this.username = username;
return this;
}
public Builder password(String password) {
this.password = password;
return this;
}
public Builder email(String email) {
this.email = email;
return this;
}
public Builder passwordEncoder(PasswordEncoder passwordEncoder) {
this.passwordEncoder = passwordEncoder;
return this;
}
public Builder passwordSalt(String passwordSalt) {
this.passwordSalt = passwordSalt;
return this;
}
public static String checkPassword(String password) {
password = StringUtils.trimTrailingWhitespace(password);
if (Strings.isNullOrEmpty(password) || password.length() < 6) {
throw new InvalidPasswordException();
}
return password;
}
public static String checkUsername(String username) {
username = StringUtils.trimTrailingWhitespace(username);
if (Strings.isNullOrEmpty(username)) {
throw new InvalidUsernameException();
}
return username;
}
public static String checkEMail(String email) {
if (Strings.isNullOrEmpty(email) || !EMAIL_VALIDATOR.isValid(email, null)) {
throw new InvalidEmailException();
}
return email;
}
public DomainUser build() {
checkNotNull(passwordEncoder, "Password encoder is not set");
checkNotNull(passwordSalt, "Password salt is not set");
checkEMail(email);
username = checkUsername(username);
password = checkPassword(password);
DomainUser user = new DomainUser();
user.setUsername(username);
user.setEmail(email);
user.setPassword(passwordEncoder.encodePassword(password, passwordSalt));
user.setPasswordSalt(passwordSalt);
return user;
}
}
}