우선순위 모델
Casbin은 정책에 **우선순위(priority)**를 적용할 수 있습니다. 여러 정책이 일치할 경우, 가장 높은 우선순위를 가진 정책이 승리하며(허용 또는 거부 를 생성할 수 있음), 그 결과가 적용됩니다.
암시적 우선순위 (정책 순서)
정책 순서가 우선순위를 정의합니다: 목록에서 첫 번째 항목이 가장 높은 우선순위를 가집니다. 이 효과를 사용하세요:
[policy_effect]
e = priority(p.eft) || deny
명시적 우선순위 (숫자 필드)
정책에 priority 필드를 추가하세요. 숫자가 낮을수록 우선순위가 높습니다. 숫자가 아닌 값은 가장 낮은 우선순위로 처리됩니다. casbin#550을 참조하세요.
기본 우선순위 필드 이름은 priority입니다. 다른 이름(예: customized_priority)을 사용하려면 초기화 후 e.SetFieldIndex("p", constant.PriorityIndex, index)를 호출하고 정책을 다시 로드하세요. 예시: TestCustomizedFieldIndex.
명시적 우선순위가 포함된 정책 예시:
[policy_definition]
p = customized_priority, sub, obj, act, eft
Golang 코드 예시:
e, _ := NewEnforcer("./example/priority_model_explicit_customized.conf",
"./example/priority_policy_explicit_customized.csv")
// Due to the customized priority token, the enforcer fails 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와 AddPolicies만 명시적 우선순위를 따릅니다. UpdatePolicy를 통해 우선순위 필드를 변경하지 마세요.
전체 모델:
[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
request:
alice, data1, write --> true // because `p, 1, alice, data1, write, allow` has the highest priority
bob, data2, read --> false
bob, data2, write --> true // because bob has the role of `data2_allow_group` which has the right to write data2, and there's no deny policy with higher priority
주체 우선순위 (역할/사용자 계층)
subjectPriority(p.eft)를 사용하여 우선순위를 역할 계층 구조에서 가져오세요. 리프 노드(예: 최종 사용자)는 내부 역할보다 높은 우선순위를 가집니다. 계층 구조는 그래프가 아닌 트리여야 하며, 사용자가 여러 역할을 가질 경우 각 트리에서 동일한 깊이에 있어야 합니다. 같은 수준의 동률은 정책 순서로 해결됩니다. casbin#833, casbin#831을 참조하세요.
Model:
[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
Request:
jane, data1, read --> true // because jane is at the bottom, her priority is higher than that of 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
Comments are powered by giscus and GitHub Discussions. They load from giscus.app and GitHub only after you click the button below.