From 820900f8393df90db6d63504a4ff9d9defb5ace8 Mon Sep 17 00:00:00 2001 From: sagilio Date: Mon, 6 Jun 2022 13:07:39 +0800 Subject: [PATCH] fix: error has adapter value Signed-off-by: sagilio --- Casbin/Abstractions/Model/IPolicyManager.cs | 8 +- .../Extensions/Enforcer/EnforcerExtension.cs | 132 ++++++++++-------- Casbin/Model/DefaultPolicyManager.cs | 116 +++++++++------ Casbin/Model/ReaderWriterPolicyManager.cs | 20 ++- 4 files changed, 171 insertions(+), 105 deletions(-) diff --git a/Casbin/Abstractions/Model/IPolicyManager.cs b/Casbin/Abstractions/Model/IPolicyManager.cs index 3fc23a8f..3c9fdf4a 100644 --- a/Casbin/Abstractions/Model/IPolicyManager.cs +++ b/Casbin/Abstractions/Model/IPolicyManager.cs @@ -12,6 +12,8 @@ public interface IPolicyManager : IPolicyStore public bool HasAdapter { get; } + public bool IsFiltered { get; } + public bool AutoSave { get; set; } public IPolicyStore PolicyStore { get; set; } @@ -46,8 +48,10 @@ public interface IPolicyManager : IPolicyStore public Task RemovePolicyAsync(string section, string policyType, IEnumerable rule); - public Task RemovePoliciesAsync(string section, string policyType, IEnumerable> rules); + public Task RemovePoliciesAsync(string section, string policyType, + IEnumerable> rules); - public Task>> RemoveFilteredPolicyAsync(string section, string policyType, int fieldIndex, params string[] fieldValues); + public Task>> RemoveFilteredPolicyAsync(string section, string policyType, + int fieldIndex, params string[] fieldValues); } } diff --git a/Casbin/Extensions/Enforcer/EnforcerExtension.cs b/Casbin/Extensions/Enforcer/EnforcerExtension.cs index c2ae43d6..72258964 100644 --- a/Casbin/Extensions/Enforcer/EnforcerExtension.cs +++ b/Casbin/Extensions/Enforcer/EnforcerExtension.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Runtime.InteropServices.ComTypes; using System.Threading.Tasks; using Casbin.Caching; using Casbin.Effect; @@ -17,7 +13,27 @@ namespace Casbin { public static partial class EnforcerExtension { + #region Model management + + /// + /// LoadModel reloads the model from the model CONF file. Because the policy is + /// Attached to a model, so the policy is invalidated and needs to be reloaded by + /// calling LoadPolicy(). + /// + public static void LoadModel(this IEnforcer enforcer) + { + if (enforcer.ModelPath is null) + { + return; + } + + enforcer.Model = DefaultModel.CreateFromFile(enforcer.ModelPath); + } + + #endregion + #region Set options + /// /// Changes the enforcing state of Casbin, when Casbin is disabled, /// all access will be allowed by the enforce() function. @@ -77,9 +93,11 @@ public static IEnforcer EnableAutoCleanEnforceCache(this IEnforcer enforcer, boo enforcer.AutoCleanEnforceCache = autoCleanEnforceCache; return enforcer; } + #endregion #region Set extensions + /// /// Sets the current effector. /// @@ -114,6 +132,7 @@ public static IEnforcer SetModel(this IEnforcer enforcer, IModel model) { model = model.ToSyncModel(); } + enforcer.Model = model; if (enforcer.AutoCleanEnforceCache) { @@ -122,6 +141,7 @@ public static IEnforcer SetModel(this IEnforcer enforcer, IModel model) enforcer.Logger?.LogInformation("Enforcer Cache, Cleared all enforce cache"); #endif } + return enforcer; } @@ -150,6 +170,7 @@ public static IEnforcer SetWatcher(this IEnforcer enforcer, IWatcher watcher, bo watcher?.SetUpdateCallback(enforcer.LoadPolicyAsync); return enforcer; } + watcher?.SetUpdateCallback(() => enforcer.LoadPolicy()); return enforcer; } @@ -179,10 +200,12 @@ public static IEnforcer SetRoleManager(this IEnforcer enforcer, string roleType, { enforcer.Model.BuildRoleLinks(); } + if (enforcer.AutoCleanEnforceCache) { enforcer.EnforceCache?.Clear(); } + return enforcer; } @@ -196,47 +219,29 @@ public static IEnforcer SetEnforceCache(this IEnforcer enforcer, IEnforceCache e enforcer.EnforceCache = enforceCache; return enforcer; } - #endregion - #region Model management - /// - /// LoadModel reloads the model from the model CONF file. Because the policy is - /// Attached to a model, so the policy is invalidated and needs to be reloaded by - /// calling LoadPolicy(). - /// - public static void LoadModel(this IEnforcer enforcer) - { - if (enforcer.ModelPath is null) - { - return; - } - enforcer.Model = DefaultModel.CreateFromFile(enforcer.ModelPath); - } #endregion #region Poilcy management + /// /// Reloads the policy from file/database. /// public static bool LoadPolicy(this IEnforcer enforcer) { - if (enforcer.Adapter is null) - { - return false; - } - - enforcer.ClearPolicy(); bool result = enforcer.PolicyManager.LoadPolicy(); if (result is false) { return false; } + enforcer.ClearCache(); enforcer.Model.RefreshPolicyStringSet(); if (enforcer.AutoBuildRoleLinks) { enforcer.BuildRoleLinks(); } + return true; } @@ -245,24 +250,19 @@ public static bool LoadPolicy(this IEnforcer enforcer) /// public static async Task LoadPolicyAsync(this IEnforcer enforcer) { - if (enforcer.Adapter is null) - { - return false; - } - - enforcer.ClearPolicy(); bool result = await enforcer.PolicyManager.LoadPolicyAsync(); - Debug.WriteLine("vvv"); if (result is false) { return false; } + enforcer.ClearCache(); enforcer.Model.RefreshPolicyStringSet(); if (enforcer.AutoBuildRoleLinks) { enforcer.BuildRoleLinks(); } + return true; } @@ -281,11 +281,17 @@ public static bool LoadFilteredPolicy(this IEnforcer enforcer, Filter filter) enforcer.ClearPolicy(); bool result = enforcer.PolicyManager.LoadFilteredPolicy(filter); - if (result && enforcer.AutoBuildRoleLinks) + if (result is false) + { + return false; + } + + if (enforcer.AutoBuildRoleLinks) { enforcer.BuildRoleLinks(); } - return result; + + return true; } /// @@ -303,11 +309,17 @@ public static async Task LoadFilteredPolicyAsync(this IEnforcer enforcer, enforcer.ClearPolicy(); bool result = await enforcer.PolicyManager.LoadFilteredPolicyAsync(filter); - if (result && enforcer.AutoBuildRoleLinks) + if (result is false) + { + return false; + } + + if (enforcer.AutoBuildRoleLinks) { enforcer.BuildRoleLinks(); } - return result; + + return true; } /// @@ -316,26 +328,12 @@ public static async Task LoadFilteredPolicyAsync(this IEnforcer enforcer, /// public static bool SavePolicy(this IEnforcer enforcer) { - if (enforcer.Adapter is null) - { - return false; - } - - if (enforcer.Adapter is not IEpochAdapter adapter) - { - throw new InvalidOperationException("Cannot save policy when use a readonly adapter"); - } - - if (enforcer.IsFiltered) - { - throw new InvalidOperationException("Cannot save a filtered policy"); - } - bool result = enforcer.PolicyManager.SavePolicy(); if (result is false) { return false; } + enforcer.Watcher?.Update(); return true; } @@ -362,11 +360,17 @@ public static async Task SavePolicyAsync(this IEnforcer enforcer) } bool result = await enforcer.PolicyManager.SavePolicyAsync(); - if (result && enforcer.Watcher is not null) + if (result is false) + { + return false; + } + + if (enforcer.Watcher is not null) { await enforcer.Watcher.UpdateAsync(); } - return result; + + return true; } /// @@ -375,6 +379,11 @@ public static async Task SavePolicyAsync(this IEnforcer enforcer) public static void ClearPolicy(this IEnforcer enforcer) { enforcer.Model.ClearPolicy(); + enforcer.ClearCache(); + } + + public static void ClearCache(this IEnforcer enforcer) + { if (enforcer.AutoCleanEnforceCache) { enforcer.EnforceCache?.Clear(); @@ -386,9 +395,11 @@ public static void ClearPolicy(this IEnforcer enforcer) enforcer.Logger?.LogInformation("Store Management, Cleared all policy"); #endif } + #endregion #region Role management + /// /// Manually rebuilds the role inheritance relations. /// @@ -409,20 +420,24 @@ public static Enforcer AddDomainMatchingFunc(this Enforcer enforcer, Func func) + public static Enforcer AddNamedMatchingFunc(this Enforcer enforcer, string roleType, + Func func) { enforcer.Model.GetRoleManger(roleType).AddMatchingFunc(func); return enforcer; } - public static Enforcer AddNamedDomainMatchingFunc(this Enforcer enforcer, string roleType, Func func) + public static Enforcer AddNamedDomainMatchingFunc(this Enforcer enforcer, string roleType, + Func func) { enforcer.Model.GetRoleManger(roleType).AddMatchingFunc(func); return enforcer; } + #endregion #region Enforce Cotext + public static EnforceContext CreateContext(this IEnforcer enforcer, bool explain) { return EnforceContext.Create(enforcer, explain); @@ -452,6 +467,7 @@ public static EnforceContext CreateContextWithMatcher(this IEnforcer enforcer, { return EnforceContext.CreateWithMatcher(enforcer, matcher, requestType, policyType, effectType, explain); } + #endregion #region Enforce extensions @@ -459,13 +475,15 @@ public static EnforceContext CreateContextWithMatcher(this IEnforcer enforcer, #endregion #if !NET452 - internal static void LogEnforceCachedResult(this IEnforcer enforcer, TRequest requestValues, bool finalResult) + internal static void LogEnforceCachedResult(this IEnforcer enforcer, TRequest requestValues, + bool finalResult) where TRequest : IRequestValues { enforcer.Logger?.LogEnforceCachedResult(requestValues, finalResult); } - internal static void LogEnforceResult(this IEnforcer enforcer, in EnforceContext context, TRequest requestValues, bool finalResult) + internal static void LogEnforceResult(this IEnforcer enforcer, in EnforceContext context, + TRequest requestValues, bool finalResult) where TRequest : IRequestValues { if (context.Explain) diff --git a/Casbin/Model/DefaultPolicyManager.cs b/Casbin/Model/DefaultPolicyManager.cs index c7da7bbc..ee9e259f 100644 --- a/Casbin/Model/DefaultPolicyManager.cs +++ b/Casbin/Model/DefaultPolicyManager.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Casbin.Persist; @@ -9,10 +8,10 @@ namespace Casbin.Model { public class DefaultPolicyManager : IPolicyManager { - protected ISingleAdapter SingleAdapter; protected IBatchAdapter BatchAdapter; protected IEpochAdapter EpochAdapter; protected IFilteredAdapter FilteredAdapter; + protected ISingleAdapter SingleAdapter; // ReSharper disable once MemberCanBeProtected.Global public DefaultPolicyManager(IPolicyStore policyStore, IReadOnlyAdapter adapter = null) @@ -25,9 +24,10 @@ public DefaultPolicyManager(IPolicyStore policyStore, IReadOnlyAdapter adapter = } public virtual bool IsSynchronized => false; - public bool HasAdapter => Adapter is null; + public bool HasAdapter => Adapter is not null; + public bool IsFiltered => FilteredAdapter is not null && FilteredAdapter.IsFiltered; public Dictionary> Sections => PolicyStore.Sections; - public bool AutoSave { get; set; } + public bool AutoSave { get; set; } = true; public IPolicyStore PolicyStore { get; set; } public IReadOnlyAdapter Adapter @@ -42,11 +42,6 @@ public IReadOnlyAdapter Adapter } } - public static IPolicyManager Create() - { - return new DefaultPolicyManager(DefaultPolicyStore.Create()); - } - public virtual void StartRead() { } @@ -82,12 +77,19 @@ public bool LoadPolicy() try { - if (EpochAdapter is not null) + if (HasAdapter is false) { - EpochAdapter.LoadPolicy(PolicyStore); - return true; + return false; } - return false; + + if (EpochAdapter is null) + { + return false; + } + + PolicyStore.ClearPolicy(); + EpochAdapter.LoadPolicy(PolicyStore); + return true; } finally { @@ -104,12 +106,13 @@ public bool LoadFilteredPolicy(Filter filter) try { - if (FilteredAdapter is not null) + if (FilteredAdapter is null) { - FilteredAdapter.LoadFilteredPolicy(PolicyStore, filter); - return true; + return false; } - return false; + + FilteredAdapter.LoadFilteredPolicy(PolicyStore, filter); + return true; } finally { @@ -126,12 +129,23 @@ public bool SavePolicy() try { - if (EpochAdapter is not null) + if (HasAdapter is false) { - EpochAdapter.SavePolicy(PolicyStore); - return true; + return false; } - return false; + + if (EpochAdapter is null) + { + throw new InvalidOperationException("Cannot save policy when use a readonly adapter"); + } + + if (IsFiltered) + { + throw new InvalidOperationException("Cannot save filtered policies"); + } + + EpochAdapter.SavePolicy(PolicyStore); + return true; } finally { @@ -148,18 +162,28 @@ public virtual async Task LoadPolicyAsync() try { - if (EpochAdapter is not null) + if (HasAdapter is false) { - await EpochAdapter.LoadPolicyAsync(PolicyStore); - return true; + return false; } - return false; + + if (EpochAdapter is null) + { + return false; + } + + PolicyStore.ClearPolicy(); + await EpochAdapter.LoadPolicyAsync(PolicyStore); + return true; } finally { EndWrite(); - }; + } + + ; } + public virtual async Task LoadFilteredPolicyAsync(Filter filter) { if (TryStartWrite() is false) @@ -169,12 +193,13 @@ public virtual async Task LoadFilteredPolicyAsync(Filter filter) try { - if (FilteredAdapter is not null) + if (FilteredAdapter is null) { - await FilteredAdapter.LoadFilteredPolicyAsync(PolicyStore, filter); - return true; + return false; } - return false; + + await FilteredAdapter.LoadFilteredPolicyAsync(PolicyStore, filter); + return true; } finally { @@ -191,12 +216,13 @@ public virtual async Task SavePolicyAsync() try { - if (EpochAdapter is not null) + if (EpochAdapter is null) { - await EpochAdapter.SavePolicyAsync(PolicyStore); - return true; + return false; } - return false; + + await EpochAdapter.SavePolicyAsync(PolicyStore); + return true; } finally { @@ -256,7 +282,8 @@ public IEnumerable> GetPolicy(string section, string policyT } } - public IEnumerable> GetFilteredPolicy(string section, string policyType, int fieldIndex, params string[] fieldValues) + public IEnumerable> GetFilteredPolicy(string section, string policyType, int fieldIndex, + params string[] fieldValues) { if (TryStartRead() is false) { @@ -347,6 +374,7 @@ public bool AddPolicy(string section, string policyType, IEnumerable rul { return PolicyStore.AddPolicy(section, policyType, ruleArray); } + SingleAdapter?.AddPolicy(section, policyType, ruleArray); return PolicyStore.AddPolicy(section, policyType, ruleArray); } @@ -370,6 +398,7 @@ public bool AddPolicies(string section, string policyType, IEnumerable { return PolicyStore.RemovePolicy(section, policyType, ruleArray); } + SingleAdapter?.RemovePolicy(section, policyType, ruleArray); return PolicyStore.RemovePolicy(section, policyType, ruleArray); } @@ -416,6 +446,7 @@ public bool RemovePolicies(string section, string policyType, IEnumerable> RemoveFilteredPolicy(string section, string policyType, int fieldIndex, params string[] fieldValues) + public IEnumerable> RemoveFilteredPolicy(string section, string policyType, int fieldIndex, + params string[] fieldValues) { if (TryStartWrite() is false) { @@ -438,6 +470,7 @@ public IEnumerable> RemoveFilteredPolicy(string section, str { return PolicyStore.RemoveFilteredPolicy(section, policyType, fieldIndex, fieldValues); } + BatchAdapter?.RemoveFilteredPolicy(section, policyType, fieldIndex, fieldValues); return PolicyStore.RemoveFilteredPolicy(section, policyType, fieldIndex, fieldValues); } @@ -475,7 +508,8 @@ public virtual async Task AddPolicyAsync(string section, string policyType } } - public virtual async Task AddPoliciesAsync(string section, string policyType, IEnumerable> rules) + public virtual async Task AddPoliciesAsync(string section, string policyType, + IEnumerable> rules) { if (TryStartWrite() is false) { @@ -531,7 +565,8 @@ public virtual async Task RemovePolicyAsync(string section, string policyT } } - public virtual async Task RemovePoliciesAsync(string section, string policyType, IEnumerable> rules) + public virtual async Task RemovePoliciesAsync(string section, string policyType, + IEnumerable> rules) { if (TryStartWrite() is false) { @@ -559,7 +594,8 @@ public virtual async Task RemovePoliciesAsync(string section, string polic } } - public virtual async Task>> RemoveFilteredPolicyAsync(string section, string policyType, int fieldIndex, params string[] fieldValues) + public virtual async Task>> RemoveFilteredPolicyAsync(string section, + string policyType, int fieldIndex, params string[] fieldValues) { if (TryStartWrite() is false) { @@ -598,5 +634,7 @@ public void ClearPolicy() EndRead(); } } + + public static IPolicyManager Create() => new DefaultPolicyManager(DefaultPolicyStore.Create()); } } diff --git a/Casbin/Model/ReaderWriterPolicyManager.cs b/Casbin/Model/ReaderWriterPolicyManager.cs index 48d6c2a2..302d4642 100644 --- a/Casbin/Model/ReaderWriterPolicyManager.cs +++ b/Casbin/Model/ReaderWriterPolicyManager.cs @@ -8,8 +8,8 @@ namespace Casbin.Model { public class ReaderWriterPolicyManager : DefaultPolicyManager { - private readonly ReaderWriterPolicyManagerOptions _options; private readonly ReaderWriterLockSlim _lockSlim = new(); + private readonly ReaderWriterPolicyManagerOptions _options; // ReSharper disable once MemberCanBePrivate.Global public ReaderWriterPolicyManager(IPolicyStore policyStore, IReadOnlyAdapter adapter = null) @@ -25,13 +25,13 @@ public ReaderWriterPolicyManager(IPolicyStore policyStore, ReaderWriterPolicyMan _options = options; } + public override bool IsSynchronized => true; + public static new IPolicyManager Create() { return new ReaderWriterPolicyManager(DefaultPolicyStore.Create()); } - public override bool IsSynchronized => true; - public override void StartRead() { _lockSlim.EnterReadLock(); @@ -73,13 +73,19 @@ public override Task LoadPolicyAsync() try { - if (EpochAdapter is not null) + if (HasAdapter is false) { - EpochAdapter.LoadPolicyAsync(PolicyStore).Wait(); - return Task.FromResult(true); + return Task.FromResult(false); } - return Task.FromResult(false); + if (EpochAdapter is null) + { + return Task.FromResult(false); + } + + PolicyStore.ClearPolicy(); + EpochAdapter.LoadPolicyAsync(PolicyStore).Wait(); + return Task.FromResult(true); } finally {