8000 feat: add priority support(#550) by kilosonc · Pull Request #714 · casbin/casbin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add priority support(#550) #714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ func (e *Enforcer) LoadPolicy() error {
return err
}

if err := e.model.SortPoliciesByPriority(); err != nil {
return err
}

e.initRmMap()

if e.autoBuildRoleLinks {
Expand All @@ -301,6 +305,10 @@ func (e *Enforcer) loadFilteredPolicy(filter interface{}) error {
return err
}

if err := e.model.SortPoliciesByPriority(); err != nil {
return err
}

e.initRmMap()
e.model.PrintPolicy()
if e.autoBuildRoleLinks {
Expand Down
33 changes: 33 additions & 0 deletions enforcer_test.go
Original file line number Diff line number Diff line change
Expand Up 10000 @@ -499,3 +499,36 @@ func TestBatchEnforce(t *testing.T) {
testBatchEnforce(t, e, [][]interface{}{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"jack", "data3", "read"}}, results)
}

func TestPriorityExplicit(t *testing.T) {
e, _ := NewEnforcer("examples/priority_model_explicit.conf", "examples/priority_policy_explicit.csv")
testBatchEnforce(t, e, [][]interface{}{
{"alice", "data1", "write"},
{"alice", "data1", "read"},
{"bob", "data2", "read"},
{"bob", "data2", "write"},
{"data1_deny_group", "data1", "read"},
{"data1_deny_group", "data1", "write"},
{"data2_allow_group", "data2", "read"},
{"data2_allow_group", "data2", "write"},
}, []bool{
true, true, false, true, false, false, true, true,
})

_, err := e.AddPolicy("1", "bob", "data2", "write", "deny")
if err != nil {
t.Fatalf("Add Policy: %v", err)
}

testBatchEnforce(t, e, [][]interface{}{
{"alice", "data1", "write"},
{"alice", "data1", "read"},
{"bob", "data2", "read"},
{"bob", "data2", "write"},
{"data1_deny_group", "data1", "read"},
{"data1_deny_group", "data1", "write"},
{"data2_allow_group", "data2", "read"},
{"data2_allow_group", "data2", "write"},
}, []bool{
true, true, false, false, false, false, true, true,
})
}
14 changes: 14 additions & 0 deletions examples/priority_model_explicit.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[request_definition]
r = sub, obj, act

[policy_definition]
p = priority, sub, obj, act, eft

[role_definition]
g = _, _

[policy_effect]
e = priority(p.eft) || deny

[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
12 changes: 12 additions & 0 deletions examples/priority_policy_explicit.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
p, 10, data1_deny_group, data1, read, deny
p, 10, data1_deny_group, data1, write, deny
p, 10, data2_allow_group, data2, read, allow
p, 10, data2_allow_group, data2, write, allow


p, 1, alice, data1, write, allow
p, 1, alice, data1, read, allow
p, 1, bob, data2, read, deny

g, bob, data2_allow_group
g, alice, data1_deny_group
4 changes: 2 additions & 2 deletions internal_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ func (e *Enforcer) updatePolicy(sec string, ptype string, oldRule []string, newR
return ruleUpdated, nil
}

func (e *Enforcer) updatePolicies(sec string, ptype string , oldRules [][]string, newRules [][]string) (bool, error) {
func (e *Enforcer) updatePolicies(sec string, ptype string, oldRules [][]string, newRules [][]string) (bool, error) {
if e.dispatcher != nil && e.autoNotifyDispatcher {
return true, e.dispatcher.UpdatePolicies(sec, ptype, oldRules, newRules)
}

if e.shouldPersist() {
if err := e.adapter.(persist.UpdatableAdapter).UpdatePolicies(sec, ptype, oldRules, newRules); err != nil {
if err.Error() != notImplemented {
Expand Down
25 changes: 25 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package model

import (
"fmt"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -202,3 +203,27 @@ func (model Model) PrintModel() {

model.GetLogger().LogModel(modelInfo)
}

func (model Model) SortPoliciesByPriority() error {
for ptype, assertion := range model["p"] {
if assertion.Tokens[0] != fmt.Sprintf("%s_priority", ptype) {
continue
}
policies := assertion.Policy
sort.SliceStable(policies, func(i, j int) bool {
p1, err := strconv.ParseUint(policies[i][0], 10, 32)
if err != nil {
return true
}
p2, err := strconv.ParseUint(policies[j][0], 10, 32)
if err != nil {
return true
}
return p1 < p2
})
for i, policy := range assertion.Policy {
assertion.PolicyMap[strings.Join(policy, ",")] = i
}
}
return nil
}
27 changes: 23 additions & 4 deletions model/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package model

import (
"fmt"
"strconv"
"strings"

"github.com/casbin/casbin/v2/rbac"
Expand Down Expand Up @@ -143,8 +145,26 @@ func (model Model) HasPolicies(sec string, ptype string, rules [][]string) bool

// AddPolicy adds a policy rule to the model.
func (model Model) AddPolicy(sec string, ptype string, rule []string) {
model[sec][ptype].Policy = append(model[sec][ptype].Policy, rule)
model[sec][ptype].PolicyMap[strings.Join(rule, DefaultSep)] = len(model[sec][ptype].Policy) - 1
assertion := model[sec][ptype]
assertion.Policy = append(assertion.Policy, rule)
if idxInsert, err := strconv.ParseUint(rule[0], 10, 32); sec == "p" && assertion.Tokens[0] == fmt.Sprintf("%s_priority", ptype) && err == nil {
i := len(assertion.Policy) - 1
for ; i > 0; i-- {
idx, err := strconv.ParseUint(assertion.Policy[i-1][0], 10, 32)
if err != nil {
break
}
if idx > idxInsert {
assertion.Policy[i] = assertion.Policy[i-1]
} else {
break
}
}
assertion.Policy[i] = rule
assertion.PolicyMap[strings.Join(rule, DefaultSep)] = i
} else {
assertion.PolicyMap[strings.Join(rule, DefaultSep)] = len(model[sec][ptype].Policy) - 1
}
}

// AddPolicies adds policy rules to the model.
Expand All @@ -162,8 +182,7 @@ func (model Model) AddPoliciesWithAffected(sec string, ptype string, rules [][]s
continue
}
effected = append(effected, rule)
model[sec][ptype].Policy = append(model[sec][ptype].Policy, rule)
model[sec][ptype].PolicyMap[hashKey] = len(model[sec][ptype].Policy) - 1
model.AddPolicy(sec, ptype, rule)
}
return effected
}
Expand Down
1 change: 0 additions & 1 deletion rbac/default-role-manager/role_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,3 @@ func TestMatchingFuncOrder(t *testing.T) {
testRole(t, rm, "u1", "g1", true)
testRole(t, rm, "u1", "g2", true)
}

20 changes: 10 additions & 10 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,13 @@ func GetEvalValue(s string) []string {
}

func RemoveDuplicateElement(s []string) []string {
result := make([]string, 0, len(s))
temp := map[string]struct{}{}
for _, item := range s {
if _, ok := temp[item]; !ok {
temp[item] = struct{}{}
result = append(result, item)
}
}
return result
}
result := make([]string, 0, len(s))
temp := map[string]struct{}{}
for _, item := range s {
if _, ok := temp[item]; !ok {
temp[item] = struct{}{}
result = append(result, item)
}
}
return result
}
0