forked from go-saas/saas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tenant_resolve_contrib.go
57 lines (48 loc) · 1.25 KB
/
tenant_resolve_contrib.go
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
package saas
type TenantResolveContrib interface {
// Name of resolver
Name() string
// Resolve tenant
Resolve(ctx *Context) error
}
//TenantNormalizerContrib normalize tenant id or name into tenant id
type TenantNormalizerContrib struct {
ts TenantStore
}
var _ TenantResolveContrib = (*TenantNormalizerContrib)(nil)
func NewTenantNormalizerContrib(ts TenantStore) *TenantNormalizerContrib {
return &TenantNormalizerContrib{
ts: ts,
}
}
func (t *TenantNormalizerContrib) Name() string {
return "TenantNormalizer"
}
func (t *TenantNormalizerContrib) Resolve(ctx *Context) error {
if len(ctx.TenantIdOrName) > 0 {
tenant, err := t.ts.GetByNameOrId(ctx.Context(), ctx.TenantIdOrName)
if err != nil {
return err
}
ctx.TenantIdOrName = tenant.ID
//store for cache
ctx.WithContext(NewTenantConfigContext(ctx.Context(), tenant.ID, tenant))
}
return nil
}
// ContextContrib resolve from current context
type ContextContrib struct {
}
var _ TenantResolveContrib = (*ContextContrib)(nil)
func (c *ContextContrib) Name() string {
return "ContextContrib"
}
func (c *ContextContrib) Resolve(ctx *Context) error {
info, ok := FromCurrentTenant(ctx.Context())
if ok {
ctx.TenantIdOrName = info.GetId()
//terminate
ctx.HasHandled = true
}
return nil
}