upd(internal): types.Access and rules(Allow/Deny/Inherit)
This commit is contained in:
committed by
Tit Petric
parent
d9761cb7a1
commit
c0422a738a
@@ -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
|
||||
}
|
||||
|
||||
+17
-17
@@ -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)
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user