Skip to content

Commit

Permalink
Revert "feat: fix null value handling logic (casbin#337)"
Browse files Browse the repository at this point in the history
This reverts commit 54db948.
  • Loading branch information
sagilio committed Apr 17, 2024
1 parent 43f4ef4 commit 8f647b5
Show file tree
Hide file tree
Showing 8 changed files with 533 additions and 126 deletions.
8 changes: 4 additions & 4 deletions Casbin.Benchmark/DefaultPolicyManagerBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ public void GlobalSetup()
if (num == 0)
{
NowTestExistedPolicyList.Add(
new PolicyValues($"group{i}", $"obj{i / 10}", "read"));
new PolicyValues<string, string, string>($"group{i}", $"obj{i / 10}", "read"));
NowTestNullPolicyList.Add(
new PolicyValues($"name{i}", $"data{i / 10}", "read"));
new PolicyValues<string, string, string>($"name{i}", $"data{i / 10}", "read"));
}
}

Console.WriteLine($"// Already set {NowPolicyCount} policies.");

NowTestUserName = $"name{NowPolicyCount / 2 + 1}";
NowTestDataName = $"data{NowPolicyCount / 2 + 1}";
NowTestPolicy = new PolicyValues(NowTestUserName, NowTestDataName, "read");
NowTestPolicy = new PolicyValues<string, string, string>(NowTestUserName, NowTestDataName, "read");
Console.WriteLine($"// Already set user name to {NowTestUserName}.");
Console.WriteLine($"// Already set data name to {NowTestDataName}.");
}
Expand All @@ -83,7 +83,7 @@ public async Task RemovePolicyAsync()
public async Task UpdatePolicyAsync()
{
await _policyManager.UpdatePolicyAsync(NowTestPolicy,
new PolicyValues(NowTestUserName + "up", NowTestDataName + "up", "read"));
new PolicyValues<string, string, string>(NowTestUserName + "up", NowTestDataName + "up", "read"));
}

[Benchmark]
Expand Down
33 changes: 16 additions & 17 deletions Casbin.UnitTests/GenericTests/GenericMatcherTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,25 @@ public class GenericMatcherTest
[Fact]
public void TestGenericMatcher()
{
//RequestValues<string, int> r = Request.CreateValues("A", 1);
//PolicyValues p = Policy.CreateValues("A", 1);
//Func<RequestValues<string, int>, PolicyValues, bool> func1 = Compile(
// "r.Value1 == p[0].GetType()(p[0]) && r.Value2 == p[1].GetType()(p[1])",
// nameof(r), in r, nameof(p), in p);
RequestValues<string, int> r = Request.CreateValues("A", 1);
PolicyValues<string, int> p = Policy.CreateValues("A", 1);
Func<RequestValues<string, int>, PolicyValues<string, int>, bool> func1 = Compile(
"r.Value1 == p.Value1 && r.Value2 == p.Value2",
nameof(r), in r, nameof(p), in p);

//Assert.True(func1(Request.CreateValues("A", 1), Policy.CreateValues("A", 1)));
//Assert.False(func1(Request.CreateValues("A", 1), Policy.CreateValues("A", 2)));
//Assert.False(func1(Request.CreateValues("B", 1), Policy.CreateValues("B", 2)));
Assert.True(func1(Request.CreateValues("A", 1), Policy.CreateValues("A", 1)));
Assert.False(func1(Request.CreateValues("A", 1), Policy.CreateValues("A", 2)));
Assert.False(func1(Request.CreateValues("B", 1), Policy.CreateValues("B", 2)));

//RequestValues<string, int, string> r2 = Request.CreateValues("A", 1, "read");
//PolicyValues p2 = Policy.CreateValues("A", 1, "read");
//Func<RequestValues<string, int, string>, PolicyValues, bool> func2 = Compile(
// "r2.Value1 == p2[0] && r2.Value2 == p2[1] && r2.Value3 == p2[2]",
// nameof(r2), in r2, nameof(p2), in p2);
RequestValues<string, int, string> r2 = Request.CreateValues("A", 1, "read");
PolicyValues<string, int, string> p2 = Policy.CreateValues("A", 1, "read");
Func<RequestValues<string, int, string>, PolicyValues<string, int, string>, bool> func2 = Compile(
"r2.Value1 == p2.Value1 && r2.Value2 == p2.Value2 && r2.Value3 == p2.Value3",
nameof(r2), in r2, nameof(p2), in p2);

//Assert.True(func2(Request.CreateValues("A", 1, "read"), Policy.CreateValues("A", 1, "read")));
//Assert.False(func2(Request.CreateValues("A", 1, "read"), Policy.CreateValues("A", 2, "read")));
//Assert.False(func2(Request.CreateValues("B", 1, "read"), Policy.CreateValues("B", 2, "read")));
Assert.True(1==1);
Assert.True(func2(Request.CreateValues("A", 1, "read"), Policy.CreateValues("A", 1, "read")));
Assert.False(func2(Request.CreateValues("A", 1, "read"), Policy.CreateValues("A", 2, "read")));
Assert.False(func2(Request.CreateValues("B", 1, "read"), Policy.CreateValues("B", 2, "read")));
}

private static Func<TRequest, TPolicy, bool> Compile<TRequest, TPolicy>
Expand Down
5 changes: 2 additions & 3 deletions Casbin/Abstractions/Model/IPolicyValues.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;

namespace Casbin.Model;

public interface IPolicyValues : IList<string>
public interface IPolicyValues : IReadOnlyList<string>
{
public new string this[int index] { get; }

Expand Down
3 changes: 1 addition & 2 deletions Casbin/EnforceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ public static string TransformMatcher(in EnforceView view, string matcher)
foreach (KeyValuePair<string, int> tokenPair in view.PolicyAssertion.Tokens)
{
Regex reg = new Regex(perfix + $@"{view.PolicyType}\.{tokenPair.Key}" + suffix);
//matcher = reg.Replace(matcher, $"{view.PolicyType}.Value{tokenPair.Value + 1}");
matcher = reg.Replace(matcher, $"{view.PolicyType}[{tokenPair.Value}]");
matcher = reg.Replace(matcher, $"{view.PolicyType}.Value{tokenPair.Value + 1}");
}

return matcher;
Expand Down
32 changes: 30 additions & 2 deletions Casbin/Enforcer.Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,36 @@ private bool InternalEnforce<TRequest>(in EnforceContext context, in TRequest re
return InternalEnforce<IRequestValues, IPolicyValues>(in context, requestValues);
}

return InternalEnforce<TRequest, PolicyValues>(in context, requestValues);

return context.View.PolicyTokens.Count switch
{
1 => InternalEnforce<TRequest, PolicyValues<string>>(in context, requestValues),
2 => InternalEnforce<TRequest, PolicyValues<string, string>>(in context, requestValues),
3 => InternalEnforce<TRequest, PolicyValues<string, string, string>>(in context, requestValues),
4 => InternalEnforce<TRequest, PolicyValues<string, string, string, string>>(in context,
requestValues),
5 => InternalEnforce<TRequest, PolicyValues<string, string, string, string, string>>(in context,
requestValues),
6 => InternalEnforce<TRequest, PolicyValues<string, string, string, string, string, string>>(
in context,
requestValues),
7 => InternalEnforce<TRequest,
PolicyValues<string, string, string, string, string, string, string>>(
in context, requestValues),
8 => InternalEnforce<TRequest,
PolicyValues<string, string, string, string, string, string, string, string>>(in context,
requestValues),
9 => InternalEnforce<TRequest,
PolicyValues<string, string, string, string, string, string, string, string, string>>(
in context,
requestValues),
10 => InternalEnforce<TRequest, PolicyValues<string, string, string, string, string, string, string,
string, string, string>>(in context, requestValues),
11 => InternalEnforce<TRequest, PolicyValues<string, string, string, string, string, string, string,
string, string, string, string>>(in context, requestValues),
12 => InternalEnforce<TRequest, PolicyValues<string, string, string, string, string, string, string,
string, string, string, string, string>>(in context, requestValues),
_ => InternalEnforce<IRequestValues, IPolicyValues>(in context, requestValues)
};
}

private bool InternalEnforce<TRequest, TPolicy>(in EnforceContext context, in TRequest requestValues)
Expand Down
14 changes: 4 additions & 10 deletions Casbin/Model/DefaultPolicyStore.Node.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace Casbin.Model;
Expand Down Expand Up @@ -45,24 +44,19 @@ public IReadOnlyList<IPolicyValues> SetPolicy(List<IPolicyValues> valuesList) =>
public bool ContainsPolicy(IPolicyValues values)
=> PolicyTextSet.Contains(values.ToText());

public bool ValidatePolicy(ref IPolicyValues values)
public bool ValidatePolicy(IPolicyValues values)
{
if (_assertion.Section is PermConstants.Section.RoleSection)
{
return _assertion.Tokens.Count <= values.Count;
}

while(_assertion.Tokens.Count > values.Count)
{
values.Add("");
}

return _assertion.Tokens.Count >= values.Count;
return _assertion.Tokens.Count == values.Count;
}

public bool TryAddPolicy(IPolicyValues values)
{
if (ValidatePolicy(ref values) is false)
if (ValidatePolicy(values) is false)
{
return false;
}
Expand Down Expand Up @@ -93,7 +87,7 @@ public bool TryAddPolicy(IPolicyValues values)

public bool TryUpdatePolicy(IPolicyValues oldValues, IPolicyValues newValues)
{
if (ValidatePolicy(ref newValues) is false)
if (ValidatePolicy(newValues) is false)
{
return false;
}
Expand Down
Loading

0 comments on commit 8f647b5

Please sign in to comment.