メインコンテンツにスキップ

優先モデル

Casbinは優先度の負荷ポリシーをサポートしています。

優先度付き負荷ポリシー

それは非常に簡単です、順序が優先順位を決定し、以前に登場したポリシーはより高い優先順位を持っています。

model.conf:

[policy_effect]
e = priority(p.eft) || 否定する

優先度付き負荷ポリシー

また参照: casbin#550

The smaller priority value will have a higher priority. If there's a non-numerical character in priority, it will be in the last, rather than throw an error.

Token name convention

The priority token name in policy definition is "priority" conventionally. A customized one requires invoking e.SetFieldIndex() and reload policies (full example on TestCustomizedFieldIndex ).

model.conf:

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

Golang code example:

e, _ := NewEnforcer("./example/priority_model_explicit_customized.conf",
"./example/priority_policy_explicit_customized.csv")
// Due to the customized priority token, the enforcer failed to handle the priority.
ok, err := e.Enforce("bob", "data2", "read") // the result will be `true, nil`
// set PriorityIndex and reload
e.SetFieldIndex("p", constant.PriorityIndex, 0)
err := e.LoadPolicy()
if err != nil {
log.Fatalf("LoadPolicy: %v", err)
}
ok, err := e.Enforce("bob", "data2", "read") // the result will be `false, nil`

今、明示的な優先度のみサポート AddPolicy & AddPolicy, もし UpdatePolicy が呼び出された場合、priority 属性を変更するべきではありません。

model.conf:

[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

policy.csv

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

リクエスト:

alice, data1, write --> true // for `p, 1, alice, data1, write, allow` has highest priority
bob, data2, read --> false
bob, data2, write --> true // for bob has role of `data2_allow_group` which has right to write data2, and there's no deny policy with higher priority

ロールとユーザ階層に基づく優先度のあるポリシーの読み込み

ロールとユーザーの継承された構造は、グラフではなく複数のツリーのみとなります。 1つのユーザーが複数のロールを持つ場合は、ユーザーが異なるツリーで同じレベルを持っていることを確認する必要があります。 2つのロールが同じレベルにある場合、ポリシー(ロール対応)が優先されます。 詳細も参照してください casbin#833casbin#831

model.conf:

[request_definition]
r = sub, obj, act

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

[role_definition]
g = _, _

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

[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act

policy.csv

p, root, data1, read, deny
p, admin, data1, read, deny

p, editor, data1, read, deny
p, subscriber, data1, read, deny

p, jane, data1, read, allow
p, alice, data1, read, allow

g, admin, root

g, editor, admin
g, subscriber, admin

g, jane, editor
g, alice, subscriber

リクエスト:

jane, data1, read --> true // jane is at the bottom,so priority is higher than editor, admin and root
alice, data1, read --> true

次のようなロール階層:

role: root
└─ role: admin
├─ role editor
│ └─ user: jane

└─ role: subscriber
└─ user: alice

優先度は次のようになります。

role: root # auto priority: 30
└─ role: admin # auto priority: 20
├─ role: editor # auto priority: 10
├─ role: subscriber # auto priority: 10