fix(tenant): 收紧双站运营提号与录单边界

This commit is contained in:
yml
2026-11-16 16:12:55 +08:00
parent 1108df195d
commit 1ce9f7aaf5
60 changed files with 1531 additions and 337 deletions
+45
View File
@@ -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
}
@@ -9,6 +9,9 @@ import (
"testing"
"time"
"hfb_sys/backend/internal/database"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/platformscope"
"hfb_sys/backend/internal/tenancy"
"github.com/gin-gonic/gin"
@@ -20,11 +23,66 @@ func TestAdminPermissionCacheKeyIncludesTenant(t *testing.T) {
if got := adminPermissionCacheKey(ctx, 7); got != "admin:perms:7:tenant:101" {
t.Fatalf("tenant permission cache key = %q", got)
}
platformCtx := platformscope.WithScope(context.Background())
if got := adminPermissionCacheKey(platformCtx, 7); got != "admin:perms:7:platform" {
t.Fatalf("platform permission cache key = %q", got)
}
if got := adminPermissionCacheKey(context.Background(), 7); got != "admin:perms:7" {
t.Fatalf("legacy permission cache key = %q", got)
}
}
func TestRequirePlatformSuperAdmin(t *testing.T) {
db := database.NewTestDB()
roles := []model.Role{
{Code: "super_admin", Name: "超级管理员"},
{Code: "finance", Name: "财务"},
}
if err := db.Create(&roles).Error; err != nil {
t.Fatal(err)
}
admins := []model.AdminUser{
{Username: "platform-root", PasswordHash: "x", Status: "active"},
{Username: "tenant-finance", PasswordHash: "x", Status: "active"},
}
if err := db.Create(&admins).Error; err != nil {
t.Fatal(err)
}
if err := db.Create(&[]model.AdminUserRole{
{AdminID: admins[0].ID, RoleID: roles[0].ID, TenantID: 0},
{AdminID: admins[1].ID, RoleID: roles[1].ID, TenantID: 101},
}).Error; err != nil {
t.Fatal(err)
}
engine := gin.New()
engine.Use(func(c *gin.Context) {
if c.GetHeader("X-Test-Admin") == "1" {
c.Set(ContextAdminID, admins[0].ID)
} else {
c.Set(ContextAdminID, admins[1].ID)
}
c.Next()
})
engine.GET("/probe", RequirePlatformSuperAdmin(db), func(c *gin.Context) {
c.Status(http.StatusNoContent)
})
req := httptest.NewRequest(http.MethodGet, "/probe", nil)
req.Header.Set("X-Test-Admin", "1")
rec := httptest.NewRecorder()
engine.ServeHTTP(rec, req)
if rec.Code != http.StatusNoContent {
t.Fatalf("super admin status = %d, want %d", rec.Code, http.StatusNoContent)
}
rec = httptest.NewRecorder()
engine.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/probe", nil))
if rec.Code != http.StatusForbidden {
t.Fatalf("tenant admin status = %d, want %d", rec.Code, http.StatusForbidden)
}
}
func TestRequireAnyPermission(t *testing.T) {
gin.SetMode(gin.TestMode)
rdb := redis.NewClient(&redis.Options{