Skip to content
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

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

Merged
merged 3 commits into from
Nov 24, 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
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")
{

}
}
}