3
0

Add permissions.TestService

Allows usage of permissons service w/o persistant backend
This commit is contained in:
Denis Arh
2019-09-09 03:10:17 +02:00
parent 18191b28aa
commit b2d678f758
2 changed files with 36 additions and 2 deletions
+14 -2
View File
@@ -106,15 +106,27 @@ func (svc *service) Grant(ctx context.Context, wl Whitelist, rules ...*Rule) (er
svc.l.Lock()
defer svc.l.Unlock()
if err = svc.checkRules(wl, rules...); err != nil {
return err
}
svc.grant(rules...)
return svc.flush(ctx)
}
func (svc service) checkRules(wl Whitelist, rules ...*Rule) error {
for _, r := range rules {
if !wl.Check(r) {
return errors.Errorf("invalid rule: '%s' on '%s'", r.Operation, r.Resource)
}
}
svc.rules = svc.rules.merge(rules...)
return nil
}
return svc.flush(ctx)
func (svc *service) grant(rules ...*Rule) {
svc.rules = svc.rules.merge(rules...)
}
// Watches for changes
+22
View File
@@ -7,6 +7,9 @@ import (
type (
ServiceAllowAll struct{}
ServiceDenyAll struct{}
TestService struct {
service
}
)
func (ServiceAllowAll) Can(ctx context.Context, res Resource, op Operation, ff ...CheckAccessFunc) bool {
@@ -32,3 +35,22 @@ func (ServiceDenyAll) Grant(ctx context.Context, wl Whitelist, rules ...*Rule) (
func (ServiceDenyAll) FindRulesByRoleID(roleID uint64) (rr RuleSet) {
return
}
func (svc *TestService) Grant(ctx context.Context, wl Whitelist, rules ...*Rule) (err error) {
if err = svc.checkRules(wl, rules...); err != nil {
return err
}
svc.grant(rules...)
return nil
}
func (svc *TestService) ClearGrants() {
svc.rules = RuleSet{}
}
func NewTestService() *TestService {
return &TestService{
service: service{},
}
}