Functions
Eşleştiricilerdeki Fonksiyonlar
You can use built-in functions in matchers or register custom ones. Built-in key-matching functions have the form:
bool function_name(string url, string pattern)
They return whether url matches pattern.
Available built-in functions:
| Fonksiyon | url | kalıp | Örnek |
|---|---|---|---|
| keyMatch | /alice_data/resource1 gibi bir URL yolu | /alice_data/* gibi bir URL yolu veya * deseni | keymatch_model.conf/keymatch_policy.csv |
| keyMatch2 | /alice_data/resource1 gibi bir URL yolu | /alice_data/:resource gibi bir URL yolu veya : deseni | keymatch2_model.conf/keymatch2_policy.csv |
| keyMatch3 | /alice_data/resource1 gibi bir URL yolu | bir URL yolu veya /alice_data/{resource} gibi bir {} kalıbı | https://github.com/casbin/casbin/blob/277c1a2b85698272f764d71a94d2595a8d425915/util/builtin_operators_test.go#L171-L196 |
| keyMatch4 | /alice_data/123/book/123 gibi bir URL yolu | bir URL yolu veya /alice_data/{id}/book/{id} gibi bir {} kalıbı | https://github.com/casbin/casbin/blob/277c1a2b85698272f764d71a94d2595a8d425915/util/builtin_operators_test.go#L208-L222 |
| keyMatch5 | /alice_data/123/?status=1 gibi bir URL yolu | bir URL yolu, /alice_data/{id}/* gibi bir {} veya * kalıbı | https://github.com/casbin/casbin/blob/1cde2646d10ad1190c0d784c3a1c0e1ace1b5bc9/util/builtin_operators_test.go#L485-L526 |
| regexMatch | herhangi bir dize | normal bir ifade deseni | keymatch_model.conf/keymatch_policy.csv |
| ipMatch | 192.168.2.123 gibi bir IP adresi | 192.168.2.0/24 gibi bir IP adresi veya CIDR | ipmatch_model.conf/ipmatch_policy.csv |
| globMatch | /alice_data/resource1 gibi bir yol benzeri yol | /alice_data/* gibi bir glob kalıbı | https://github.com/casbin/casbin/blob/277c1a2b85698272f764d71a94d2595a8d425915/util/builtin_operators_test.go#L426-L466 |
Key-extraction functions take three parameters (except keyGet, which takes two):
bool function_name(string url, string pattern, string key_name)
They return the value of the named key when the URL matches the pattern, or "" otherwise.
Examples:
KeyGet2("/resource1/action", "/:res/action", "res")→"resource1"KeyGet3("/resource1_admin/action", "/{res}_admin/*", "res")→"resource1"KeyGet("/resource1/action", "/*")→"resource1/action"(two-parameter form)
| Fonksiyon | url | kalıp | key_name | örnek |
|---|---|---|---|---|
| anahtarAl | /proj/resource1 gibi bir URL yolu | /proj/* gibi bir URL yolu veya * kalıbı | \ | keyget_model.conf/keymatch_policy.csv |
| anahtarAl2 | /proj/resource1 gibi bir URL yolu | a URL path or : pattern like /proj/:resource | kalıpta belirtilen anahtar adı | keyget2_model.conf/keymatch2_policy.csv |
| keyGet3 | /proj/res3_admin/ gibi bir URL yolu | /proj/{resource}_admin/* gibi bir URL yolu veya {} kalıbı | Kalıpta belirtilen anahtar adı | https://github.com/casbin/casbin/blob/7bd496f94f5a2739a392d333a9aaaa10ae397673/util/builtin_operators_test.go#L209-L247 |
Complete function details: https://github.com/casbin/casbin/blob/master/util/builtin_operators_test.go
Adding a custom function
- Implement a function that takes the required arguments 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]
}
- Wrap it for Casbin (signature
func(...interface{}) (interface{}, error)):
func KeyMatchFunc(args ...interface{}) (interface{}, error) {
name1 := args[0].(string)
name2 := args[1].(string)
return (bool)(KeyMatch(name1, name2)), nil
}
- Register it on the enforcer:
e.AddFunction("my_func", KeyMatchFunc)
- Use it in your model:
[matchers]
m = r.sub == p.sub && my_func(r.obj, p.obj) && r.act == p.act