refactor: 数据权限过滤从 GORM Callback 改为 Store 层显式调用
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m2s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m2s
- 移除 RegisterDataPermissionCallback 和 SkipDataPermission 机制 - 在 Auth 中间件预计算 SubordinateShopIDs 并注入 Context - 新增 ApplyShopFilter/ApplyEnterpriseFilter/ApplyOwnerShopFilter 等 Helper 函数 - 所有 Store 层查询方法显式调用数据权限过滤函数 - 权限检查函数 CanManageShop/CanManageEnterprise 改为从 Context 获取数据 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
91
pkg/middleware/data_scope.go
Normal file
91
pkg/middleware/data_scope.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GetSubordinateShopIDs 获取当前用户可管理的店铺ID列表
|
||||
// 返回 nil 表示不受数据权限限制(平台用户/超管)
|
||||
// 返回 []uint 表示限制在这些店铺范围内(代理用户)
|
||||
func GetSubordinateShopIDs(ctx context.Context) []uint {
|
||||
if ctx == nil {
|
||||
return nil
|
||||
}
|
||||
if ids, ok := ctx.Value(constants.ContextKeySubordinateShopIDs).([]uint); ok {
|
||||
return ids
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ApplyShopFilter 应用店铺数据权限过滤
|
||||
// 平台用户/超管:不添加条件(SubordinateShopIDs 为 nil)
|
||||
// 代理用户:WHERE shop_id IN (subordinateShopIDs)
|
||||
// 注意:NULL shop_id 的记录对代理用户不可见
|
||||
func ApplyShopFilter(ctx context.Context, query *gorm.DB) *gorm.DB {
|
||||
shopIDs := GetSubordinateShopIDs(ctx)
|
||||
if shopIDs == nil {
|
||||
return query
|
||||
}
|
||||
return query.Where("shop_id IN ?", shopIDs)
|
||||
}
|
||||
|
||||
// ApplyEnterpriseFilter 应用企业数据权限过滤
|
||||
// 非企业用户:不添加条件
|
||||
// 企业用户:WHERE enterprise_id = ?
|
||||
func ApplyEnterpriseFilter(ctx context.Context, query *gorm.DB) *gorm.DB {
|
||||
userType := GetUserTypeFromContext(ctx)
|
||||
if userType != constants.UserTypeEnterprise {
|
||||
return query
|
||||
}
|
||||
enterpriseID := GetEnterpriseIDFromContext(ctx)
|
||||
if enterpriseID == 0 {
|
||||
// 企业用户但无企业ID,返回空结果
|
||||
return query.Where("1 = 0")
|
||||
}
|
||||
return query.Where("enterprise_id = ?", enterpriseID)
|
||||
}
|
||||
|
||||
// ApplyOwnerShopFilter 应用归属店铺数据权限过滤
|
||||
// 用于 Enterprise 等使用 owner_shop_id 字段的表
|
||||
// 平台用户/超管:不添加条件
|
||||
// 代理用户:WHERE owner_shop_id IN (subordinateShopIDs)
|
||||
func ApplyOwnerShopFilter(ctx context.Context, query *gorm.DB) *gorm.DB {
|
||||
shopIDs := GetSubordinateShopIDs(ctx)
|
||||
if shopIDs == nil {
|
||||
return query
|
||||
}
|
||||
return query.Where("owner_shop_id IN ?", shopIDs)
|
||||
}
|
||||
|
||||
// IsUnrestricted 检查当前用户是否不受数据权限限制
|
||||
// 平台用户/超管返回 true,代理/企业用户返回 false
|
||||
func IsUnrestricted(ctx context.Context) bool {
|
||||
return GetSubordinateShopIDs(ctx) == nil
|
||||
}
|
||||
|
||||
// ApplySellerShopFilter 应用销售店铺数据权限过滤
|
||||
// 用于 Order 等使用 seller_shop_id 字段的表
|
||||
// 平台用户/超管:不添加条件
|
||||
// 代理用户:WHERE seller_shop_id IN (subordinateShopIDs)
|
||||
func ApplySellerShopFilter(ctx context.Context, query *gorm.DB) *gorm.DB {
|
||||
shopIDs := GetSubordinateShopIDs(ctx)
|
||||
if shopIDs == nil {
|
||||
return query
|
||||
}
|
||||
return query.Where("seller_shop_id IN ?", shopIDs)
|
||||
}
|
||||
|
||||
// ApplyShopTagFilter 应用店铺标签数据权限过滤
|
||||
// 用于 CardWallet 等使用 shop_id_tag 字段的表
|
||||
// 平台用户/超管:不添加条件
|
||||
// 代理用户:WHERE shop_id_tag IN (subordinateShopIDs)
|
||||
func ApplyShopTagFilter(ctx context.Context, query *gorm.DB) *gorm.DB {
|
||||
shopIDs := GetSubordinateShopIDs(ctx)
|
||||
if shopIDs == nil {
|
||||
return query
|
||||
}
|
||||
return query.Where("shop_id_tag IN ?", shopIDs)
|
||||
}
|
||||
Reference in New Issue
Block a user