跳转至主要内容

Functions

Matchers中的函数

你甚至可以在Matcher中指定函数,使它更强大。 You can use built-in functions or specify your own function. The built-in key-matching functions take the following format:

bool function_name(string url, string pattern)

They return a boolean indicating whether the url matches the pattern.

支持的内置函数如下:

函数url模式示例
keyMatch一个URL 路径,例如 /alice_data/resource1一个URL 路径或 * 模式下,例如 /alice_data/*keymatch_model.conf/keymatch_policy.csv
keyMatch2一个URL 路径,例如 /alice_data/resource1一个URL 路径或 : 模式下,例如 /alice_data/:resourcekeymatch2_model.conf/keymatch2_policy.csv
keyMatch3一个URL 路径,例如 /alice_data/resource1一个URL 路径或 {} 模式下,例如 /alice_data/{resource}https://github.com/casbin/casbin/blob/277c1a2b85698272f764d71a94d2595a8d425915/util/builtin_operators_test.go#L171-L196
keyMatch4一个URL 路径,例如 /alice_data/resource1一个URL 路径或 {} 模式下,例如 /alice_data//{id}/book/{id}https://github.com/casbin/casbin/blob/277c1a2b85698272f764d71a94d2595a8d425915/util/builtin_operators_test.go#L208-L222
keyMatch5a URL path like /alice_data/123/?status=1a URL path, a {} or * pattern like /alice_data/{id}/*https://github.com/casbin/casbin/blob/1cde2646d10ad1190c0d784c3a1c0e1ace1b5bc9/util/builtin_operators_test.go#L485-L526
regexMatch任意字符串正则表达式模式keymatch_model.conf/keymatch_policy.csv
ipMatch一个 IP 地址,例如 192.168.2.123一个 IP 地址或一个 CIDR ,例如192.168.2.0/24ipmatch_model.conf/ipmatch_policy.csv
globMatch类似路径的 /alice_data/resource1一个全局模式,例如 /alice_data/*https://github.com/casbin/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)

如果密钥 key_name 与模式匹配,他们将返回它的值。如果没有匹配,则返回 ""

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

函数url模式密钥名称示例
keyGet一个URL 路径,例如 /alice_data/resource1一个URL 路径或 * 模式下,例如 /alice_data/*\ keyget_model.conf/keymatch_policy.csv
keyGet2a URL path like /proj/resource1一个URL 路径或 : 模式下,例如 /alice_data/:resource模式中指定的密钥名称keyget_model.conf/keymatch_policy.csv
keyGet3一个URL 路径,例如 /proj/res3_admin/ 一个URL 路径或 {} 模式下,例如 /proj/{resource}_admin/ key name specified in the patternhttps://github.com/casbin/casbin/blob/7bd496f94f5a2739a392d333a9aaaa10ae397673/util/builtin_operators_test.go#L209-L247

See details for the above functions at: https://github.com/casbin/casbin/blob/master/util/builtin_operators_test.go

怎样增加自定义函数

First, prepare your function. It takes several parameters and returns a 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]
}

Then, wrap it with interface{} types:

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

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

Finally, register the function to the Casbin enforcer:

e.AddFunction("my_func", KeyMatchFunc)

现在,您可以在您的模型CONF中像这样使用这个函数:

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