feat(platform-ops): 增加平台只读统计接口与超管入口
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user