package hooks import ( "context" "database/sql" "reflect" "strings" "time" "github.com/gogf/gf/v2/database/gdb" "easygoadmin/internal/consts" ) // ============================== 全局模型钩子 ============================== // // 全局钩子是架构红线的执行点: // - Select、Update、Delete 的条件过滤在 dao.Ctx 覆写中注入(见 ApplyCtx,进入 SQL 构建前最可靠) // - Insert:主键交给数据库自增,自动补齐审计字段(create_user/create_time/del_flag)与租户ID // - Update:自动为审计字段(update_user/update_time)盖章 // - Delete:物理删除一律改写为软删除 UPDATE del_flag=1 // // 所有表均含统一字段 id/create_user/create_time/update_user/update_time/del_flag。 // tenantTables 含 tenant_id 列的租户级表 // // 与初始化脚本保持一致,共 9 张 var tenantTables = map[string]bool{ "goframe_user": true, "goframe_role": true, "goframe_dept": true, "goframe_level": true, "goframe_position": true, "goframe_article": true, "goframe_category": true, "goframe_notice": true, "goframe_link": true, } // IsTenantTable 判断是否为租户级表 func IsTenantTable(table string) bool { return tenantTables[table] } // Handler 返回全局钩子 // 由 dao 扩展模板挂载 func Handler() gdb.HookHandler { return gdb.HookHandler{ Insert: insertHook, Update: updateHook, Delete: deleteHook, } } // ApplyCtx 注入软删除过滤与租户隔离条件 // // 由 dao 扩展模板的 Ctx 覆写方法调用,在模型层(SQL 构建之前)注入: // // 所有表:del_flag = 0 // 租户级表:平台级(超管)不过滤,全量可见;租户用户追加 tenant_id IN (0, 自身租户) func ApplyCtx(ctx context.Context, m *gdb.Model, table string) *gdb.Model { m = m.Where("del_flag", 0) if tenantTables[table] && !consts.IsPlatform(ctx) { m = m.WhereIn("tenant_id", []int{0, consts.CtxTenantID(ctx)}) } return m } // auditUser 获取审计操作人 // // 优先取用户名,保证租户内稳定可追溯 func auditUser(ctx context.Context) string { if u := consts.GetUsername(ctx); u != "" { return u } return consts.GetRealname(ctx) } // isBlank 判断插入值是否为空 // // 为空时由钩子补齐 func isBlank(v any) bool { if v == nil { return true } if s, ok := v.(string); ok { return s == "" } rv := reflect.ValueOf(v) switch rv.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64: return rv.IsZero() } return false } // hookTable 归一化钩子回调中的表名 // // gdb 传入的表名带反引号,如 `goframe_user` func hookTable(table string) string { return strings.Trim(table, "`") } // insertHook 插入钩子 // // 自动补齐主键、审计字段与租户ID func insertHook(ctx context.Context, in *gdb.HookInsertInput) (sql.Result, error) { now := time.Now() table := hookTable(in.Table) for _, item := range in.Data { // 主键交由数据库自增(id 统一 int,不使用雪花算法) if isBlank(item["del_flag"]) { item["del_flag"] = 0 } if isBlank(item["create_time"]) { item["create_time"] = now } if isBlank(item["create_user"]) { item["create_user"] = auditUser(ctx) } // 租户级表: // - 租户用户未指定租户ID时自动携带自身(零值视为未指定,防跨租户逃逸) // - 超管未指定租户ID时代入默认租户(tenant.defaultId); // 显式传 0 可创建平台共享行,显式传具体租户从显式值 if tenantTables[table] { v, ok := item["tenant_id"] if !consts.IsPlatform(ctx) { if !ok || isBlank(v) { item["tenant_id"] = consts.CtxTenantID(ctx) } } else if !ok || v == nil { item["tenant_id"] = consts.DefaultTenantID(ctx) } } } return in.Next(ctx) } // updateHook 更新钩子 // // 自动为审计字段盖章 func updateHook(ctx context.Context, in *gdb.HookUpdateInput) (sql.Result, error) { if data, ok := in.Data.(map[string]any); ok { data["update_time"] = time.Now() data["update_user"] = auditUser(ctx) } return in.Next(ctx) } // deleteHook 删除钩子 // // 将物理删除改写为软删除,为架构红线; // 注意:gdb 的 Model.Update 不接收 ctx,模型已携带上下文设置, // 传入 ctx 会被误当作数据导致匹配字段失败 func deleteHook(ctx context.Context, in *gdb.HookDeleteInput) (sql.Result, error) { return in.Model.Update(map[string]any{"del_flag": 1}) }