パターン付きRBAC
クイックスタート
Use pattern in
g(_, _)
.e, _ := NewEnforcer("./example.conf", "./example.csv")
e.AddNamedMatchingFunc("g", "KeyMatch2", util.KeyMatch2)Use pattern with domain.
e.AddNamedDomainMatchingFunc("g", "KeyMatch2", util.KeyMatch2)
Use all patterns.
Just combine the use of both APIs.
As shown above, after you create the enforcer
instance, you need to activate pattern matching via the AddNamedMatchingFunc
and AddNamedDomainMatchingFunc
APIs, which determine how the pattern matches.
オンラインエディタを使用する場合、左下隅のパターンマッチング機能を指定します。
RBACでパターンマッチングを使用
Sometimes, you want certain subjects, objects, or domains/tenants with a specific pattern to be automatically granted a role. RBACのパターンマッチング機能はそれを助けることができます。 パターンマッチング関数は、以前の マッチャ関数と同じパラメータと戻り値を共有します。
The pattern matching function supports each parameter of g
.
We know that normally RBAC is expressed as g(r.sub, p.sub)
in a matcher. Then we can use a policy like:
p, alice, book_group, read
g, /book/1, book_group
g, /book/2, book_group
alice
は book 1
や book 2
を含むすべての本を読むことができます。 But there can be thousands of books, and it's very tedious to add each book to the book role (or group) with one g
policy rule.
しかし、パターンマッチング関数では、ポリシーを1行だけで書くことができます。
g, /book/:id, book_group
Casbin will automatically match /book/1
and /book/2
into the pattern /book/:id
for you. この関数をエンフォーサーに登録する必要があるのは以下のようなものです:
- Go
- Node.js
e.AddNamedMatchingFunc("g", "KeyMatch2", util.KeyMatch2)
await e.addNamedMatchingFunc('g', Util.keyMatch2Func);
When using a pattern matching function in domains/tenants, you need to register the function with the enforcer and model.
- Go
- Node.js
e.AddNamedDomainMatchingFunc("g", "KeyMatch2", util.KeyMatch2)
await e.addNamedDomainMatchingFunc('g', Util.keyMatch2Func);
g(r.sub, p.sub, r.dom)
の意味がわからない場合は、 rbac-with-domains を読んでください。 In short, g(r.sub, p.sub, r.dom)
will check whether the user r.sub
has a role p.sub
in the domain r.dom
. So this is how the matcher works. 完全な例 はこちら をご覧ください。
上記のパターンマッチング構文とは別に、純粋なドメインパターンを使用することもできます。
For example, if we want sub
to have access in different domains, domain1
and domain2
, we can use the pure domain pattern:
p, admin, domain1, data1, read
p, admin, domain1, data1, write
p, admin, domain2, data2, read
p, admin, domain2, data2, write
g, alice, admin, *
g, bob, admin, domain2
In this example, we want alice
to read and write data
in domain1 and domain2. Pattern matching *
in g
makes alice
have access to two domains.
By using pattern matching, especially in scenarios that are more complicated and have a lot of domains or objects to consider, we can implement the policy_definition
in a more elegant and effective way.