From b498d7af469f194d2078068253b9d9b9b8d5b89d Mon Sep 17 00:00:00 2001 From: sagilio Date: Mon, 21 Nov 2022 14:43:51 +0800 Subject: [PATCH] fix: err filter logic Signed-off-by: sagilio --- Casbin/EnforceOptions.cs | 4 ++- Casbin/Extensions/Model/ModelExtension.cs | 44 +++++++++++++++++++++-- Casbin/Persist/Filter.cs | 5 +-- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/Casbin/EnforceOptions.cs b/Casbin/EnforceOptions.cs index 87af43c..61721d3 100644 --- a/Casbin/EnforceOptions.cs +++ b/Casbin/EnforceOptions.cs @@ -10,7 +10,9 @@ public class EnforcerOptions public bool AutoNotifyWatcher { get; set; } = true; public bool AutoCleanEnforceCache { get; set; } = true; public bool AutoLoadPolicy { get; set; } = true; - public IPolicyFilter AutoLoadPolicyFilter { get; set; } = new Filter(); + public IPolicyFilter AutoLoadPolicyFilter { get; set; } = null; } + + diff --git a/Casbin/Extensions/Model/ModelExtension.cs b/Casbin/Extensions/Model/ModelExtension.cs index d0e8b59..826e5c1 100644 --- a/Casbin/Extensions/Model/ModelExtension.cs +++ b/Casbin/Extensions/Model/ModelExtension.cs @@ -94,24 +94,64 @@ public static async Task LoadPolicyAsync(this IModel model) public static bool LoadFilteredPolicy(this IModel model, IPolicyFilter filter) { + if (model.AdapterHolder.Adapter is null) + { + return false; + } + if (model.AdapterHolder.FilteredAdapter is null) { return false; } - model.AdapterHolder.FilteredAdapter.LoadFilteredPolicy(model.PolicyStoreHolder.PolicyStore, filter); + DefaultPolicyStore policyStore = new(); + foreach (KeyValuePair pair in model.Sections.GetPolicyAssertions()) + { + policyStore.AddNode(PermConstants.Section.PolicySection, pair.Key, pair.Value); + } + + if (model.Sections.ContainsSection(PermConstants.Section.RoleSection)) + { + foreach (KeyValuePair pair in model.Sections.GetRoleAssertions()) + { + policyStore.AddNode(PermConstants.Section.RoleSection, pair.Key, pair.Value); + } + } + + model.AdapterHolder.FilteredAdapter.LoadFilteredPolicy(policyStore, filter); + model.PolicyStoreHolder.PolicyStore = policyStore; return true; } public static async Task LoadFilteredPolicyAsync(this IModel model, IPolicyFilter filter) { + if (model.AdapterHolder.Adapter is null) + { + return false; + } + if (model.AdapterHolder.FilteredAdapter is null) { return false; } - await model.AdapterHolder.FilteredAdapter.LoadFilteredPolicyAsync(model.PolicyStoreHolder.PolicyStore, + DefaultPolicyStore policyStore = new(); + foreach (KeyValuePair pair in model.Sections.GetPolicyAssertions()) + { + policyStore.AddNode(PermConstants.Section.PolicySection, pair.Key, pair.Value); + } + + if (model.Sections.ContainsSection(PermConstants.Section.RoleSection)) + { + foreach (KeyValuePair pair in model.Sections.GetRoleAssertions()) + { + policyStore.AddNode(PermConstants.Section.RoleSection, pair.Key, pair.Value); + } + } + + await model.AdapterHolder.FilteredAdapter.LoadFilteredPolicyAsync(policyStore, filter); + model.PolicyStoreHolder.PolicyStore = policyStore; return true; } diff --git a/Casbin/Persist/Filter.cs b/Casbin/Persist/Filter.cs index 15845b9..b45dee8 100644 --- a/Casbin/Persist/Filter.cs +++ b/Casbin/Persist/Filter.cs @@ -22,7 +22,7 @@ public IEnumerable P { _p = value; _filterP = new PolicyFilter(PermConstants.DefaultPolicyType, 0, - Policy.CreateValues(value)); + Policy.ValuesFrom(value)); } } @@ -33,7 +33,7 @@ public IEnumerable G { _g = value; _filterG = new PolicyFilter(PermConstants.DefaultGroupingPolicyType, 0, - Policy.CreateValues(value)); + Policy.ValuesFrom(value)); } } @@ -69,3 +69,4 @@ public IQueryable Apply(IQueryable policies) where T : IPersistPolicy +