diff --git a/Casbin.Persist.Adapter.EFCore.UnitTest/Casbin.Persist.Adapter.EFCore.UnitTest.csproj b/Casbin.Persist.Adapter.EFCore.UnitTest/Casbin.Persist.Adapter.EFCore.UnitTest.csproj index 36ef063..c158dbf 100644 --- a/Casbin.Persist.Adapter.EFCore.UnitTest/Casbin.Persist.Adapter.EFCore.UnitTest.csproj +++ b/Casbin.Persist.Adapter.EFCore.UnitTest/Casbin.Persist.Adapter.EFCore.UnitTest.csproj @@ -3,6 +3,7 @@ net8.0;net7.0;net6.0;net5.0;netcoreapp3.1; false + 11 diff --git a/Casbin.Persist.Adapter.EFCore.UnitTest/SpecialPolicyTest.cs b/Casbin.Persist.Adapter.EFCore.UnitTest/SpecialPolicyTest.cs index be0993f..a9ae7b7 100644 --- a/Casbin.Persist.Adapter.EFCore.UnitTest/SpecialPolicyTest.cs +++ b/Casbin.Persist.Adapter.EFCore.UnitTest/SpecialPolicyTest.cs @@ -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, IClassFixture + public class SpecialPolicyTest : TestUtil, IClassFixture, + IClassFixture { private readonly ModelProvideFixture _modelProvideFixture; private readonly DbContextProviderFixture _dbContextProviderFixture; - public SpecialPolicyTest(ModelProvideFixture modelProvideFixture, DbContextProviderFixture dbContextProviderFixture) + public SpecialPolicyTest(ModelProvideFixture modelProvideFixture, + DbContextProviderFixture dbContextProviderFixture) { _modelProvideFixture = modelProvideFixture; _dbContextProviderFixture = dbContextProviderFixture; @@ -22,33 +25,98 @@ public void TestCommaPolicy() var context = _dbContextProviderFixture.GetContext("CommaPolicy"); context.Clear(); var adapter = new EFCoreAdapter(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("UnexpectedPolicy"); + context.Clear(); + context.Policies.Add(new EFCorePersistPolicy() + { + Type = "p", + Value1 = "a1", + Value2 = "a2", + Value3 = null, + }); + context.Policies.Add(new EFCorePersistPolicy() + { + Type = "p", + Value1 = "a1", + Value2 = "a2", + Value3 = "a3", + }); + context.Policies.Add(new EFCorePersistPolicy() + { + Type = "p", + Value1 = "a1", + Value2 = "a2", + Value3 = "a3", + Value4 = "a4", + }); + context.Policies.Add(new EFCorePersistPolicy() + { + Type = "p", + Value1 = "b1", + Value2 = "b2", + Value3 = "b3", + Value4 = "b4", + }); + context.SaveChanges(); + + var adapter = new EFCoreAdapter(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") + )); + } } } \ No newline at end of file diff --git a/Casbin.Persist.Adapter.EFCore/Casbin.Persist.Adapter.EFCore.csproj b/Casbin.Persist.Adapter.EFCore/Casbin.Persist.Adapter.EFCore.csproj index 1a26cb5..e9971db 100644 --- a/Casbin.Persist.Adapter.EFCore/Casbin.Persist.Adapter.EFCore.csproj +++ b/Casbin.Persist.Adapter.EFCore/Casbin.Persist.Adapter.EFCore.csproj @@ -17,7 +17,7 @@ - + diff --git a/Casbin.Persist.Adapter.EFCore/Extensions/PolicyStoreExtension.cs b/Casbin.Persist.Adapter.EFCore/Extensions/PolicyStoreExtension.cs index 5d6b39d..2a8d406 100644 --- a/Casbin.Persist.Adapter.EFCore/Extensions/PolicyStoreExtension.cs +++ b/Casbin.Persist.Adapter.EFCore/Extensions/PolicyStoreExtension.cs @@ -14,7 +14,9 @@ internal static void LoadPolicyFromPersistPolicy(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); } }