Skip to content

Commit

Permalink
feat: Add keymatch5 for ignoring params in url
Browse files Browse the repository at this point in the history
  • Loading branch information
AsakusaRinne committed Jun 3, 2022
1 parent 601a6c0 commit c29c95f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Casbin.UnitTest/UtilTests/BuiltInFunctionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@ public void TestKeyMatch4(string key1, string key2, bool expectedResult)
BuiltInFunctions.KeyMatch4(key1, key2));
}

public static IEnumerable<object[]> KeyMatch5TestData = new[]
{
new object[] { "/parent/child?status=1&type=2", "/parent/child", true},
new object[] { "/parent?status=1&type=2", "/parent/child", false},

new object[] { "/parent/child/?status=1&type=2", "/parent/child/", true},
new object[] { "/parent/child/?status=1&type=2", "/parent/child", false},
new object[] { "/parent/child?status=1&type=2", "/parent/child/", false}
};

[Theory]
[MemberData(nameof(KeyMatch5TestData))]
public void TestKeyMatch5(string key1, string key2, bool expectedResult)
{
Assert.Equal(expectedResult,
BuiltInFunctions.KeyMatch5(key1, key2));
}

public static IEnumerable<object[]> GlobMatchTestData = new[]
{
new object[] {"/foo", "/foo", true},
Expand Down
1 change: 1 addition & 0 deletions Casbin/Model/FunctionMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ internal static FunctionMap LoadFunctionMap()
map.AddFunction("keyMatch2", BuiltInFunctions.KeyMatch2);
map.AddFunction("keyMatch3", BuiltInFunctions.KeyMatch3);
map.AddFunction("keyMatch4", BuiltInFunctions.KeyMatch4);
map.AddFunction("keyMatch5", BuiltInFunctions.KeyMatch5);
map.AddFunction("regexMatch", BuiltInFunctions.RegexMatch);
map.AddFunction("ipMatch", BuiltInFunctions.IPMatch);
map.AddFunction("globMatch", BuiltInFunctions.GlobMatch);
Expand Down
16 changes: 16 additions & 0 deletions Casbin/Util/BuiltInFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ public static bool KeyMatch4(string key1, string key2)

return true;
}
/// <summary>
/// KeyMatch determines whether key1 matches the pattern of key2 and ignores the parameters in key2.
/// For example, "/foo/bar?status=1&amp;type=2" matches "/foo/bar"
/// </summary>
/// <param name="key1"> The first argument. </param>
/// <param name="key2"> The second argument. </param>
/// <returns></returns>
public static bool KeyMatch5(string key1, string key2)
{
var key1Span = key1.AsSpan();
var key2Span = key2.AsSpan();
int index = key1Span.IndexOf('?');
return index is -1
? key1Span.Equals(key2Span, StringComparison.Ordinal)
: key1Span.Slice(0, index).Equals(key2Span, StringComparison.Ordinal);
}

/// <summary>
/// Determines whether IP address ip1 matches the pattern of IP address ip2,
Expand Down

0 comments on commit c29c95f

Please sign in to comment.