Zum Hauptinhalt springen

Functions

Funktionen in Übereinstimmungen

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:

FunktionurlpatternBeispiel
keyMatchein URL-Pfad wie /alice_data/resource1ein URL-Pfad oder ein * Muster wie /alice_data/*keymatch_model.conf/keymatch_policy.csv
keyMatch2ein URL-Pfad wie /alice_data/resource1ein URL-Pfad oder ein : Muster wie /alice_data/:resourcekeymatch2_model.conf/keymatch2_policy.csv
keyMatch3ein URL-Pfad wie /alice_data/resource1ein URL-Pfad oder ein {} Muster wie /alice_data/{resource}https://github.com/casbin/casbin/blob/277c1a2b85698272f764d71a94d2595a8d425915/util/builtin_operators_test.go#L171-L196
keyMatch4ein URL-Pfad wie /alice_data/123/book/123ein URL-Pfad oder ein {} Muster wie /alice_data/{id}/book/{id}https://github.com/casbin/casbin/blob/277c1a2b85698272f764d71a94d2595a8d425915/util/builtin_operators_test.go#L208-L222
keyMatch5ein URL-Pfad wie /alice_data/123/?status=1ein URL-Pfad, ein {} oder * Muster wie /alice_data/{id}/*https://github.com/casbin/casbin/blob/1cde2646d10ad1190c0d784c3a1c0e1ace1b5bc9/util/builtin_operators_test.go#L485-L526
regexMatchbeliebiger Stringein reguläres Ausdrucksmusterkeymatch_model.conf/keymatch_policy.csv
ipMatcheine IP-Adresse wie 192.168.2.123eine IP-Adresse oder ein CIDR wie 192.168.2.0/24ipmatch_model.conf/ipmatch_policy.csv
globMatchein pfadähnlicher Pfad wie /alice_data/resource1ein glob-Muster wie /alice_data/*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)
FunktionurlMusterkey_nameBeispiel
keyGetein URL-Pfad wie /proj/resource1ein URL-Pfad oder ein * Muster wie /proj/*\keyget_model.conf/keymatch_policy.csv
keyGet2ein URL-Pfad wie /proj/resource1a URL path or : pattern like /proj/:resourceSchlüsselname, der im Muster angegeben istkeyget2_model.conf/keymatch2_policy.csv
keyGet3ein URL-Pfad wie /proj/res3_admin/ein URL-Pfad oder {} Muster wie /proj/{resource}_admin/*Schlüsselname, der im Muster angegeben isthttps://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

  1. 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]
}
  1. 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
}
  1. Register it on the enforcer:
e.AddFunction("my_func", KeyMatchFunc)
  1. Use it in your model:
[matchers]
m = r.sub == p.sub && my_func(r.obj, p.obj) && r.act == p.act