提交 df5fab6e 作者: 方治民

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

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