Skip to content

Commit

Permalink
fix: error has adapter value
Browse files Browse the repository at this point in the history
Signed-off-by: sagilio <sagilio@outlook.com>
  • Loading branch information
sagilio committed Jun 6, 2022
1 parent ebdbc23 commit 820900f
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 105 deletions.
8 changes: 6 additions & 2 deletions Casbin/Abstractions/Model/IPolicyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -46,8 +48,10 @@ public interface IPolicyManager : IPolicyStore

public Task<bool> RemovePolicyAsync(string section, string policyType, IEnumerable<string> rule);

public Task<bool> RemovePoliciesAsync(string section, string policyType, IEnumerable<IEnumerable<string>> rules);
public Task<bool> RemovePoliciesAsync(string section, string policyType,
IEnumerable<IEnumerable<string>> rules);

public Task<IEnumerable<IEnumerable<string>>> RemoveFilteredPolicyAsync(string section, string policyType, int fieldIndex, params string[] fieldValues);
public Task<IEnumerable<IEnumerable<string>>> RemoveFilteredPolicyAsync(string section, string policyType,
int fieldIndex, params string[] fieldValues);
}
}
132 changes: 75 additions & 57 deletions Casbin/Extensions/Enforcer/EnforcerExtension.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +13,27 @@ namespace Casbin
{
public static partial class EnforcerExtension
{
#region Model management

/// <summary>
/// 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().
/// </summary>
public static void LoadModel(this IEnforcer enforcer)
{
if (enforcer.ModelPath is null)
{
return;
}

enforcer.Model = DefaultModel.CreateFromFile(enforcer.ModelPath);
}

#endregion

#region Set options

/// <summary>
/// Changes the enforcing state of Casbin, when Casbin is disabled,
/// all access will be allowed by the enforce() function.
Expand Down Expand Up @@ -77,9 +93,11 @@ public static IEnforcer EnableAutoCleanEnforceCache(this IEnforcer enforcer, boo
enforcer.AutoCleanEnforceCache = autoCleanEnforceCache;
return enforcer;
}

#endregion

#region Set extensions

/// <summary>
/// Sets the current effector.
/// </summary>
Expand Down Expand Up @@ -114,6 +132,7 @@ public static IEnforcer SetModel(this IEnforcer enforcer, IModel model)
{
model = model.ToSyncModel();
}

enforcer.Model = model;
if (enforcer.AutoCleanEnforceCache)
{
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -179,10 +200,12 @@ public static IEnforcer SetRoleManager(this IEnforcer enforcer, string roleType,
{
enforcer.Model.BuildRoleLinks();
}

if (enforcer.AutoCleanEnforceCache)
{
enforcer.EnforceCache?.Clear();
}

return enforcer;
}

Expand All @@ -196,47 +219,29 @@ public static IEnforcer SetEnforceCache(this IEnforcer enforcer, IEnforceCache e
enforcer.EnforceCache = enforceCache;
return enforcer;
}
#endregion

#region Model management
/// <summary>
/// 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().
/// </summary>
public static void LoadModel(this IEnforcer enforcer)
{
if (enforcer.ModelPath is null)
{
return;
}
enforcer.Model = DefaultModel.CreateFromFile(enforcer.ModelPath);
}
#endregion

#region Poilcy management

/// <summary>
/// Reloads the policy from file/database.
/// </summary>
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;
}

Expand All @@ -245,24 +250,19 @@ public static bool LoadPolicy(this IEnforcer enforcer)
/// </summary>
public static async Task<bool> 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;
}

Expand All @@ -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;
}

/// <summary>
Expand All @@ -303,11 +309,17 @@ public static async Task<bool> 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;
}

/// <summary>
Expand All @@ -316,26 +328,12 @@ public static async Task<bool> LoadFilteredPolicyAsync(this IEnforcer enforcer,
/// </summary>
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;
}
Expand All @@ -362,11 +360,17 @@ public static async Task<bool> 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;
}

/// <summary>
Expand All @@ -375,6 +379,11 @@ public static async Task<bool> 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();
Expand All @@ -386,9 +395,11 @@ public static void ClearPolicy(this IEnforcer enforcer)
enforcer.Logger?.LogInformation("Store Management, Cleared all policy");
#endif
}

#endregion

#region Role management

/// <summary>
/// Manually rebuilds the role inheritance relations.
/// </summary>
Expand All @@ -409,20 +420,24 @@ public static Enforcer AddDomainMatchingFunc(this Enforcer enforcer, Func<string
return enforcer;
}

public static Enforcer AddNamedMatchingFunc(this Enforcer enforcer, string roleType, Func<string, string, bool> func)
public static Enforcer AddNamedMatchingFunc(this Enforcer enforcer, string roleType,
Func<string, string, bool> func)
{
enforcer.Model.GetRoleManger(roleType).AddMatchingFunc(func);
return enforcer;
}

public static Enforcer AddNamedDomainMatchingFunc(this Enforcer enforcer, string roleType, Func<string, string, bool> func)
public static Enforcer AddNamedDomainMatchingFunc(this Enforcer enforcer, string roleType,
Func<string, string, bool> 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);
Expand Down Expand Up @@ -452,20 +467,23 @@ public static EnforceContext CreateContextWithMatcher(this IEnforcer enforcer,
{
return EnforceContext.CreateWithMatcher(enforcer, matcher, requestType, policyType, effectType, explain);
}

#endregion

#region Enforce extensions

#endregion

#if !NET452
internal static void LogEnforceCachedResult<TRequest>(this IEnforcer enforcer, TRequest requestValues, bool finalResult)
internal static void LogEnforceCachedResult<TRequest>(this IEnforcer enforcer, TRequest requestValues,
bool finalResult)
where TRequest : IRequestValues
{
enforcer.Logger?.LogEnforceCachedResult(requestValues, finalResult);
}

internal static void LogEnforceResult<TRequest>(this IEnforcer enforcer, in EnforceContext context, TRequest requestValues, bool finalResult)
internal static void LogEnforceResult<TRequest>(this IEnforcer enforcer, in EnforceContext context,
TRequest requestValues, bool finalResult)
where TRequest : IRequestValues
{
if (context.Explain)
Expand Down
Loading

0 comments on commit 820900f

Please sign in to comment.