Skip to content

Commit

Permalink
feat: update Casbin.NET to v2.7.0 and keep compatible with unexpected…
Browse files Browse the repository at this point in the history
… policy

Signed-off-by: sagilio <sagilio@outlook.com>
  • Loading branch information
sagilio committed May 20, 2024
1 parent 85e4fdb commit efaccc6
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net8.0;net7.0;net6.0;net5.0;netcoreapp3.1;</TargetFrameworks>
<IsPackable>false</IsPackable>
<LangVersion>11</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
106 changes: 87 additions & 19 deletions Casbin.Persist.Adapter.EFCore.UnitTest/SpecialPolicyTest.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using Casbin.Persist.Adapter.EFCore.UnitTest.Extensions;
using Casbin.Model;
using Casbin.Persist.Adapter.EFCore.Entities;
using Casbin.Persist.Adapter.EFCore.UnitTest.Fixtures;
using Xunit;

namespace Casbin.Persist.Adapter.EFCore.UnitTest
{
public class SpecialPolicyTest : IClassFixture<ModelProvideFixture>, IClassFixture<DbContextProviderFixture>
public class SpecialPolicyTest : TestUtil, IClassFixture<ModelProvideFixture>,
IClassFixture<DbContextProviderFixture>
{
private readonly ModelProvideFixture _modelProvideFixture;
private readonly DbContextProviderFixture _dbContextProviderFixture;

public SpecialPolicyTest(ModelProvideFixture modelProvideFixture, DbContextProviderFixture dbContextProviderFixture)
public SpecialPolicyTest(ModelProvideFixture modelProvideFixture,
DbContextProviderFixture dbContextProviderFixture)
{
_modelProvideFixture = modelProvideFixture;
_dbContextProviderFixture = dbContextProviderFixture;
Expand All @@ -22,33 +25,98 @@ public void TestCommaPolicy()
var context = _dbContextProviderFixture.GetContext<int>("CommaPolicy");
context.Clear();
var adapter = new EFCoreAdapter<int>(context);
var enforcer = new Enforcer(DefaultModel.CreateFromText(@"
[request_definition]
r = _
var enforcer = new Enforcer(DefaultModel.CreateFromText(
"""
[request_definition]
r = _
[policy_definition]
p = rule, a1, a2
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = eval(p.rule)
"""
), adapter);
enforcer.AddFunction("equal", (a1, a2) => a1 == a2);

[policy_definition]
p = rule, a1, a2
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = eval(p.rule)
"), adapter);
enforcer.AddFunction("equal", (a1 , a2) => a1 == a2);

enforcer.AddPolicy("equal(p.a1, p.a2)", "a1", "a1");
Assert.True(enforcer.Enforce("_"));

enforcer.LoadPolicy();
Assert.True(enforcer.Enforce("_"));

enforcer.RemovePolicy("equal(p.a1, p.a2)", "a1", "a1");
enforcer.AddPolicy("equal(p.a1, p.a2)", "a1", "a2");
Assert.False(enforcer.Enforce("_"));

enforcer.LoadPolicy();
Assert.False(enforcer.Enforce("_"));
}

[Fact]
public void TestUnexpectedPolicy()
{
var context = _dbContextProviderFixture.GetContext<int>("UnexpectedPolicy");
context.Clear();
context.Policies.Add(new EFCorePersistPolicy<int>()
{
Type = "p",
Value1 = "a1",
Value2 = "a2",
Value3 = null,
});
context.Policies.Add(new EFCorePersistPolicy<int>()
{
Type = "p",
Value1 = "a1",
Value2 = "a2",
Value3 = "a3",
});
context.Policies.Add(new EFCorePersistPolicy<int>()
{
Type = "p",
Value1 = "a1",
Value2 = "a2",
Value3 = "a3",
Value4 = "a4",
});
context.Policies.Add(new EFCorePersistPolicy<int>()
{
Type = "p",
Value1 = "b1",
Value2 = "b2",
Value3 = "b3",
Value4 = "b4",
});
context.SaveChanges();

var adapter = new EFCoreAdapter<int>(context);
var enforcer = new Enforcer(DefaultModel.CreateFromText(
"""
[request_definition]
r = _
[policy_definition]
p = a1, a2, a3
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = true
"""), adapter);

enforcer.LoadPolicy();
var policies = enforcer.GetPolicy();

TestGetPolicy(enforcer, AsList(
AsList("a1", "a2", ""),
AsList("a1", "a2", "a3"),
AsList("b1", "b2", "b3")
));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Casbin.NET" Version="2.5.1" />
<PackageReference Include="Casbin.NET" Version="2.7.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ internal static void LoadPolicyFromPersistPolicy<TPersistPolicy>(this IPolicySto
{
policy.Section = policy.Type.Substring(0, 1);
}
store.AddPolicy(policy.Section, policy.Type, Policy.ValuesFrom(policy));
var requiredCount = store.GetRequiredValuesCount(policy.Section, policy.Type);
var values = Policy.ValuesFrom(policy, requiredCount);
store.AddPolicy(policy.Section, policy.Type, values);
}
}

Expand Down

0 comments on commit efaccc6

Please sign in to comment.