feat(gateway): 多租户增量2 —— gorm 租户插件按上下文自动隔离查询(SaaS P1)
统一强制、别靠人肉:受租户模型标记 isTenantScoped() 后,store/tenant_scope.go 的 gorm 回调按请求 ctx 自动给查询加 WHERE tenant_id、创建自动填 tenant_id。 - KB / Agent 加 TenantID 字段 + isTenantScoped() 标记 - middleware.TenantContext 把 tenant 注入 request context 供 store 插件读取 - ctx 无租户(系统/回填/未登录)不过滤,保留跨租户操作能力 - 启动 BackfillRowTenants 回填存量行 tenant_id=owner 默认租户(幂等) live 验证:创建自动写 tenant_id ✓;同 owner 不同 tenant 的行被查询过滤 ✓。 Doc/DocLink(异步入库)、Task/Eval(无 owner)留待增量2b。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -16,12 +16,14 @@ import (
|
||||
// 向量/全文/图谱实际以 "owner/name" 作分区键,保证只有 owner 能查到自己的库。
|
||||
type KB struct {
|
||||
BaseModel
|
||||
Owner string `gorm:"size:64;uniqueIndex:idx_kb_owner_name"`
|
||||
Name string `gorm:"size:64;uniqueIndex:idx_kb_owner_name"`
|
||||
Kind string `gorm:"size:16"` // folder / project / case / general
|
||||
TenantID string `gorm:"size:64;index"` // 多租户作用域(gorm tenant 插件按上下文自动填/过滤)
|
||||
Owner string `gorm:"size:64;uniqueIndex:idx_kb_owner_name"`
|
||||
Name string `gorm:"size:64;uniqueIndex:idx_kb_owner_name"`
|
||||
Kind string `gorm:"size:16"` // folder / project / case / general
|
||||
}
|
||||
|
||||
func (KB) TableName() string { return "sundynix_kb" }
|
||||
func (KB) TableName() string { return "sundynix_kb" }
|
||||
func (KB) isTenantScoped() {}
|
||||
|
||||
// ListKB 列出某 owner 的全部知识库(按创建时间)。
|
||||
func (p *Postgres) ListKB(ctx context.Context, owner string) ([]KB, error) {
|
||||
@@ -51,12 +53,14 @@ func (p *Postgres) EnsureKB(ctx context.Context, owner, name, kind string) error
|
||||
// 表名 sundynix_agent。(owner,name) 唯一 —— 同一用户下编排名不重复。
|
||||
type Agent struct {
|
||||
BaseModel
|
||||
Owner string `gorm:"size:64;uniqueIndex:idx_agent_on"`
|
||||
Name string `gorm:"size:128;uniqueIndex:idx_agent_on"`
|
||||
Graph string `gorm:"type:text"` // {nodes,edges} 的 JSON(含布局)
|
||||
TenantID string `gorm:"size:64;index"` // 多租户作用域(tenant 插件自动填/过滤)
|
||||
Owner string `gorm:"size:64;uniqueIndex:idx_agent_on"`
|
||||
Name string `gorm:"size:128;uniqueIndex:idx_agent_on"`
|
||||
Graph string `gorm:"type:text"` // {nodes,edges} 的 JSON(含布局)
|
||||
}
|
||||
|
||||
func (Agent) TableName() string { return "sundynix_agent" }
|
||||
func (Agent) isTenantScoped() {}
|
||||
|
||||
// ListAgents 返回某 owner 的全部编排(最近更新在前)。
|
||||
func (p *Postgres) ListAgents(ctx context.Context, owner string) ([]Agent, error) {
|
||||
|
||||
Reference in New Issue
Block a user