Skip to content

Commit

Permalink
feat: Add globMatch as a global built-in matcher function (#232)
Browse files Browse the repository at this point in the history
* #231 add globMatch as a global built-in matcher function

* feat: Add globMatch as a global built-in matcher function

Signed-off-by: Leon Segal <leon.segal@hs-soft.com>

Co-authored-by: Leon Segal <leon.segal@hs-soft.com>
  • Loading branch information
dviry and Leon Segal committed Nov 24, 2021
1 parent 3c91830 commit 49cce4d
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 14 deletions.
72 changes: 60 additions & 12 deletions NetCasbin.UnitTest/BuiltInFunctionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public class BuiltInFunctionTest

[Theory]
[MemberData(nameof(ipMatchTestData))]
public void TestIPMatch(string key1, string key2, bool exceptResult)
public void TestIPMatch(string key1, string key2, bool expectedResult)
{
Assert.Equal(exceptResult,
Assert.Equal(expectedResult,
BuiltInFunctions.IPMatch(key1, key2));
}

Expand All @@ -44,9 +44,9 @@ public void TestIPMatch(string key1, string key2, bool exceptResult)

[Theory]
[MemberData(nameof(regexMatchTestData))]
public void TestRegexMatch(string key1, string key2, bool exceptResult)
public void TestRegexMatch(string key1, string key2, bool expectedResult)
{
Assert.Equal(exceptResult,
Assert.Equal(expectedResult,
BuiltInFunctions.RegexMatch(key1, key2));
}

Expand All @@ -65,9 +65,9 @@ public void TestRegexMatch(string key1, string key2, bool exceptResult)

[Theory]
[MemberData(nameof(keyMatchTestData))]
public void TestKeyMatch(string key1, string key2, bool exceptResult)
public void TestKeyMatch(string key1, string key2, bool expectedResult)
{
Assert.Equal(exceptResult,
Assert.Equal(expectedResult,
BuiltInFunctions.KeyMatch(key1, key2));
}

Expand Down Expand Up @@ -105,9 +105,9 @@ public void TestKeyMatch(string key1, string key2, bool exceptResult)

[Theory]
[MemberData(nameof(KeyMatch2TestData))]
public void TestKeyMatch2(string key1, string key2, bool exceptResult)
public void TestKeyMatch2(string key1, string key2, bool expectedResult)
{
Assert.Equal(exceptResult,
Assert.Equal(expectedResult,
BuiltInFunctions.KeyMatch2(key1, key2));
}

Expand Down Expand Up @@ -141,9 +141,9 @@ public void TestKeyMatch2(string key1, string key2, bool exceptResult)

[Theory]
[MemberData(nameof(KeyMatch3TestData))]
public void TestKeyMatch3(string key1, string key2, bool exceptResult)
public void TestKeyMatch3(string key1, string key2, bool expectedResult)
{
Assert.Equal(exceptResult,
Assert.Equal(expectedResult,
BuiltInFunctions.KeyMatch3(key1, key2));
}

Expand All @@ -166,10 +166,58 @@ public void TestKeyMatch3(string key1, string key2, bool exceptResult)

[Theory]
[MemberData(nameof(KeyMatch4TestData))]
public void TestKeyMatch4(string key1, string key2, bool exceptResult)
public void TestKeyMatch4(string key1, string key2, bool expectedResult)
{
Assert.Equal(exceptResult,
Assert.Equal(expectedResult,
BuiltInFunctions.KeyMatch4(key1, key2));
}

public static IEnumerable<object[]> GlobMatchTestData = new[]
{
new object[] {"/foo", "/foo", true},
new object[] {"/foo", "/foo*", true},
new object[] {"/foo", "/foo/*", false},
new object[] {"/foo/bar", "/foo", false},
new object[] {"/foo/bar", "/foo*", false},
new object[] {"/foo/bar", "/foo/*", true},
new object[] {"/foobar", "/foo", false},
new object[] {"/foobar", "/foo*", true},
new object[] {"/foobar", "/foo/*", false},
new object[] {"/foo", "*/foo", true},
new object[] {"/foo", "*/foo*", true},
new object[] {"/foo", "*/foo/*", false},
new object[] {"/foo/bar", "*/foo", false},
new object[] {"/foo/bar", "*/foo*", false},
new object[] {"/foo/bar", "*/foo/*", true},
new object[] {"/foobar", "*/foo", false},
new object[] {"/foobar", "*/foo*", true},
new object[] {"/foobar", "*/foo/*", false},
new object[] {"/prefix/foo", "*/foo", false},
new object[] {"/prefix/foo", "*/foo*", false},
new object[] {"/prefix/foo", "*/foo/*", false},
new object[] {"/prefix/foo/bar", "*/foo", false},
new object[] {"/prefix/foo/bar", "*/foo*", false},
new object[] {"/prefix/foo/bar", "*/foo/*", false},
new object[] {"/prefix/foobar", "*/foo", false},
new object[] {"/prefix/foobar", "*/foo*", false},
new object[] {"/prefix/foobar", "*/foo/*", false},
new object[] {"/prefix/subprefix/foo", "*/foo", false},
new object[] {"/prefix/subprefix/foo", "*/foo*", false},
new object[] {"/prefix/subprefix/foo", "*/foo/*", false},
new object[] {"/prefix/subprefix/foo/bar", "*/foo", false},
new object[] {"/prefix/subprefix/foo/bar", "*/foo*", false},
new object[] {"/prefix/subprefix/foo/bar", "*/foo/*", false},
new object[] {"/prefix/subprefix/foobar", "*/foo", false},
new object[] {"/prefix/subprefix/foobar", "*/foo*", false},
new object[] {"/prefix/subprefix/foobar", "*/foo/*", false},
};

[Theory]
[MemberData(nameof(GlobMatchTestData))]
public void TestGlobMatch(string key1, string key2, bool expectedResult)
{
Assert.Equal(expectedResult,
BuiltInFunctions.GlobMatch(key1, key2));
}
}
}
4 changes: 2 additions & 2 deletions NetCasbin/Model/FunctionMap.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using NetCasbin.Util;
using NetCasbin.Util.Function;

namespace NetCasbin.Model
Expand All @@ -21,12 +20,13 @@ public static FunctionMap LoadFunctionMap()
FunctionDict = new Dictionary<string, Delegate>()
};

map.AddFunction("keyMatch", new KeyMatchFunc());
map.AddFunction("keyMatch", new KeyMatchFunc());
map.AddFunction("keyMatch2", new KeyMatch2Func());
map.AddFunction("keyMatch3", new KeyMatch3Func());
map.AddFunction("keyMatch4", new KeyMatch4Func());
map.AddFunction("regexMatch", new RegexMatchFunc());
map.AddFunction("ipMatch", new IPMatchFunc());
map.AddFunction("globMatch", new GlobMatchFunc());
return map;
}
}
Expand Down
1 change: 1 addition & 0 deletions NetCasbin/NetCasbin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

<ItemGroup>
<PackageReference Include="DynamicExpresso.Core" Version="2.4.1" />
<PackageReference Include="DotNet.Glob" Version="3.1.3" />
<PackageReference Include="System.Memory" Version="4.5.4" />
</ItemGroup>

Expand Down
12 changes: 12 additions & 0 deletions NetCasbin/Util/BuiltInFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Net;
using System.Text.RegularExpressions;
using DotNet.Globbing;
using NetCasbin.Abstractions;
using NetCasbin.Extensions;
using NetCasbin.Rbac;
Expand Down Expand Up @@ -188,6 +189,17 @@ public static bool RegexMatch(string key1, string key2)
return Regex.Match(key1, key2).Success;
}

/// <summary>
/// Determines whether key1 matches the globbing pattern of key2
/// </summary>
/// <param name="key1">The keyword to match</param>
/// <param name="key2">The Globbing pattern</param>
/// <returns>Whether key1 matches key2.</returns>
public static bool GlobMatch(string key1, string key2)
{
return Glob.Parse(key2).IsMatch(key1);
}

/// <summary>
/// GenerateGFunction is the factory method of the g(_, _) function.
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions NetCasbin/Util/Function/GlobMatchFunc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Net;
using NetCasbin.Abstractions;

namespace NetCasbin.Util.Function
{
public class GlobMatchFunc : AbstractFunction
{
protected override Delegate GetFunc()
{
Func<string, string, bool> call = BuiltInFunctions.GlobMatch;
return call;
}

public GlobMatchFunc() : base("globMatch")
{

}
}
}

0 comments on commit 49cce4d

Please sign in to comment.