8000 feat: keyMatch5 for ignoring params in url by Abingcbc · Pull Request #910 · casbin/casbin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: keyMatch5 for ignoring params in url #910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions model/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func LoadFunctionMap() FunctionMap {
fm.AddFunction("keyGet2", util.KeyGet2Func)
fm.AddFunction("keyMatch3", util.KeyMatch3Func)
fm.AddFunction("keyMatch4", util.KeyMatch4Func)
fm.AddFunction("keyMatch5", util.KeyMatch5Func)
fm.AddFunction("regexMatch", util.RegexMatchFunc)
fm.AddFunction("ipMatch", util.IPMatchFunc)
fm.AddFunction("globMatch", util.GlobMatchFunc)
Expand Down
23 changes: 23 additions & 0 deletions util/builtin_operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,29 @@ func KeyMatch4Func(args ...interface{}) (interface{}, error) {
return bool(KeyMatch4(name1, name2)), nil
}

// KeyMatch determines whether key1 matches the pattern of key2 and ignores the parameters in key2.
// For example, "/foo/bar?status=1&type=2" matches "/foo/bar"
func KeyMatch5(key1 string, key2 string) bool {
i := strings.Index(key1, "?")
if i == -1 {
return key1 == key2
}

return key1[:i] == key2
}

// KeyMatch5Func is the wrapper for KeyMatch5.
func KeyMatch5Func(args ...interface{}) (interface{}, error) {
if err := validateVariadicArgs(2, args...); err != nil {
return false, fmt.Errorf("%s: %s", "keyMatch5", err)
}

name1 := args[0].(string)
name2 := args[1].(string)

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

// RegexMatch determines whether key1 matches the pattern of key2 in regular expression.
func RegexMatch(key1 string, key2 string) bool {
res, err := regexp.MatchString(key2, key1)
Expand Down
28 changes: 28 additions & 0 deletions util/builtin_operators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,20 @@ func testKeyMatch4Func(t *testing.T, res bool, err string, args ...interface{})
}
}

func testKeyMatch5Func(t *testing.T, res bool, err string, args ...interface{}) {
t.Helper()
myRes, myErr := KeyMatch5Func(args...)
myErrStr := ""

if myErr != nil {
myErrStr = myErr.Error()
}

if myRes != res || err != myErrStr {
t.Errorf("%v returns %v %v, supposed to be %v %v", args, myRes, myErr, res, err)
}
}

func testIPMatchFunc(t *testing.T, res bool, err string, args ...interface{}) {
t.Helper()
myRes, myErr := IPMatchFunc(args...)
Expand Down Expand Up @@ -417,6 +431,20 @@ func TestKeyMatch4Func(t *testing.T) {

}

func TestKeyMatch5Func(t *testing.T) {
testKeyMatch5Func(t, false, "keyMatch5: Expected 2 arguments, but got 1", "/foo")
testKeyMatch5Func(t, false, "keyMatch5: Expected 2 arguments, but got 3", "/foo/create/123", "/foo/*", "/foo/update/123")
testKeyMatch5Func(t, false, "keyMatch5: Argument must be a string", "/parent/123", true)

testKeyMatch5Func(t, true, "", "/parent/child?status=1&type=2", "/parent/child")
testKeyMatch5Func(t, false, "", "/parent?status=1&type=2", "/parent/child")

testKeyMatch5Func(t, true, "", "/parent/child/?status=1&type=2", "/parent/child/")
testKeyMatch5Func(t, false, "", "/parent/child/?status=1&type=2", "/parent/child")
testKeyMatch5Func(t, false, "", "/parent/child?status=1&type=2", "/parent/child/")

}

func TestIPMatchFunc(t *testing.T) {
testIPMatchFunc(t, false, "ipMatch: Expected 2 arguments, but got 1", "192.168.2.123")
testIPMatchFunc(t, false, "ipMatch: Argument must be a string", "192.168.2.123", 128)
Expand Down
0