提交 df5fab6e 作者: 方治民

feat: 针对 User/Role/Permission 添加 hibernate 逻辑删除的相关实现

上级 f393c586
/* (C) 2022 YiRing, Inc. */ /* (C) 2022 YiRing, Inc. */
package com.yiring.auth.domain.permission; package com.yiring.auth.domain.permission;
import static com.yiring.auth.domain.permission.Permission.DELETE_SQL;
import static com.yiring.auth.domain.permission.Permission.TABLE_NAME;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.vladmihalcea.hibernate.type.json.JsonType; import com.vladmihalcea.hibernate.type.json.JsonType;
import com.yiring.common.domain.BasicEntity; import com.yiring.common.domain.BasicEntity;
import java.io.Serial; import java.io.Serial;
import java.io.Serializable; import java.io.Serializable;
import javax.persistence.*; import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.Table;
import lombok.*; import lombok.*;
import lombok.experimental.FieldDefaults; import lombok.experimental.FieldDefaults;
import lombok.experimental.FieldNameConstants; import lombok.experimental.FieldNameConstants;
import lombok.experimental.SuperBuilder; import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.Comment; import org.hibernate.annotations.*;
import org.hibernate.annotations.TypeDef;
/** /**
* 权限 * 权限
...@@ -29,19 +34,31 @@ import org.hibernate.annotations.TypeDef; ...@@ -29,19 +34,31 @@ import org.hibernate.annotations.TypeDef;
@AllArgsConstructor @AllArgsConstructor
@FieldNameConstants @FieldNameConstants
@FieldDefaults(level = AccessLevel.PRIVATE) @FieldDefaults(level = AccessLevel.PRIVATE)
@SQLDelete(sql = DELETE_SQL)
@SQLDeleteAll(sql = DELETE_SQL)
@Where(clause = BasicEntity.Where.EXIST)
@Entity @Entity
@TypeDef(name = "json", typeClass = JsonType.class) @TypeDef(name = "json", typeClass = JsonType.class)
@Table( @Table(
name = "SYS_PERMISSION", name = TABLE_NAME,
indexes = { @Index(columnList = "type"), @Index(columnList = "pid"), @Index(columnList = "tree") } indexes = {
@Index(columnList = "deleteTime"),
@Index(columnList = "type"),
@Index(columnList = "pid"),
@Index(columnList = "tree"),
}
) )
@Comment("系统权限") @Comment("系统权限")
public class Permission extends BasicEntity implements Serializable { public class Permission extends BasicEntity implements Serializable {
public static final String TABLE_NAME = "SYS_PERMISSION";
public static final String DELETE_SQL = "update " + TABLE_NAME + BasicEntity.Where.DELETE_SET;
@Serial @Serial
private static final long serialVersionUID = -2001221843529000953L; private static final long serialVersionUID = -2001221843529000953L;
@Comment("类型(MENU: 菜单, BUTTON: 按钮)") @Comment("类型(MENU: 菜单, BUTTON: 按钮)")
@Column(nullable = false)
@Enumerated(EnumType.STRING) @Enumerated(EnumType.STRING)
Type type; Type type;
...@@ -49,10 +66,11 @@ public class Permission extends BasicEntity implements Serializable { ...@@ -49,10 +66,11 @@ public class Permission extends BasicEntity implements Serializable {
Integer serial; Integer serial;
@Comment("标识") @Comment("标识")
@Column(unique = true, nullable = false) @Column(nullable = false)
String uid; String uid;
@Comment("名称") @Comment("名称")
@Column(nullable = false)
String name; String name;
@Comment("路径") @Comment("路径")
......
/* (C) 2022 YiRing, Inc. */ /* (C) 2022 YiRing, Inc. */
package com.yiring.auth.domain.role; package com.yiring.auth.domain.role;
import static com.yiring.auth.domain.role.Role.DELETE_SQL;
import static com.yiring.auth.domain.role.Role.TABLE_NAME;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.yiring.auth.domain.permission.Permission; import com.yiring.auth.domain.permission.Permission;
import com.yiring.auth.domain.user.User; import com.yiring.auth.domain.user.User;
...@@ -15,6 +18,9 @@ import lombok.experimental.FieldDefaults; ...@@ -15,6 +18,9 @@ import lombok.experimental.FieldDefaults;
import lombok.experimental.FieldNameConstants; import lombok.experimental.FieldNameConstants;
import lombok.experimental.SuperBuilder; import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.Comment; import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLDeleteAll;
import org.hibernate.annotations.Where;
/** /**
* 角色 * 角色
...@@ -31,11 +37,20 @@ import org.hibernate.annotations.Comment; ...@@ -31,11 +37,20 @@ import org.hibernate.annotations.Comment;
@AllArgsConstructor @AllArgsConstructor
@FieldNameConstants @FieldNameConstants
@FieldDefaults(level = AccessLevel.PRIVATE) @FieldDefaults(level = AccessLevel.PRIVATE)
@SQLDelete(sql = DELETE_SQL)
@SQLDeleteAll(sql = DELETE_SQL)
@Where(clause = BasicEntity.Where.EXIST)
@Entity @Entity
@Table(
name = TABLE_NAME,
indexes = { @Index(columnList = "deleteTime"), @Index(columnList = "uid,deleteTime", unique = true) }
)
@Comment("系统角色") @Comment("系统角色")
@Table(name = "SYS_ROLE", indexes = { @Index(columnList = "uid", unique = true) })
public class Role extends BasicEntity implements Serializable { public class Role extends BasicEntity implements Serializable {
public static final String TABLE_NAME = "SYS_ROLE";
public static final String DELETE_SQL = "update " + TABLE_NAME + BasicEntity.Where.DELETE_SET;
@Serial @Serial
private static final long serialVersionUID = 910404402503275957L; private static final long serialVersionUID = 910404402503275957L;
...@@ -44,6 +59,7 @@ public class Role extends BasicEntity implements Serializable { ...@@ -44,6 +59,7 @@ public class Role extends BasicEntity implements Serializable {
String uid; String uid;
@Comment("名称") @Comment("名称")
@Column(nullable = false)
String name; String name;
@JsonIgnore @JsonIgnore
......
...@@ -15,6 +15,8 @@ import lombok.experimental.FieldDefaults; ...@@ -15,6 +15,8 @@ import lombok.experimental.FieldDefaults;
import lombok.experimental.FieldNameConstants; import lombok.experimental.FieldNameConstants;
import lombok.experimental.SuperBuilder; import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.Comment; import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLDeleteAll;
import org.hibernate.annotations.Where; import org.hibernate.annotations.Where;
/** /**
...@@ -32,12 +34,26 @@ import org.hibernate.annotations.Where; ...@@ -32,12 +34,26 @@ import org.hibernate.annotations.Where;
@AllArgsConstructor @AllArgsConstructor
@FieldNameConstants @FieldNameConstants
@FieldDefaults(level = AccessLevel.PRIVATE) @FieldDefaults(level = AccessLevel.PRIVATE)
@Where(clause = "deleted = false") @SQLDelete(sql = User.DELETE_SQL)
@SQLDeleteAll(sql = User.DELETE_SQL)
@Where(clause = User.Where.EXIST)
@Entity @Entity
@Table(name = "SYS_USER", indexes = { @Index(columnList = "enabled"), @Index(columnList = "deleted") }) @Table(
name = User.TABLE_NAME,
indexes = {
@Index(columnList = "enabled"),
@Index(columnList = "deleteTime"),
@Index(columnList = "username,deleteTime", unique = true),
@Index(columnList = "mobile,deleteTime", unique = true),
@Index(columnList = "email,deleteTime", unique = true),
}
)
@Comment("系统用户") @Comment("系统用户")
public class User extends BasicEntity implements Serializable { public class User extends BasicEntity implements Serializable {
public static final String TABLE_NAME = "SYS_USER";
public static final String DELETE_SQL = "update " + TABLE_NAME + BasicEntity.Where.DELETE_SET;
@Serial @Serial
private static final long serialVersionUID = -5787847701210907511L; private static final long serialVersionUID = -5787847701210907511L;
...@@ -45,15 +61,12 @@ public class User extends BasicEntity implements Serializable { ...@@ -45,15 +61,12 @@ public class User extends BasicEntity implements Serializable {
String realName; String realName;
@Comment("用户名") @Comment("用户名")
@Column(unique = true)
String username; String username;
@Comment("手机号") @Comment("手机号")
@Column(unique = true)
String mobile; String mobile;
@Comment("邮箱") @Comment("邮箱")
@Column(unique = true)
String email; String email;
@Comment("密码") @Comment("密码")
...@@ -68,9 +81,6 @@ public class User extends BasicEntity implements Serializable { ...@@ -68,9 +81,6 @@ public class User extends BasicEntity implements Serializable {
@Comment("是否启用") @Comment("是否启用")
Boolean enabled; Boolean enabled;
@Comment("是否删除")
Boolean deleted;
@JsonIgnore @JsonIgnore
@Builder.Default @Builder.Default
@Comment("角色集合") @Comment("角色集合")
......
...@@ -80,7 +80,6 @@ public class AuthController { ...@@ -80,7 +80,6 @@ public class AuthController {
.username(param.getUsername()) .username(param.getUsername())
.password(SaSecureUtil.sha256(param.getPassword())) .password(SaSecureUtil.sha256(param.getPassword()))
.enabled(param.getEnable()) .enabled(param.getEnable())
.deleted(Boolean.FALSE)
.createTime(LocalDateTime.now()) .createTime(LocalDateTime.now())
.build(); .build();
userRepository.saveAndFlush(user); userRepository.saveAndFlush(user);
...@@ -105,7 +104,7 @@ public class AuthController { ...@@ -105,7 +104,7 @@ public class AuthController {
} }
// 检查用户是否已被删除 // 检查用户是否已被删除
if (!Boolean.FALSE.equals(user.getDeleted())) { if (user.getDeleteTime() != null) {
return Result.no(Status.FORBIDDEN, "用户被禁用, 请联系管理员"); return Result.no(Status.FORBIDDEN, "用户被禁用, 请联系管理员");
} }
......
...@@ -2,10 +2,7 @@ ...@@ -2,10 +2,7 @@
package com.yiring.common.domain; package com.yiring.common.domain;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import javax.persistence.Column; import javax.persistence.*;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import lombok.*; import lombok.*;
import lombok.experimental.FieldDefaults; import lombok.experimental.FieldDefaults;
import lombok.experimental.FieldNameConstants; import lombok.experimental.FieldNameConstants;
...@@ -50,4 +47,13 @@ public abstract class BasicEntity { ...@@ -50,4 +47,13 @@ public abstract class BasicEntity {
@Column(nullable = false) @Column(nullable = false)
@UpdateTimestamp @UpdateTimestamp
LocalDateTime updateTime; LocalDateTime updateTime;
@Comment("删除时间")
LocalDateTime deleteTime;
public interface Where {
String EXIST = " delete_time is null ";
String DELETE_SET = " set delete_time = now() where id = ? ";
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论