fix(tenant): 收紧双站运营提号与录单边界
This commit is contained in:
@@ -7,11 +7,13 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"hfb_sys/backend/internal/platformscope"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"hfb_sys/backend/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// RequirePermission 检查当前管理员是否拥有指定权限。
|
||||
@@ -46,6 +48,46 @@ func RequirePermissionIf(permCode string, rdb *redis.Client, predicate func(*gin
|
||||
}
|
||||
}
|
||||
|
||||
// RequirePlatformSuperAdmin 仅放行平台超级管理员,用于不属于单一经营站点的全局财务数据。
|
||||
func RequirePlatformSuperAdmin(db *gorm.DB) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
value, ok := c.Get(ContextAdminID)
|
||||
if !ok {
|
||||
response.Unauthorized(c, "缺少管理员上下文")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
adminID, ok := value.(uint64)
|
||||
if !ok || adminID == 0 {
|
||||
response.Unauthorized(c, "管理员上下文无效")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if db == nil {
|
||||
response.ServiceUnavailable(c, "权限校验服务暂时不可用")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
var count int64
|
||||
err := db.WithContext(c.Request.Context()).
|
||||
Table("admin_user_roles AS aur").
|
||||
Joins("JOIN roles AS r ON r.id = aur.role_id").
|
||||
Where("aur.admin_user_id = ? AND r.code = ?", adminID, "super_admin").
|
||||
Count(&count).Error
|
||||
if err != nil {
|
||||
response.ServiceUnavailable(c, "权限校验服务暂时不可用")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if count == 0 {
|
||||
response.Error(c, http.StatusForbidden, "platform_super_admin_required", "仅平台超级管理员可访问")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func checkAnyPermission(c *gin.Context, rdb *redis.Client, permCodes ...string) bool {
|
||||
value, ok := c.Get(ContextAdminID)
|
||||
if !ok {
|
||||
@@ -108,6 +150,9 @@ func getPermCodes(c *gin.Context, rdb *redis.Client, adminID uint64) ([]string,
|
||||
|
||||
func adminPermissionCacheKey(ctx context.Context, adminID uint64) string {
|
||||
key := fmt.Sprintf("admin:perms:%d", adminID)
|
||||
if platformscope.IsScope(ctx) {
|
||||
return key + ":platform"
|
||||
}
|
||||
if scope := tenancy.ScopeKey(ctx); scope != "" {
|
||||
key += ":" + scope
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user