From 394ff020f433bfd2dcd0d2ff5199a71dca739a41 Mon Sep 17 00:00:00 2001 From: AsakusaRinne Date: Sun, 18 Sep 2022 01:14:22 +0800 Subject: [PATCH] feat: change lazyLoadPolicy to AutoLoadPolicy. --- Casbin.UnitTests/ModelTests/EnforcerTest.cs | 4 +- Casbin/Abstractions/IEnforcer.cs | 13 +++++ Casbin/DefaultEnforcer.cs | 19 +++---- Casbin/Enforcer.cs | 52 ++++++++++++------- .../Extensions/Enforcer/EnforcerExtension.cs | 20 +++---- .../InternalEnforcerExtension.Events.cs | 6 +-- 6 files changed, 70 insertions(+), 44 deletions(-) diff --git a/Casbin.UnitTests/ModelTests/EnforcerTest.cs b/Casbin.UnitTests/ModelTests/EnforcerTest.cs index 1fdf9129..db9bfbf0 100644 --- a/Casbin.UnitTests/ModelTests/EnforcerTest.cs +++ b/Casbin.UnitTests/ModelTests/EnforcerTest.cs @@ -61,7 +61,7 @@ public void TestEnforceWithMultipleEval() } [Fact] - public void TestEnforceWithLazyLoadPolicy() + public void TestEnforceWithoutAutoLoadPolicy() { IModel m = DefaultModel.Create(); m.AddDef("r", "r", "sub, obj, act"); @@ -71,7 +71,7 @@ public void TestEnforceWithLazyLoadPolicy() FileAdapter a = new("examples/keymatch_policy.csv"); - IEnforcer e = DefaultEnforcer.Create(m, a, true); + IEnforcer e = DefaultEnforcer.Create(m, a, options => { options.AutoLoadPolicy = false; }); Assert.Empty(e.GetPolicy()); e = DefaultEnforcer.Create(m, a); diff --git a/Casbin/Abstractions/IEnforcer.cs b/Casbin/Abstractions/IEnforcer.cs index 4d2f8d52..a31edf36 100644 --- a/Casbin/Abstractions/IEnforcer.cs +++ b/Casbin/Abstractions/IEnforcer.cs @@ -22,6 +22,18 @@ namespace Casbin /// public interface IEnforcer { + public class EnforcerOptions + { + public bool Enabled { get; set; } = true; + public bool EnabledCache { get; set; } = true; + + public bool AutoBuildRoleLinks { get; set; } = true; + public bool AutoNotifyWatcher { get; set; } = true; + public bool AutoCleanEnforceCache { get; set; } = true; + public bool AutoLoadPolicy { get; set; } = true; + public Filter AutoLoadPolicyFilter { get; set; } = null; + } + /// /// Decides whether a "subject" can access a "object" with the operation /// "action", input parameters are usually: (sub, obj, act). @@ -76,6 +88,7 @@ public BatchEnforceAsyncResults BatchEnforceAsync(EnforceContext conte #region Options + public EnforcerOptions Options { get; set; } public bool Enabled { get; set; } public bool EnabledCache { get; set; } public bool AutoBuildRoleLinks { get; set; } diff --git a/Casbin/DefaultEnforcer.cs b/Casbin/DefaultEnforcer.cs index 1208a72a..cf633ba5 100644 --- a/Casbin/DefaultEnforcer.cs +++ b/Casbin/DefaultEnforcer.cs @@ -1,28 +1,29 @@ -using Casbin.Model; +using System; +using Casbin.Model; using Casbin.Persist; namespace Casbin { public static class DefaultEnforcer { - public static IEnforcer Create(IReadOnlyAdapter adapter = null, bool lazyLoadPolicy = false) + public static IEnforcer Create(IReadOnlyAdapter adapter = null, Action optionSettings = null) { - return new Enforcer(DefaultModel.Create(), adapter, lazyLoadPolicy); + return new Enforcer(DefaultModel.Create(), adapter, optionSettings); } - public static IEnforcer Create(string modelPath, string policyPath, bool lazyLoadPolicy = false) + public static IEnforcer Create(string modelPath, string policyPath, Action optionSettings = null) { - return new Enforcer(modelPath, policyPath, lazyLoadPolicy); + return new Enforcer(modelPath, policyPath, optionSettings); } - public static IEnforcer Create(string modelPath, IReadOnlyAdapter adapter = null, bool lazyLoadPolicy = false) + public static IEnforcer Create(string modelPath, IReadOnlyAdapter adapter = null, Action optionSettings = null) { - return new Enforcer(modelPath, adapter, lazyLoadPolicy); + return new Enforcer(modelPath, adapter, optionSettings); } - public static IEnforcer Create(IModel model, IReadOnlyAdapter adapter = null, bool lazyLoadPolicy = false) + public static IEnforcer Create(IModel model, IReadOnlyAdapter adapter = null, Action optionSettings = null) { - return new Enforcer(model, adapter, lazyLoadPolicy); + return new Enforcer(model, adapter, optionSettings); } } } diff --git a/Casbin/Enforcer.cs b/Casbin/Enforcer.cs index 96068379..54b92a5b 100644 --- a/Casbin/Enforcer.cs +++ b/Casbin/Enforcer.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading.Tasks; using Casbin.Adapter.File; using Casbin.Caching; @@ -17,27 +18,38 @@ public Enforcer() { } - public Enforcer(string modelPath, string policyPath, bool lazyLoadPolicy = false) - : this(modelPath, new FileAdapter(policyPath), lazyLoadPolicy) + public Enforcer(string modelPath, string policyPath, Action optionSettings = null) + : this(modelPath, new FileAdapter(policyPath), optionSettings) { } - public Enforcer(string modelPath, IReadOnlyAdapter adapter = null, bool lazyLoadPolicy = false) - : this(DefaultModel.CreateFromFile(modelPath), adapter, lazyLoadPolicy) + public Enforcer(string modelPath, IReadOnlyAdapter adapter = null, Action optionSettings = null) + : this(DefaultModel.CreateFromFile(modelPath), adapter, optionSettings) { } - public Enforcer(IModel model, IReadOnlyAdapter adapter = null, bool lazyLoadPolicy = false) + public Enforcer(IModel model, IReadOnlyAdapter adapter = null, Action optionSettings = null) { + if(optionSettings is not null) + { + optionSettings(Options); + } this.SetModel(model); if (adapter is not null) { this.SetAdapter(adapter); } - if (lazyLoadPolicy is false) + if (Options.AutoLoadPolicy is true) { - this.LoadPolicy(); + if(Adapter is IFilteredAdapter && Options.AutoLoadPolicyFilter is not null) + { + this.LoadFilteredPolicy(Options.AutoLoadPolicyFilter); + } + else + { + this.LoadPolicy(); + } } model.SortPolicy(); @@ -45,12 +57,12 @@ public Enforcer(IModel model, IReadOnlyAdapter adapter = null, bool lazyLoadPoli #region Options - public bool Enabled { get; set; } = true; - public bool EnabledCache { get; set; } = true; - - public bool AutoBuildRoleLinks { get; set; } = true; - public bool AutoNotifyWatcher { get; set; } = true; - public bool AutoCleanEnforceCache { get; set; } = true; + public IEnforcer.EnforcerOptions Options { get; set; } = new IEnforcer.EnforcerOptions(); + public bool Enabled { get => Options.Enabled; set => Options.Enabled = value; } + public bool EnabledCache { get => Options.EnabledCache; set => Options.EnabledCache = value; } + public bool AutoBuildRoleLinks { get => Options.AutoBuildRoleLinks; set => Options.AutoBuildRoleLinks = value; } + public bool AutoNotifyWatcher { get => Options.AutoNotifyWatcher; set => Options.AutoNotifyWatcher = value; } + public bool AutoCleanEnforceCache { get => Options.AutoCleanEnforceCache; set => Options.AutoCleanEnforceCache = value; } #endregion @@ -103,12 +115,12 @@ public bool Enforce(EnforceContext context, TRequest requestValues) wh return InternalEnforce(in context, in requestValues); } - if (Enabled is false) + if (Options.Enabled is false) { return true; } - if (EnabledCache) + if (Options.EnabledCache) { if (EnforceCache.TryGetResult(requestValues, out bool cachedResult)) { @@ -121,7 +133,7 @@ public bool Enforce(EnforceContext context, TRequest requestValues) wh bool result = InternalEnforce(in context, in requestValues); - if (EnabledCache) + if (Options.EnabledCache) { EnforceCache.TrySetResult(requestValues, result); } @@ -147,12 +159,12 @@ public async Task EnforceAsync(EnforceContext context, TRequest return await InternalEnforceAsync(context, requestValues); } - if (Enabled is false) + if (Options.Enabled is false) { return true; } - if (EnabledCache) + if (Options.EnabledCache) { bool? cachedResult = await EnforceCache.TryGetResultAsync(requestValues); if (cachedResult.HasValue) @@ -167,7 +179,7 @@ public async Task EnforceAsync(EnforceContext context, TRequest context.HandleOptionAndCached = true; bool result = await InternalEnforceAsync(context, requestValues); - if (EnabledCache) + if (Options.EnabledCache) { await EnforceCache.TrySetResultAsync(requestValues, result); } diff --git a/Casbin/Extensions/Enforcer/EnforcerExtension.cs b/Casbin/Extensions/Enforcer/EnforcerExtension.cs index bd8ed46c..6f2db3dc 100644 --- a/Casbin/Extensions/Enforcer/EnforcerExtension.cs +++ b/Casbin/Extensions/Enforcer/EnforcerExtension.cs @@ -42,7 +42,7 @@ public static void LoadModel(this IEnforcer enforcer) /// public static IEnforcer EnableEnforce(this IEnforcer enforcer, bool enable) { - enforcer.Enabled = enable; + enforcer.Options.Enabled = enable; return enforcer; } @@ -66,7 +66,7 @@ public static IEnforcer EnableAutoSave(this IEnforcer enforcer, bool autoSave) /// Whether to automatically build the role links. public static IEnforcer EnableAutoBuildRoleLinks(this IEnforcer enforcer, bool autoBuildRoleLinks) { - enforcer.AutoBuildRoleLinks = autoBuildRoleLinks; + enforcer.Options.AutoBuildRoleLinks = autoBuildRoleLinks; return enforcer; } @@ -78,19 +78,19 @@ public static IEnforcer EnableAutoBuildRoleLinks(this IEnforcer enforcer, bool a /// Whether to automatically notify watcher. public static IEnforcer EnableAutoNotifyWatcher(this IEnforcer enforcer, bool autoNotifyWatcher) { - enforcer.AutoNotifyWatcher = autoNotifyWatcher; + enforcer.Options.AutoNotifyWatcher = autoNotifyWatcher; return enforcer; } public static IEnforcer EnableCache(this IEnforcer enforcer, bool enableCache) { - enforcer.EnabledCache = enableCache; + enforcer.Options.EnabledCache = enableCache; return enforcer; } public static IEnforcer EnableAutoCleanEnforceCache(this IEnforcer enforcer, bool autoCleanEnforceCache) { - enforcer.AutoCleanEnforceCache = autoCleanEnforceCache; + enforcer.Options.AutoCleanEnforceCache = autoCleanEnforceCache; return enforcer; } @@ -186,7 +186,7 @@ public static bool LoadPolicy(this IEnforcer enforcer) enforcer.ClearCache(); enforcer.Model.SortPolicy(); - if (enforcer.AutoBuildRoleLinks) + if (enforcer.Options.AutoBuildRoleLinks) { enforcer.BuildRoleLinks(); } @@ -207,7 +207,7 @@ public static async Task LoadPolicyAsync(this IEnforcer enforcer) enforcer.ClearCache(); enforcer.Model.SortPolicy(); - if (enforcer.AutoBuildRoleLinks) + if (enforcer.Options.AutoBuildRoleLinks) { enforcer.BuildRoleLinks(); } @@ -229,7 +229,7 @@ public static bool LoadFilteredPolicy(this IEnforcer enforcer, Filter filter) return false; } - if (enforcer.AutoBuildRoleLinks) + if (enforcer.Options.AutoBuildRoleLinks) { enforcer.BuildRoleLinks(); } @@ -251,7 +251,7 @@ public static async Task LoadFilteredPolicyAsync(this IEnforcer enforcer, return false; } - if (enforcer.AutoBuildRoleLinks) + if (enforcer.Options.AutoBuildRoleLinks) { enforcer.BuildRoleLinks(); } @@ -336,7 +336,7 @@ public static void SetRoleManager(this IEnforcer enforcer, IRoleManager roleMana public static void SetRoleManager(this IEnforcer enforcer, string roleType, IRoleManager roleManager) { enforcer.Model.SetRoleManager(roleType, roleManager); - if (enforcer.AutoBuildRoleLinks) + if (enforcer.Options.AutoBuildRoleLinks) { enforcer.BuildRoleLinks(); } diff --git a/Casbin/Extensions/Enforcer/InternalEnforcerExtension.Events.cs b/Casbin/Extensions/Enforcer/InternalEnforcerExtension.Events.cs index cbd6c86a..8b3fccf8 100644 --- a/Casbin/Extensions/Enforcer/InternalEnforcerExtension.Events.cs +++ b/Casbin/Extensions/Enforcer/InternalEnforcerExtension.Events.cs @@ -71,7 +71,7 @@ internal static void TryBuildIncrementalRoleLinks(this IEnforcer enforcer, Polic internal static void TryCleanEnforceCache(this IEnforcer enforcer) { - if (enforcer.AutoCleanEnforceCache) + if (enforcer.Options.AutoCleanEnforceCache) { enforcer.ClearCache(); } @@ -80,7 +80,7 @@ internal static void TryCleanEnforceCache(this IEnforcer enforcer) internal static void TryNotifyPolicyChanged(this IEnforcer enforcer, PolicyChangedMessage message) { // ReSharper disable once InvertIf - if (enforcer.AutoNotifyWatcher && enforcer.Watcher is not null) + if (enforcer.Options.AutoNotifyWatcher && enforcer.Watcher is not null) { WatcherHolder holder = enforcer.Model.WatcherHolder; if (holder.WatcherEx is not null) @@ -105,7 +105,7 @@ internal static void TryNotifyPolicyChanged(this IEnforcer enforcer, PolicyChang internal static async Task TryNotifyPolicyChangedAsync(this IEnforcer enforcer, PolicyChangedMessage message) { - if (enforcer.AutoNotifyWatcher && enforcer.Watcher is not null) + if (enforcer.Options.AutoNotifyWatcher && enforcer.Watcher is not null) { WatcherHolder holder = enforcer.Model.WatcherHolder; if (holder.WatcherEx is not null)