/
permission.go
51 lines (46 loc) · 1.73 KB
/
permission.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
package authz
import (
"context"
"github.com/goxiaoy/go-saas/common"
)
type PermissionManagementService interface {
AddGrant(ctx context.Context, resource Resource, action Action, subject Subject, tenantID string, effect Effect) error
//ListAcl list permission of subjects. if not subjects provided, all acl will be returned
ListAcl(ctx context.Context, subjects ...Subject) ([]PermissionBean, error)
UpdateGrant(ctx context.Context, subject Subject, acl []UpdateSubjectPermission) error
RemoveGrant(ctx context.Context, resource Resource, action Action, subject Subject, tenantID string, effects []Effect) error
}
func EnsureGrant(ctx context.Context, mgr PermissionManagementService, checker PermissionChecker, resource Resource, action Action, subject Subject, tenantID string) error {
eff, err := checker.IsGrantTenant(ctx, []*Requirement{NewRequirement(resource, action)}, tenantID, subject)
if err != nil {
return err
}
if eff[0] != EffectGrant {
err = mgr.AddGrant(ctx, resource, action, subject, tenantID, EffectGrant)
if err != nil {
return err
}
}
return nil
}
func EnsureForbidden(ctx context.Context, mgr PermissionManagementService, checker PermissionChecker, resource Resource, action Action, subject Subject, tenantID string) error {
eff, err := checker.IsGrantTenant(ctx, []*Requirement{NewRequirement(resource, action)}, tenantID, subject)
if err != nil {
return err
}
if eff[0] != EffectForbidden {
err = mgr.AddGrant(ctx, resource, action, subject, tenantID, EffectForbidden)
if err != nil {
return err
}
}
return nil
}
func NormalizeTenantId(ctx context.Context, tenantId string) string {
ti, _ := common.FromCurrentTenant(ctx)
if ti.GetId() == "" {
//host side
return tenantId
}
return ti.GetId()
}