diff --git a/internal/rules/interfaces.go b/internal/rules/interfaces.go index 0b7a6a8dd..6cd777edf 100644 --- a/internal/rules/interfaces.go +++ b/internal/rules/interfaces.go @@ -4,6 +4,8 @@ import ( "context" "github.com/titpetric/factory" + + "github.com/crusttech/crust/system/types" ) type ResourcesInterface interface { @@ -12,5 +14,5 @@ type ResourcesInterface interface { CheckAccessMulti(resource string, operation string) error CheckAccess(resource string, operation string) error - Grant(resource string, teamID uint64, operations []string, value Access) error + Grant(resource string, teamID uint64, operations []string, value types.Access) error } diff --git a/internal/rules/resources.go b/internal/rules/resources.go index 14881774f..a71bc17d0 100644 --- a/internal/rules/resources.go +++ b/internal/rules/resources.go @@ -11,19 +11,19 @@ import ( "github.com/crusttech/crust/system/types" ) -type Access int - -const ( - Allow Access = 1 - Deny = 0 - Inherit = -1 -) - type resources struct { ctx context.Context db *factory.DB } +type Access types.Access + +var ( + Allow = types.Allow + Deny = types.Deny + Inherit = types.Inherit +) + func NewResources(ctx context.Context, db *factory.DB) ResourcesInterface { return (&resources{}).With(ctx, db) } @@ -41,7 +41,7 @@ func (r *resources) identity() uint64 { func (r *resources) CheckAccessMulti(resource string, operation string) error { user := r.identity() - result := []Access{} + result := []types.Access{} query := []string{ // select rules "select r.value from sys_rules r", @@ -58,12 +58,12 @@ func (r *resources) CheckAccessMulti(resource string, operation string) error { // order by deny, allow for _, val := range result { - if val == Deny { + if val == types.Deny { return errors.New("Access not allowed") } } for _, val := range result { - if val == Allow { + if val == types.Allow { return nil } } @@ -72,7 +72,7 @@ func (r *resources) CheckAccessMulti(resource string, operation string) error { func (r *resources) CheckAccess(resource string, operation string) error { user := r.identity() - result := []Access{} + result := []types.Access{} query := []string{ // select rules "select r.value from sys_rules r", @@ -88,30 +88,30 @@ func (r *resources) CheckAccess(resource string, operation string) error { // order by deny, allow for _, val := range result { - if val == Deny { + if val == types.Deny { return errors.New("Access not allowed") } } for _, val := range result { - if val == Allow { + if val == types.Allow { return nil } } return errors.New("Access not allowed") } -func (r *resources) Grant(resource string, teamID uint64, operations []string, value Access) error { +func (r *resources) Grant(resource string, teamID uint64, operations []string, value types.Access) error { row := types.Rules{ TeamID: teamID, Resource: resource, - Value: int(value), + Value: value, } var err error for _, operation := range operations { row.Operation = operation switch value { - case Inherit: + case types.Inherit: _, err = r.db.NamedExec("delete from sys_rules where rel_team=:rel_team and resource=:resource and operation=:operation", row) default: err = r.db.Replace("sys_rules", row) diff --git a/system/types/rules.go b/system/types/rules.go index f97cfdd08..200dff5f5 100644 --- a/system/types/rules.go +++ b/system/types/rules.go @@ -1,8 +1,16 @@ package types +type Access int + +const ( + Allow Access = 1 + Deny Access = 0 + Inherit Access = -1 +) + type Rules struct { TeamID uint64 `db:"rel_team"` Resource string `db:"resource"` Operation string `db:"operation"` - Value int `db:"value"` + Value Access `db:"value"` }