From 3747bce99d6686a8143f66c8c662009da6c820fa Mon Sep 17 00:00:00 2001 From: Mitja Zivkovic Date: Wed, 23 Jan 2019 20:15:35 +0100 Subject: [PATCH] upd(internal): add ListGrants --- internal/rules/interfaces.go | 1 + internal/rules/resources.go | 10 ++++++++++ internal/rules/resources_test.go | 11 +++++++++++ 3 files changed, 22 insertions(+) diff --git a/internal/rules/interfaces.go b/internal/rules/interfaces.go index 6cd777edf..c89002d5c 100644 --- a/internal/rules/interfaces.go +++ b/internal/rules/interfaces.go @@ -15,4 +15,5 @@ type ResourcesInterface interface { CheckAccess(resource string, operation string) error Grant(resource string, teamID uint64, operations []string, value types.Access) error + ListGrants(resource string, teamID uint64) ([]types.Rules, error) } diff --git a/internal/rules/resources.go b/internal/rules/resources.go index a71bc17d0..646117cf3 100644 --- a/internal/rules/resources.go +++ b/internal/rules/resources.go @@ -122,3 +122,13 @@ func (r *resources) Grant(resource string, teamID uint64, operations []string, v } return err } + +func (r *resources) ListGrants(resource string, teamID uint64) ([]types.Rules, error) { + result := []types.Rules{} + + query := "select * from sys_rules where rel_team = ? and resource = ?" + if err := r.db.Select(&result, query, teamID, resource); err != nil { + return nil, err + } + return result, nil +} diff --git a/internal/rules/resources_test.go b/internal/rules/resources_test.go index 188012c5c..dc48fc608 100644 --- a/internal/rules/resources_test.go +++ b/internal/rules/resources_test.go @@ -47,6 +47,17 @@ func TestRules(t *testing.T) { NoError(t, resources.CheckAccessMulti("channel:*", "edit"), "channel:* edit, expected no error") } + // list grants for team + { + grants, err := resources.ListGrants("channel:2", 2) + NoError(t, err, "expect no error") + Assert(t, len(grants) == 2, "expected 2 grants") + Assert(t, grants[0].TeamID == 2, "expected TeamID == 2, got %v", grants[0].TeamID) + Assert(t, grants[0].Resource == "channel:2", "expected Resource == channel:2, got %s", grants[0].Resource) + Assert(t, grants[0].Operation == "delete", "expected Operation == delete, got %s", grants[0].Operation) + Assert(t, grants[0].Value == rules.Allow, "expected Value == Allow, got %s", grants[0].Value) + } + // deny channel:1 group:1 (explicit deny, multi=deny) { resources.Grant("channel:1", 1, []string{"edit"}, rules.Deny)