219 lines
5.4 KiB
Java
219 lines
5.4 KiB
Java
package ru.ulstu.user.model;
|
|
|
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
|
import org.springframework.util.StringUtils;
|
|
import ru.ulstu.configuration.Constants;
|
|
import ru.ulstu.odin.model.OdinDto;
|
|
import ru.ulstu.odin.model.annotation.OdinCaption;
|
|
import ru.ulstu.odin.model.annotation.OdinReadOnly;
|
|
import ru.ulstu.odin.model.annotation.OdinString;
|
|
import ru.ulstu.odin.model.annotation.OdinVisible;
|
|
import ru.ulstu.user.controller.UserController;
|
|
|
|
import javax.validation.constraints.Email;
|
|
import javax.validation.constraints.NotBlank;
|
|
import javax.validation.constraints.Pattern;
|
|
import javax.validation.constraints.Size;
|
|
import java.util.Collection;
|
|
import java.util.Date;
|
|
import java.util.LinkedHashSet;
|
|
import java.util.Objects;
|
|
import java.util.Set;
|
|
import java.util.stream.Collectors;
|
|
|
|
import static ru.ulstu.odin.model.annotation.OdinString.OdinStringType.PASSWORD;
|
|
|
|
public class UserDto implements OdinDto {
|
|
@OdinReadOnly
|
|
private Integer id;
|
|
|
|
@NotBlank
|
|
@Pattern(regexp = Constants.LOGIN_REGEX)
|
|
@Size(min = 4, max = 50)
|
|
@OdinCaption("Логин")
|
|
private String login;
|
|
|
|
@NotBlank
|
|
@Size(min = 2, max = 50)
|
|
@OdinCaption("Имя")
|
|
private String firstName;
|
|
|
|
@NotBlank
|
|
@Size(min = 2, max = 50)
|
|
@OdinCaption("Фамилия")
|
|
private String lastName;
|
|
|
|
@Email
|
|
@NotBlank
|
|
@Size(min = 5, max = 100)
|
|
@OdinCaption("E-Mail")
|
|
private String email;
|
|
|
|
@OdinCaption("Аккаунт активен")
|
|
private boolean activated;
|
|
|
|
@OdinCaption("Роли")
|
|
private final LinkedHashSet<UserRoleDto> roles;
|
|
|
|
@OdinString(type = PASSWORD)
|
|
@OdinVisible(type = OdinVisible.OdinVisibleType.ON_UPDATE)
|
|
@OdinCaption("Текущий пароль")
|
|
@Size(max = 50)
|
|
private String oldPassword;
|
|
|
|
@OdinString(type = PASSWORD)
|
|
@OdinCaption("Пароль")
|
|
@Size(min = Constants.MIN_PASSWORD_LENGTH, max = 50)
|
|
private String password;
|
|
|
|
@OdinString(type = PASSWORD)
|
|
@OdinCaption("Пароль (подтверждение)")
|
|
@Size(min = Constants.MIN_PASSWORD_LENGTH, max = 50)
|
|
private String passwordConfirm;
|
|
|
|
private Date birthDate;
|
|
|
|
private User.UserDegree degree;
|
|
|
|
public UserDto() {
|
|
activated = false;
|
|
roles = new LinkedHashSet<>();
|
|
}
|
|
|
|
public UserDto(User user) {
|
|
this();
|
|
this.id = user.getId();
|
|
this.login = user.getLogin();
|
|
this.firstName = user.getFirstName();
|
|
this.lastName = user.getLastName();
|
|
this.email = user.getEmail();
|
|
this.activated = user.getActivated();
|
|
this.roles.addAll(user.getRoles().stream()
|
|
.map(UserRoleDto::new)
|
|
.collect(Collectors.toList()));
|
|
this.birthDate = user.getBirthDate();
|
|
this.degree = user.getDegree();
|
|
}
|
|
|
|
public Integer getId() {
|
|
return id;
|
|
}
|
|
|
|
@Override
|
|
public String getViewValue() {
|
|
return login;
|
|
}
|
|
|
|
@Override
|
|
public String getControllerPath() {
|
|
return UserController.URL;
|
|
}
|
|
|
|
public String getLogin() {
|
|
return login;
|
|
}
|
|
|
|
public void setLogin(String login) {
|
|
this.login = login;
|
|
}
|
|
|
|
public String getFirstName() {
|
|
return firstName;
|
|
}
|
|
|
|
public void setFirstName(String firstName) {
|
|
this.firstName = firstName;
|
|
}
|
|
|
|
public String getLastName() {
|
|
return lastName;
|
|
}
|
|
|
|
public void setLastName(String lastName) {
|
|
this.lastName = lastName;
|
|
}
|
|
|
|
public String getEmail() {
|
|
return email;
|
|
}
|
|
|
|
public void setEmail(String email) {
|
|
this.email = email;
|
|
}
|
|
|
|
public boolean isActivated() {
|
|
return activated;
|
|
}
|
|
|
|
public void setActivated(boolean activated) {
|
|
this.activated = activated;
|
|
}
|
|
|
|
public Set<UserRoleDto> getRoles() {
|
|
return roles;
|
|
}
|
|
|
|
public void setRoles(Collection<UserRoleDto> roles) {
|
|
this.roles.clear();
|
|
this.roles.addAll(roles);
|
|
}
|
|
|
|
public String getOldPassword() {
|
|
return oldPassword;
|
|
}
|
|
|
|
public String getPassword() {
|
|
return password;
|
|
}
|
|
|
|
public String getPasswordConfirm() {
|
|
return passwordConfirm;
|
|
}
|
|
|
|
public Date getBirthDate() {
|
|
return birthDate;
|
|
}
|
|
|
|
public void setBirthDate(Date birthDate) {
|
|
this.birthDate = birthDate;
|
|
}
|
|
|
|
public User.UserDegree getDegree() {
|
|
return degree;
|
|
}
|
|
|
|
public void setDegree(User.UserDegree degree) {
|
|
this.degree = degree;
|
|
}
|
|
|
|
@JsonIgnore
|
|
public boolean isPasswordsValid() {
|
|
if (StringUtils.isEmpty(password) || StringUtils.isEmpty(passwordConfirm)) {
|
|
return true;
|
|
}
|
|
return !Objects.equals(password, passwordConfirm);
|
|
}
|
|
|
|
@JsonIgnore
|
|
public boolean isOldPasswordValid() {
|
|
return !StringUtils.isEmpty(oldPassword);
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return getClass().getSimpleName() + " {" +
|
|
"id=" + id +
|
|
", login='" + login + '\'' +
|
|
", firstName='" + firstName + '\'' +
|
|
", lastName='" + lastName + '\'' +
|
|
", email='" + email + '\'' +
|
|
", activated=" + activated +
|
|
", roles=" + roles +
|
|
", password='" + password + '\'' +
|
|
", passwordConfirm='" + passwordConfirm + '\'' +
|
|
", birthDate='" + birthDate + '\'' +
|
|
", degree='" + degree + '\'' +
|
|
'}';
|
|
}
|
|
}
|