|
@@ -4,22 +4,32 @@ import lombok.AllArgsConstructor;
|
|
|
import lombok.Getter;
|
|
|
import lombok.NoArgsConstructor;
|
|
|
import lombok.Setter;
|
|
|
+import org.hibernate.annotations.CreationTimestamp;
|
|
|
+import org.hibernate.annotations.UpdateTimestamp;
|
|
|
+import org.springframework.security.core.GrantedAuthority;
|
|
|
+import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
|
+import org.springframework.security.core.userdetails.UserDetails;
|
|
|
|
|
|
import javax.persistence.*;
|
|
|
import javax.validation.constraints.NotNull;
|
|
|
-import java.io.Serializable;
|
|
|
+import java.util.*;
|
|
|
|
|
|
@Entity
|
|
|
-@Table(name = "user")
|
|
|
+@Table(name = "user",
|
|
|
+ uniqueConstraints = {@UniqueConstraint(name = "unique_username", columnNames = {"username"})},
|
|
|
+ indexes = {
|
|
|
+ @Index(name = "index_id", columnList = "id"),
|
|
|
+ @Index(name = "index_username", columnList = "username")
|
|
|
+ })
|
|
|
@NoArgsConstructor
|
|
|
@AllArgsConstructor
|
|
|
-public class User implements Serializable {
|
|
|
+public class User implements UserDetails {
|
|
|
|
|
|
@Getter
|
|
|
@Setter
|
|
|
@Id
|
|
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
|
|
- @Column(name = "id", nullable = false, columnDefinition = "INT COMMENT '用户实体ID'")
|
|
|
+ @Column(name = "id", nullable = false, columnDefinition = "INT COMMENT '用户实体'")
|
|
|
private Integer id;
|
|
|
|
|
|
@Getter
|
|
@@ -37,6 +47,31 @@ public class User implements Serializable {
|
|
|
@Column(name = "name", columnDefinition = "VARCHAR(64) COMMENT '姓名'")
|
|
|
private String name;
|
|
|
|
|
|
+ @Getter
|
|
|
+ @Setter
|
|
|
+ @Column(name = "nickname", columnDefinition = "VARCHAR(32) COMMENT '昵称'")
|
|
|
+ private String nickname;
|
|
|
+
|
|
|
+ @Getter
|
|
|
+ @Setter
|
|
|
+ @Column(name = "signature", columnDefinition = "VARCHAR(255) COMMENT '签名'")
|
|
|
+ private String signature;
|
|
|
+
|
|
|
+ @Getter
|
|
|
+ @Setter
|
|
|
+ @Column(name = "email", columnDefinition = "VARCHAR(255) COMMENT '电子邮箱'")
|
|
|
+ private String email;
|
|
|
+
|
|
|
+ @Getter
|
|
|
+ @Setter
|
|
|
+ @Column(name = "phone", columnDefinition = "VARCHAR(16) COMMENT '电话'")
|
|
|
+ private String phone;
|
|
|
+
|
|
|
+ @Getter
|
|
|
+ @Setter
|
|
|
+ @Column(name = "qq", columnDefinition = "VARCHAR(16) COMMENT 'QQ'")
|
|
|
+ private String qq;
|
|
|
+
|
|
|
@Getter
|
|
|
@Setter
|
|
|
@Column(name = "avatar", columnDefinition = "VARCHAR(255) COMMENT '头像'")
|
|
@@ -47,8 +82,92 @@ public class User implements Serializable {
|
|
|
@Column(name = "school", columnDefinition = "VARCHAR(64) COMMENT '学校'")
|
|
|
private String school;
|
|
|
|
|
|
- public User(@NotNull String username, @NotNull String password) {
|
|
|
+ @Getter
|
|
|
+ @Setter
|
|
|
+ @ManyToMany(cascade = {CascadeType.REFRESH}, fetch = FetchType.EAGER)
|
|
|
+ @JoinTable(name = "user_user_role",
|
|
|
+ joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
|
|
|
+ inverseJoinColumns = {@JoinColumn(name = "user_role_id", referencedColumnName = "id")}
|
|
|
+ )
|
|
|
+ private Set<UserRole> userRoleSet;
|
|
|
+
|
|
|
+ @Getter
|
|
|
+ @Setter
|
|
|
+ @Column(name = "is_account_non_expired", nullable = false, columnDefinition = "TINYINT DEFAULT '1' COMMENT '帐户未过期'")
|
|
|
+ private Boolean isAccountNonExpired;
|
|
|
+
|
|
|
+ @Getter
|
|
|
+ @Setter
|
|
|
+ @Column(name = "is_account_non_locked", nullable = false, columnDefinition = "TINYINT DEFAULT '1' COMMENT '帐户未锁定'")
|
|
|
+ private Boolean isAccountNonLocked;
|
|
|
+
|
|
|
+ @Getter
|
|
|
+ @Setter
|
|
|
+ @Column(name = "is_credentials_non_expired", nullable = false, columnDefinition = "TINYINT DEFAULT '1' COMMENT '凭据未过期'")
|
|
|
+ private Boolean isCredentialsNonExpired;
|
|
|
+
|
|
|
+ @Getter
|
|
|
+ @Setter
|
|
|
+ @Column(name = "is_enabled", nullable = false, columnDefinition = "TINYINT DEFAULT '1' COMMENT '帐户已启用'")
|
|
|
+ private Boolean isEnabled;
|
|
|
+
|
|
|
+ @Getter
|
|
|
+ @Setter
|
|
|
+ @Column(name = "created_at", columnDefinition = "DATETIME COMMENT '创建时间'")
|
|
|
+ @CreationTimestamp
|
|
|
+ private Date createdAt;
|
|
|
+
|
|
|
+ @Getter
|
|
|
+ @Setter
|
|
|
+ @Column(name = "updated_at", columnDefinition = "DATETIME COMMENT '更新时间'")
|
|
|
+ @UpdateTimestamp
|
|
|
+ private Date updatedAt;
|
|
|
+
|
|
|
+ @Version
|
|
|
+ @Column(name = "version", columnDefinition = "INTEGER COMMENT '版本号'")
|
|
|
+ public Integer version;
|
|
|
+
|
|
|
+ public User(@NotNull String name, @NotNull String username, @NotNull String password) {
|
|
|
+ this.name = name;
|
|
|
this.username = username;
|
|
|
this.password = password;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Collection<? extends GrantedAuthority> getAuthorities() {
|
|
|
+ List<GrantedAuthority> grantedAuthorityList = new ArrayList<>();
|
|
|
+ for (UserRole userRole : this.getUserRoleSet()) {
|
|
|
+ grantedAuthorityList.add(new SimpleGrantedAuthority("ROLE_" + userRole.getRole().name()));
|
|
|
+ }
|
|
|
+ return grantedAuthorityList;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean isAccountNonExpired() {
|
|
|
+ return this.isAccountNonExpired;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean isAccountNonLocked() {
|
|
|
+ return this.isAccountNonLocked;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean isCredentialsNonExpired() {
|
|
|
+ return this.isCredentialsNonExpired;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean isEnabled() {
|
|
|
+ return this.isEnabled;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean hasRole(UserRole.Role role) {
|
|
|
+ for (UserRole userRole : getUserRoleSet()) {
|
|
|
+ if (userRole.getRole().equals(role)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|