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

関数

マッチャー内の関数

マッチャー内の関数を指定して、よりパワフルにすることもできます。 組み込み関数を使用したり、独自の関数を指定したりできます。 The built-in key-matching functions take such a format:

bool function_name(string url, string pattern)

It returns a boolean indicating whether url matches pattern.

サポートされている組み込み関数は次のとおりです。

関数urlpattern
keyMatch/alice_data/resource1 のような URL パスURL パスまたは * パターン。 /alice_data/*keymatch_model.conf/keymatch_policy.csv
keyMatch2/alice_data/resource1 のような URL パスURL パスまたは : /alice_data/:resource のようなパターンkeymatch2_model.conf/keymatch2_policy.csv
keyMatch3/alice_data/resource1 のような URL パスURL パスまたは {} /alice_data/{resource}のようなパターンhttps://github.com/casbin/blob/277c1a2b85698272f764d71a94d2595a8d425915/util/builtin_operators_test.go#L171-L196
keyMatch4/alice_data/123/book/123 のURLパスURL パスまたは {} パターンのような /alice_data/{id}/book/{id}https://github.com/casbin/blob/277c1a2b85698272f764d71a94d2595a8d425915/util/builtin_operators_test.go#L208-L222
正規表現一致すべての文字列正規表現のパターンですkeymatch_model.conf/keymatch_policy.csv
ipMatch192.168.2.123 のようなIP アドレス192.168.2.0/24 のようなIPアドレスまたはCIDRipmatch_model.conf/ipmatch_policy.csv
globMatch/alice_data/resource1 のようなパス/alice_data/* のようなグローブパターンhttps://github.com/casbin/blob/277c1a2b85698272f764d71a94d2595a8d425915/util/builtin_operators_test.go#L426-L466

For key-getting functions, they usually take three parameters(except keyGet):

bool function_name(string url, string pattern, string key_name)

They will return the value of the key key_name if it matches the pattern, and return "" if nothing is matched.

For example, KeyGet2("/resource1/action", "/:res/action, "res") will return "resource1", KeyGet3("/resource1_admin/action", "/{res}_admin/*", "res") will return "resource1". As for KeyGet, which takes two parameters, KeyGet("/resource1/action", "/*) will return "resource1/action".

Functionurlpatternkey_nameexample
keyGeta URL path like /proj/resource1a URL path or a * pattern like /proj/*\ keyget_model.conf/keymatch_policy.csv
keyGet2a URL path like /proj/resource1a URL path or : pattern like /prooj/:resourcekey name specified in the patternkeyget2_model.conf/keymatch2_policy.csv
keyGet3a URL path like /proj/res3_admin/a URL path or {} pattern like /proj/{resource}_admin/*key name specified in the patternhttps://github.com/casbin/casbin/blob/7bd496f94f5a2739a392d333a9aaaa10ae397673/util/builtin_operators_test.go#L209-L247

上記の関数の詳細は https://github.com/casbin/casbin/blob/master/util/builtin_operators_test.go

カスタマイズした機能を追加する方法

まず、関数を準備します。 いくつかのパラメータを取り、boolを返します。

func KeyMatch(key1 string, key2 string) bool {
i := strings.Index(key2, "*")
if i == -1 {
return key1 == key2
}

if len(key1) > i {
return key1[:i] == key2[:i]
}
return key1 == key2[:i]
}

次に、 interface{} タイプでラップします。

func KeyMatchFunc(args ...interface{}) (interface{}, error) {
name1 := args[0].(string)
name2 := args[1].(string)

return (bool)(KeyMatch(name1, name2)), nil
}

最後に、Casbin enforcerにこの関数を登録します。

e.AddFunction("my_func", KeyMatchFunc)

これで、モデルCONFの関数を次のように使用できます。

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