重构: ResScope 改成 ResFlags 位标志,行级过滤与字段脱敏拆成独立开关

ResScope(Off/On/All 三选一)改成 ResFlags 位标志:ResRow/ResRowRelations 管行级权限过滤,ResMask/ResMaskRelations 管字段级脱敏,两个维度可以按位独立组合(*Relations 那两位只保留定义,还没接入判断逻辑)。WithRolesScope 改名 WithPermission(req.ResFlags),dataProcessor 里原来单一的 skipPermission 拆成 skipRowFilter/skipFieldMask 分别控制。
This commit is contained in:
2026-07-22 14:03:33 +08:00
parent 6fc7854b0a
commit acb55bb53a
4 changed files with 39 additions and 23 deletions
+7 -6
View File
@@ -42,8 +42,9 @@ type dataProcessor struct {
ownsTx bool // tx 是否由本次写操作自动开启(而非调用方传入),决定 After 要不要 Commit/Rollback
onChange ResChangeRowFunc // 非 nil 表示本次写操作需要抓取快照/收集变更内容,由 Before 阶段的 DataInterceptor 决定
rows []changeRow
silent bool // ResOptions.Silent:跳过变更通知,见 req.WithSilent 注释
skipPermission bool // ResOptions.RolesScope ResScopeOn/ResScopeAll:跳过权限过滤/字段脱敏,见 req.WithRolesScope 注释
silent bool // ResOptions.Silent:跳过变更通知,见 req.WithSilent 注释
skipRowFilter bool // ResOptions.Permission ResRow:跳过行级权限过滤,见 req.WithPermission 注释
skipFieldMask bool // ResOptions.Permission 不含 ResMask:跳过字段级脱敏/写权限,见 req.WithPermission 注释
}
// UseTx 在 Before 之前调用:如果本次写操作会触发 onChange、且调用方没有显式传入事务,自动开一个
@@ -82,11 +83,11 @@ func (dp *dataProcessor) UseTx(dataset interface{}) (exec.QueryFactory, error) {
}
// applyIntercept 调用容器里注册的 DataInterceptor,返回角色权限过滤条件,并记录本次写完后要不要调用、调用谁。
// skipPermission 时丢弃行级权限过滤条件;silent 时不设置 onChange(写完后不会触发变更通知)。两者
// skipRowFilter 时丢弃行级权限过滤条件;silent 时不设置 onChange(写完后不会触发变更通知)。两者
// 相互独立,可以只生效一个。
func (dp *dataProcessor) applyIntercept(category ResEventType) (sub *db.SelectDataset, cond db.Expression) {
sub, cond, onChange := dp.res.intercept(dp.u, category)
if dp.skipPermission {
if dp.skipRowFilter {
sub, cond = nil, nil
}
if !dp.silent {
@@ -408,7 +409,7 @@ func (dp *dataProcessor) normalizeSaveValue(row db.Record) error {
}
field, hasField := dp.res.GetField(k)
if hasField && !dp.skipPermission && !hasFieldAccess(field, dp.u) {
if hasField && !dp.skipFieldMask && !hasFieldAccess(field, dp.u) {
delete(row, k)
continue
}
@@ -489,7 +490,7 @@ func (dp *dataProcessor) beforeSelectDataset(sd *db.SelectDataset) error {
// 真正解析 MaskField 标记的别名对应哪个资源、哪个字段(见 resolveLazyMask),解析失败说明
// 调用方标记的表名/字段名写错了,是编程错误,直接报错而不是静默跳过。
func (dp *dataProcessor) maskSelect(sd *db.SelectDataset) error {
if dp.res.IsVirtual() || dp.skipPermission {
if dp.res.IsVirtual() || dp.skipFieldMask {
return nil
}