feat(platform-ops): 增加平台只读统计接口与超管入口

This commit is contained in:
yml
2026-11-15 21:31:12 +08:00
parent d494746839
commit 5ba893cfde
19 changed files with 752 additions and 3 deletions
@@ -13,6 +13,7 @@ import (
"hfb_sys/backend/internal/logging"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/modules/auth"
"hfb_sys/backend/internal/platformscope"
"hfb_sys/backend/internal/tenancy"
"github.com/redis/go-redis/v9"
@@ -76,6 +77,9 @@ func (r *Repository) Login(ctx context.Context, username string, password string
return LoginResult{}, ErrInvalidCredential
}
allowed, err := r.adminHasTenantAccess(ctx, admin.ID)
if platformscope.IsScope(ctx) {
allowed, err = r.adminHasPlatformAccess(ctx, admin.ID)
}
if err != nil {
return LoginResult{}, err
}
@@ -120,6 +124,9 @@ func (r *Repository) FindActiveForToken(ctx context.Context, id uint64, tokenVer
return nil, newTokenVersionMismatchError(tokenVersion, admin.TokenVersion)
}
allowed, err := r.adminHasTenantAccess(ctx, admin.ID)
if platformscope.IsScope(ctx) {
allowed, err = r.adminHasPlatformAccess(ctx, admin.ID)
}
if err != nil {
return nil, newTokenDependencyError(err)
}
@@ -226,7 +233,9 @@ func (r *Repository) loadRolesAndPerms(ctx context.Context, dto *AdminDTO) {
roleQuery := r.db.WithContext(ctx).Table("roles").
Joins("JOIN admin_user_roles aur ON aur.role_id = roles.id").
Where("aur.admin_user_id = ?", dto.ID)
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
if platformscope.IsScope(ctx) {
roleQuery = roleQuery.Where("roles.code = ?", "super_admin")
} else if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
roleQuery = roleQuery.Where("(aur.tenant_id = ? OR roles.code = ?)", tenantID, "super_admin")
}
roleQuery.Find(&roles)
@@ -331,12 +340,24 @@ func scopedAuthKey(tenantID uint64, base string) 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
}
return key
}
func (r *Repository) adminHasPlatformAccess(ctx context.Context, adminID uint64) (bool, error) {
var count int64
err := r.db.WithContext(ctx).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
return count > 0, err
}
func (r *Repository) adminHasTenantAccess(ctx context.Context, adminID uint64) (bool, error) {
tenantID := tenancy.IDFromContext(ctx)
if tenantID == 0 {
@@ -7,6 +7,7 @@ import (
"hfb_sys/backend/internal/database"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/platformscope"
"hfb_sys/backend/internal/tenancy"
)
@@ -14,6 +15,50 @@ func adminTenantContext(tenantID uint64) context.Context {
return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID})
}
func TestPlatformOpsOnlyAllowsSuperAdmin(t *testing.T) {
db := database.NewTestDB()
roles := []model.Role{
{Code: "super_admin", Name: "超级管理员"},
{Code: "cs", Name: "客服"},
}
if err := db.Create(&roles).Error; err != nil {
t.Fatal(err)
}
tenantAdmin := model.AdminUser{Username: "tenant-cs-platform", PasswordHash: "x", Status: "active", TokenVersion: 1}
superAdmin := model.AdminUser{Username: "root-platform", PasswordHash: "x", Status: "active", TokenVersion: 1}
if err := db.Create(&tenantAdmin).Error; err != nil {
t.Fatal(err)
}
if err := db.Create(&superAdmin).Error; err != nil {
t.Fatal(err)
}
for _, assignment := range []model.AdminUserRole{
{AdminID: tenantAdmin.ID, RoleID: roles[1].ID, TenantID: 101},
{AdminID: superAdmin.ID, RoleID: roles[0].ID, TenantID: 0},
} {
if err := db.Create(&assignment).Error; err != nil {
t.Fatal(err)
}
}
repo := NewRepository(db, nil, nil)
ctx := platformscope.WithScope(context.Background())
if _, err := repo.FindActiveForToken(ctx, tenantAdmin.ID, 1); !errors.Is(err, ErrAdminNotFound) {
t.Fatalf("tenant admin platform token error = %v, want ErrAdminNotFound", err)
}
if _, err := repo.FindActiveForToken(ctx, superAdmin.ID, 1); err != nil {
t.Fatalf("super admin platform token: %v", err)
}
dto, err := repo.FindByID(ctx, superAdmin.ID)
if err != nil {
t.Fatal(err)
}
if len(dto.Roles) != 1 || dto.Roles[0].Code != "super_admin" ||
len(dto.Permissions) != 1 || dto.Permissions[0] != "*" {
t.Fatalf("platform super admin = %+v", dto)
}
}
func TestAdminAccessAndPermissionsAreScopedByTenant(t *testing.T) {
db := database.NewTestDB()
roles := []model.Role{