feat(tenant): 按租户隔离用户身份与后台权限

This commit is contained in:
yml
2026-11-15 15:37:23 +08:00
parent ca57f604bf
commit abf0fce89b
43 changed files with 1572 additions and 196 deletions
+18
View File
@@ -3,6 +3,7 @@ package tenancy
import (
"context"
"encoding/json"
"fmt"
"gorm.io/datatypes"
)
@@ -37,6 +38,23 @@ func MustFromContext(ctx context.Context) Scope {
return scope
}
func IDFromContext(ctx context.Context) uint64 {
scope, ok := FromContext(ctx)
if !ok {
return 0
}
return scope.TenantID
}
// ScopeKey returns a stable cache scope suffix. An empty string keeps legacy
// callers and tests on their existing unqualified keys.
func ScopeKey(ctx context.Context) string {
if tenantID := IDFromContext(ctx); tenantID > 0 {
return fmt.Sprintf("tenant:%d", tenantID)
}
return ""
}
func (s Scope) Profile() map[string]any {
return decodeJSONMap(s.ProfileJSON)
}