mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-11 19:04:35 +00:00
Add tenancy to seen and otp services
This commit is contained in:
38
pkg/cache/cache.go
vendored
38
pkg/cache/cache.go
vendored
@@ -2,20 +2,26 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/micro/micro/v3/service/store"
|
||||
"github.com/micro/services/pkg/tenant"
|
||||
)
|
||||
|
||||
type Cache interface {
|
||||
// Context returns a tenant scoped Cache
|
||||
Context(ctx context.Context) Cache
|
||||
Get(key string, val interface{}) error
|
||||
Put(key string, val interface{}, expires time.Time) error
|
||||
Delete(key string) error
|
||||
}
|
||||
|
||||
type cache struct {
|
||||
Store store.Store
|
||||
Store store.Store
|
||||
Prefix string
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -23,7 +29,25 @@ var (
|
||||
)
|
||||
|
||||
func New(st store.Store) Cache {
|
||||
return &cache{st}
|
||||
return &cache{Store: st}
|
||||
}
|
||||
|
||||
func (c *cache) Key(k string) string {
|
||||
if len(c.Prefix) > 0 {
|
||||
return fmt.Sprintf("%s/%s", c.Prefix, k)
|
||||
}
|
||||
return k
|
||||
}
|
||||
|
||||
func (c *cache) Context(ctx context.Context) Cache {
|
||||
t, ok := tenant.FromContext(ctx)
|
||||
if !ok {
|
||||
return c
|
||||
}
|
||||
return &cache{
|
||||
Store: c.Store,
|
||||
Prefix: t,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *cache) Get(key string, val interface{}) error {
|
||||
@@ -31,7 +55,7 @@ func (c *cache) Get(key string, val interface{}) error {
|
||||
c.Store = store.DefaultStore
|
||||
}
|
||||
|
||||
recs, err := c.Store.Read(key, store.ReadLimit(1))
|
||||
recs, err := c.Store.Read(c.Key(key), store.ReadLimit(1))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -57,7 +81,7 @@ func (c *cache) Put(key string, val interface{}, expires time.Time) error {
|
||||
expiry = time.Duration(0)
|
||||
}
|
||||
return c.Store.Write(&store.Record{
|
||||
Key: key,
|
||||
Key: c.Key(key),
|
||||
Value: b,
|
||||
Expiry: expiry,
|
||||
})
|
||||
@@ -67,7 +91,11 @@ func (c *cache) Delete(key string) error {
|
||||
if c.Store == nil {
|
||||
c.Store = store.DefaultStore
|
||||
}
|
||||
return c.Store.Delete(key)
|
||||
return c.Store.Delete(c.Key(key))
|
||||
}
|
||||
|
||||
func Context(ctx context.Context) Cache {
|
||||
return DefaultCache.Context(ctx)
|
||||
}
|
||||
|
||||
func Get(key string, val interface{}) error {
|
||||
|
||||
27
pkg/tenant/tenant.go
Normal file
27
pkg/tenant/tenant.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// Package tenant provides multi-tenancy helpers
|
||||
package tenant
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/micro/micro/v3/service/auth"
|
||||
)
|
||||
|
||||
// FromContext returns a tenant from the context
|
||||
func FromContext(ctx context.Context) (string, bool) {
|
||||
acc, ok := auth.AccountFromContext(ctx)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
return FromAccount(acc), true
|
||||
}
|
||||
|
||||
// FromAccount returns a tenant from
|
||||
func FromAccount(acc *auth.Account) string {
|
||||
owner := acc.Metadata["apikey_owner"]
|
||||
if len(owner) == 0 {
|
||||
owner = acc.ID
|
||||
}
|
||||
return fmt.Sprintf("%s/%s", acc.Issuer, owner)
|
||||
}
|
||||
Reference in New Issue
Block a user