Files
junhong_cmp_fiber/pkg/middleware/data_scope.go
huang f5000f2bfc
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m0s
修复超管无法回收资产的问题
2026-02-27 11:03:44 +08:00

124 lines
4.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 len(shopIDs) == 0 {
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 字段的表
// 平台用户/超管不添加条件SubordinateShopIDs 为 nil 或空数组)
// 代理用户WHERE owner_shop_id IN (subordinateShopIDs)
func ApplyOwnerShopFilter(ctx context.Context, query *gorm.DB) *gorm.DB {
shopIDs := GetSubordinateShopIDs(ctx)
if len(shopIDs) == 0 {
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 字段的表
// 平台用户/超管不添加条件SubordinateShopIDs 为 nil 或空数组)
// 代理用户WHERE seller_shop_id IN (subordinateShopIDs)
func ApplySellerShopFilter(ctx context.Context, query *gorm.DB) *gorm.DB {
shopIDs := GetSubordinateShopIDs(ctx)
if len(shopIDs) == 0 {
return query
}
return query.Where("seller_shop_id IN ?", shopIDs)
}
// ApplyShopTagFilter 应用店铺标签数据权限过滤
// 用于 CardWallet 等使用 shop_id_tag 字段的表
// 平台用户/超管不添加条件SubordinateShopIDs 为 nil 或空数组)
// 代理用户WHERE shop_id_tag IN (subordinateShopIDs)
func ApplyShopTagFilter(ctx context.Context, query *gorm.DB) *gorm.DB {
shopIDs := GetSubordinateShopIDs(ctx)
if len(shopIDs) == 0 {
return query
}
return query.Where("shop_id_tag IN ?", shopIDs)
}
// ApplyShopIDFilter 应用店铺主键数据权限过滤
// 用于 Shop 表,根据 id 字段过滤
// 平台用户/超管不添加条件SubordinateShopIDs 为 nil 或空数组)
// 代理用户WHERE id IN (subordinateShopIDs)
func ApplyShopIDFilter(ctx context.Context, query *gorm.DB) *gorm.DB {
shopIDs := GetSubordinateShopIDs(ctx)
if len(shopIDs) == 0 {
return query
}
return query.Where("id IN ?", shopIDs)
}
// ApplyAllocatorShopFilter 应用分配者店铺数据权限过滤
// 用于 ShopSeriesAllocation、ShopPackageAllocation 等使用 allocator_shop_id 字段的表
// 代理用户只能看到自己作为分配者的记录
// 平台用户/超管不添加条件allocator_shop_id=0 表示平台分配)
// 代理用户WHERE allocator_shop_id = 当前用户店铺ID
func ApplyAllocatorShopFilter(ctx context.Context, query *gorm.DB) *gorm.DB {
userType := GetUserTypeFromContext(ctx)
// 平台用户和超管不限制
if userType == constants.UserTypeSuperAdmin || userType == constants.UserTypePlatform {
return query
}
// 代理用户只能看到自己分配出去的记录
shopID := GetShopIDFromContext(ctx)
if shopID == 0 {
// 代理用户但无店铺ID返回空结果
return query.Where("1 = 0")
}
return query.Where("allocator_shop_id = ?", shopID)
}