Skip to content

Commit

Permalink
feat: keep compatible with unexpected policy, revert PR: #337 (#350)
Browse files Browse the repository at this point in the history
* Revert "fix: fix bugs in policy equal and persist (#347)"

This reverts commit 3306375.

* Revert "feat: fix null value handling logic (#337)"

This reverts commit 54db948.

* feat: compatible with unexpected policy

Signed-off-by: sagilio <[email protected]>

---------

Signed-off-by: sagilio <[email protected]>
Co-authored-by: sagilio <[email protected]>
  • Loading branch information
sagilio0728 and sagilio committed Apr 22, 2024
1 parent 9cecadd commit 442a59d
Show file tree
Hide file tree
Showing 21 changed files with 1,193 additions and 694 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;
using Casbin.Model;
using Xunit;

namespace Casbin.UnitTests.Extensions;

internal static class ManagementEnforcerExtension
{
internal static void TestGetPolicy(this IEnforcer e, IReadOnlyList<IPolicyValues> exceptedValues)
{
IEnumerable<IEnumerable<string>> actualValues = e.GetPolicy();
Assert.True(exceptedValues.DeepEquals(actualValues));
}
}
18 changes: 18 additions & 0 deletions Casbin.UnitTests/Extensions/Model/RequestValuesExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using Casbin.Model;

namespace Casbin.UnitTests.Extensions;

internal static class RequestValuesExtension
{
internal static IEnumerable<string> ToEnumerable(this IRequestValues values)
{
string[] res = new string[values.Count];
for (int i = 0; i < values.Count; i++)
{
res[i] = values[i];
}

return res;
}
}
18 changes: 0 additions & 18 deletions Casbin.UnitTests/Extensions/RequestValuesExtension.cs

This file was deleted.

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
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
namespace Casbin.UnitTests.GenericTests;

[Collection("Model collection")]
public class SupportCountTests
public class SupportCountTest
{
private readonly TestModelFixture _testModelFixture;

public SupportCountTests(TestModelFixture testModelFixture) => _testModelFixture = testModelFixture;
public SupportCountTest(TestModelFixture testModelFixture) => _testModelFixture = testModelFixture;

[Fact]
public void TestSupportCount()
Expand Down Expand Up @@ -59,60 +59,74 @@ private static void TestEnforce(IEnforcer enforcer, EnforceContext context, int
case 5:
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3",
"value4", "value5"));
Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3",
"value4", "value5" }));
Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3", "value4", "value5" }));
break;
case 6:
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3",
"value4", "value5", "value6"));
Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3",
"value4", "value5", "value6" }));
Assert.True(enforcer.Enforce(context,
new[] { "value1", "value2", "value3", "value4", "value5", "value6" }));
break;
case 7:
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3",
"value4", "value5", "value6", "value7"));
Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3",
"value4", "value5", "value6", "value7" }));
Assert.True(enforcer.Enforce(context,
new[] { "value1", "value2", "value3", "value4", "value5", "value6", "value7" }));
break;
case 8:
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3",
"value4", "value5", "value6", "value7", "value8"));
Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3",
"value4", "value5", "value6", "value7", "value8" }));
Assert.True(enforcer.Enforce(context,
new[] { "value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8" }));
break;
case 9:
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3",
"value4", "value5", "value6", "value7", "value8", "value9"));
Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3",
"value4", "value5", "value6", "value7", "value8", "value9" }));
Assert.True(enforcer.Enforce(context,
new[]
{
"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9"
}));
break;
case 10:
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3",
"value4", "value5", "value6", "value7", "value8", "value9",
"value10"));
Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3",
"value4", "value5", "value6", "value7", "value8", "value9", "value10" }));
Assert.True(enforcer.Enforce(context,
new[]
{
"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9",
"value10"
}));
break;
case 11:
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3",
"value4", "value5", "value6", "value7", "value8", "value9",
"value10", "value11"));
Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3",
"value4", "value5", "value6", "value7", "value8", "value9", "value10", "value11" }));
Assert.True(enforcer.Enforce(context,
new[]
{
"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9",
"value10", "value11"
}));
break;
case 12:
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3",
"value4", "value5", "value6", "value7", "value8", "value9",
"value10", "value11", "value12"));
Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3",
"value4", "value5", "value6", "value7", "value8", "value9", "value10", "value11", "value12" }));
Assert.True(enforcer.Enforce(context,
new[]
{
"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9",
"value10", "value11", "value12"
}));
break;
case 13:
Assert.True(enforcer.Enforce(context, Request.CreateValues("value1", "value2", "value3",
"value4", "value5", "value6", "value7", "value8", "value9",
"value10", "value11", "value12", "value13")));
Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3",
"value4", "value5", "value6", "value7", "value8", "value9", "value10", "value11", "value12", "value13" }));
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3", "value4", "value5", "value6",
"value7", "value8", "value9", "value10", "value11", "value12", "value13"));
break;
default:
throw new ArgumentOutOfRangeException(nameof(requestCount));
Expand Down
35 changes: 35 additions & 0 deletions Casbin.UnitTests/ModelTests/ManagementApiTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Casbin.Model;
using Casbin.UnitTests.Extensions;
using Casbin.UnitTests.Fixtures;
using Xunit;
using static Casbin.UnitTests.Util.TestUtil;
Expand Down Expand Up @@ -551,4 +553,37 @@ public async Task TestModifyGroupingPolicyAsync()
TestGetRoles(e, "admin", AsList("data5_admin"));
TestGetRoles(e, "eve", AsList("admin_groups"));
}

[Fact]
public void TestModifySpecialPolicy()
{
Enforcer e = new(TestModelFixture.GetNewTestModel(_testModelFixture._rbacModelText));

e.AddPolicy("alice", "data1");
e.AddPolicy("alice", "data1", "read");
e.AddPolicy("alice", "data1", "read", "dump1");

e.TestGetPolicy(Policy.ValuesListFrom(new[]
{
Policy.CreateValues("alice", "data1", ""), Policy.CreateValues("alice", "data1", "read"),
}
));
}

[Fact]
public async Task TestModifySpecialPolicyAsync()
{
Enforcer e = new(_testModelFixture.GetNewRbacTestModel());
e.ClearPolicy();

await e.AddPolicyAsync("alice", "data1");
await e.AddPolicyAsync("alice", "data1", "read");
await e.AddPolicyAsync("alice", "data1", "read", "dump1");

e.TestGetPolicy(Policy.ValuesListFrom(new[]
{
Policy.CreateValues("alice", "data1", ""), Policy.CreateValues("alice", "data1", "read"),
}
));
}
}
4 changes: 2 additions & 2 deletions Casbin.UnitTests/Util/TestUtil.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Casbin.Rbac;
using Casbin.Model;
using Casbin.Rbac;
using Casbin.Util;
using Xunit;

namespace Casbin.UnitTests.Util;

public static class TestUtil
internal static class TestUtil
{
internal static List<T> AsList<T>(params T[] values) => values.ToList();

Expand Down
7 changes: 2 additions & 5 deletions Casbin/Abstractions/Model/IPolicyValues.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
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; }

public string ToText();

public bool Equals(IPolicyValues other);

int GetRealCount();
}
6 changes: 6 additions & 0 deletions Casbin/Abstractions/Model/IReadOnlyPolicyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ public interface IReadOnlyPolicyStore

public bool ContainsNode(string section, string policyType);

public int GetRequiredValuesCount(string section, string policyType);

public bool ValidatePolicy(string section, string policyType, IPolicyValues values);

public bool ValidatePolicies(string section, string policyType, IReadOnlyList<IPolicyValues> valuesList);

public PolicyScanner Scan(string section, string policyType);

public IEnumerable<IPolicyValues> GetPolicy(string section, string policyType);
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
Loading

0 comments on commit 442a59d

Please sign in to comment.