Skip to content

Commit

Permalink
Merge pull request #295 from AsakusaRinne/add_auto_load_policy_preview
Browse files Browse the repository at this point in the history
feat: change lazyLoadPolicy to AutoLoadPolicy.
  • Loading branch information
sagilio authored Oct 15, 2022
2 parents a6c464e + 394ff02 commit 6dec197
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 44 deletions.
4 changes: 2 additions & 2 deletions Casbin.UnitTests/ModelTests/EnforcerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void TestEnforceWithMultipleEval()
}

[Fact]
public void TestEnforceWithLazyLoadPolicy()
public void TestEnforceWithoutAutoLoadPolicy()
{
IModel m = DefaultModel.Create();
m.AddDef("r", "r", "sub, obj, act");
Expand All @@ -73,7 +73,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);
Expand Down
13 changes: 13 additions & 0 deletions Casbin/Abstractions/IEnforcer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ namespace Casbin
/// </summary>
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;
}

/// <summary>
/// Decides whether a "subject" can access a "object" with the operation
/// "action", input parameters are usually: (sub, obj, act).
Expand Down Expand Up @@ -76,6 +88,7 @@ public BatchEnforceAsyncResults BatchEnforceAsync<TRequest>(EnforceContext conte

#region Options

public EnforcerOptions Options { get; set; }
public bool Enabled { get; set; }
public bool EnabledCache { get; set; }
public bool AutoBuildRoleLinks { get; set; }
Expand Down
19 changes: 10 additions & 9 deletions Casbin/DefaultEnforcer.cs
Original file line number Diff line number Diff line change
@@ -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<IEnforcer.EnforcerOptions> 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<IEnforcer.EnforcerOptions> 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<IEnforcer.EnforcerOptions> 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<IEnforcer.EnforcerOptions> optionSettings = null)
{
return new Enforcer(model, adapter, lazyLoadPolicy);
return new Enforcer(model, adapter, optionSettings);
}
}
}
52 changes: 32 additions & 20 deletions Casbin/Enforcer.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,40 +18,51 @@ public Enforcer()
{
}

public Enforcer(string modelPath, string policyPath, bool lazyLoadPolicy = false)
: this(modelPath, new FileAdapter(policyPath), lazyLoadPolicy)
public Enforcer(string modelPath, string policyPath, Action<IEnforcer.EnforcerOptions> 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<IEnforcer.EnforcerOptions> 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<IEnforcer.EnforcerOptions> 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();
}

#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

Expand Down Expand Up @@ -103,12 +115,12 @@ public bool Enforce<TRequest>(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))
{
Expand All @@ -121,7 +133,7 @@ public bool Enforce<TRequest>(EnforceContext context, TRequest requestValues) wh

bool result = InternalEnforce(in context, in requestValues);

if (EnabledCache)
if (Options.EnabledCache)
{
EnforceCache.TrySetResult(requestValues, result);
}
Expand All @@ -147,12 +159,12 @@ public async Task<bool> EnforceAsync<TRequest>(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)
Expand All @@ -167,7 +179,7 @@ public async Task<bool> EnforceAsync<TRequest>(EnforceContext context, TRequest
context.HandleOptionAndCached = true;
bool result = await InternalEnforceAsync(context, requestValues);

if (EnabledCache)
if (Options.EnabledCache)
{
await EnforceCache.TrySetResultAsync(requestValues, result);
}
Expand Down
20 changes: 10 additions & 10 deletions Casbin/Extensions/Enforcer/EnforcerExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static void LoadModel(this IEnforcer enforcer)
/// <param name="enable"></param>
public static IEnforcer EnableEnforce(this IEnforcer enforcer, bool enable)
{
enforcer.Enabled = enable;
enforcer.Options.Enabled = enable;
return enforcer;
}

Expand All @@ -66,7 +66,7 @@ public static IEnforcer EnableAutoSave(this IEnforcer enforcer, bool autoSave)
/// <param name="autoBuildRoleLinks">Whether to automatically build the role links.</param>
public static IEnforcer EnableAutoBuildRoleLinks(this IEnforcer enforcer, bool autoBuildRoleLinks)
{
enforcer.AutoBuildRoleLinks = autoBuildRoleLinks;
enforcer.Options.AutoBuildRoleLinks = autoBuildRoleLinks;
return enforcer;
}

Expand All @@ -78,19 +78,19 @@ public static IEnforcer EnableAutoBuildRoleLinks(this IEnforcer enforcer, bool a
/// <param name="autoNotifyWatcher">Whether to automatically notify watcher.</param>
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;
}

Expand Down Expand Up @@ -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();
}
Expand All @@ -207,7 +207,7 @@ public static async Task<bool> LoadPolicyAsync(this IEnforcer enforcer)

enforcer.ClearCache();
enforcer.Model.SortPolicy();
if (enforcer.AutoBuildRoleLinks)
if (enforcer.Options.AutoBuildRoleLinks)
{
enforcer.BuildRoleLinks();
}
Expand All @@ -229,7 +229,7 @@ public static bool LoadFilteredPolicy(this IEnforcer enforcer, Filter filter)
return false;
}

if (enforcer.AutoBuildRoleLinks)
if (enforcer.Options.AutoBuildRoleLinks)
{
enforcer.BuildRoleLinks();
}
Expand All @@ -251,7 +251,7 @@ public static async Task<bool> LoadFilteredPolicyAsync(this IEnforcer enforcer,
return false;
}

if (enforcer.AutoBuildRoleLinks)
if (enforcer.Options.AutoBuildRoleLinks)
{
enforcer.BuildRoleLinks();
}
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 6dec197

Please sign in to comment.