Repository navigation
Expand file tree
/
Copy pathscope.go
More file actions
64 lines (55 loc) · 1.96 KB
/
Copy pathscope.go
File metadata and controls
64 lines (55 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package warden
import (
"context"
"github.com/xraph/forge"
)
type tenantScope struct {
appID string
tenantID string
namespacePath string
// namespaces is AncestorNamespaces(namespacePath), computed once by
// Check after every scope override (context, CheckRequest, CallOption)
// has been applied, and reused by evaluateRBAC/evaluateReBAC/evaluateABAC
// instead of each recomputing it.
namespaces []string
}
// scopeFromContext extracts tenant scope from context. Explicit WithTenant
// values take priority over forge.Scope so that callers (e.g.
// ensureWardenScope) can override the tenant derived from forge scope when
// OrgID is empty (app-scoped sessions where roles are stored under appID).
func scopeFromContext(ctx context.Context) tenantScope {
// 1. Check for explicit WithTenant values first, since these take priority.
if tid := tenantIDFromContext(ctx); tid != "" {
return tenantScope{
appID: appIDFromContext(ctx),
tenantID: tid,
namespacePath: namespacePathFromContext(ctx),
}
}
// 2. Fall back to forge.Scope.
if s, ok := forge.ScopeFrom(ctx); ok {
return tenantScope{
appID: s.AppID(),
tenantID: s.OrgID(),
namespacePath: namespacePathFromContext(ctx),
}
}
// 3. Standalone mode with no tenant set.
return tenantScope{
appID: appIDFromContext(ctx),
tenantID: tenantIDFromContext(ctx),
namespacePath: namespacePathFromContext(ctx),
}
}
// ScopeFromContext extracts the tenant scope from the given context.
// Returns appID and tenantID using the standard resolution chain:
// explicit WithTenant > forge.Scope > empty.
func ScopeFromContext(ctx context.Context) (appID, tenantID string) {
s := scopeFromContext(ctx)
return s.appID, s.tenantID
}
// NamespaceFromContext extracts the namespace path from the given context.
// Returns the empty string (tenant root) if not set.
func NamespaceFromContext(ctx context.Context) string {
return scopeFromContext(ctx).namespacePath
}