Add permissions.TestService
Allows usage of permissons service w/o persistant backend
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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{},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user