feat(tenant): 按租户隔离用户身份与后台权限
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user