diff --git a/NetCasbin/Abstractions/Persist/IAdapter.cs b/NetCasbin/Abstractions/Persist/IAdapter.cs index 60895e5e..3d20b56a 100644 --- a/NetCasbin/Abstractions/Persist/IAdapter.cs +++ b/NetCasbin/Abstractions/Persist/IAdapter.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using Casbin.Model; @@ -6,32 +7,32 @@ namespace Casbin.Persist { public interface IAdapter { - void LoadPolicy(IModel model); + public void LoadPolicy(IModel model, CancellationToken cancellationToken = default); - Task LoadPolicyAsync(IModel model); + public Task LoadPolicyAsync(IModel model, CancellationToken cancellationToken = default); - void SavePolicy(IModel model); + public void SavePolicy(IModel model, CancellationToken cancellationToken = default); - Task SavePolicyAsync(IModel model); + public Task SavePolicyAsync(IModel model, CancellationToken cancellationToken = default); - void AddPolicy(string section, string policyType, IEnumerable rule); + public void AddPolicy(string section, string policyType, IEnumerable rule, CancellationToken cancellationToken = default); - Task AddPolicyAsync(string section, string policyType, IEnumerable rule); + public Task AddPolicyAsync(string section, string policyType, IEnumerable rule, CancellationToken cancellationToken = default); - void AddPolicies(string section, string policyType, IEnumerable> rules); + public void AddPolicies(string section, string policyType, IEnumerable> rules, CancellationToken cancellationToken = default); - Task AddPoliciesAsync(string section, string policyType, IEnumerable> rules); + public Task AddPoliciesAsync(string section, string policyType, IEnumerable> rules, CancellationToken cancellationToken = default); - void RemovePolicy(string section, string policyType, IEnumerable rule); + public void RemovePolicy(string section, string policyType, IEnumerable rule, CancellationToken cancellationToken = default); - Task RemovePolicyAsync(string section, string policyType, IEnumerable rule); + public Task RemovePolicyAsync(string section, string policyType, IEnumerable rule, CancellationToken cancellationToken = default); - void RemovePolicies(string section, string policyType, IEnumerable> rules); + public void RemovePolicies(string section, string policyType, IEnumerable> rules, CancellationToken cancellationToken = default); - Task RemovePoliciesAsync(string section, string policyType, IEnumerable> rules); + public Task RemovePoliciesAsync(string section, string policyType, IEnumerable> rules, CancellationToken cancellationToken = default); - void RemoveFilteredPolicy(string section, string policyType, int fieldIndex, params string[] fieldValues); + public void RemoveFilteredPolicy(string section, string policyType, int fieldIndex, params string[] fieldValues); - 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/NetCasbin/Adapter/File/FileAdapter.cs b/NetCasbin/Adapter/File/FileAdapter.cs index ea1657be..c57f03c6 100644 --- a/NetCasbin/Adapter/File/FileAdapter.cs +++ b/NetCasbin/Adapter/File/FileAdapter.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; using Casbin.Model; using Casbin.Persist; @@ -34,7 +35,7 @@ public FileAdapter(Stream inputStream) } } - public void LoadPolicy(IModel model) + public void LoadPolicy(IModel model, CancellationToken cancellationToken = default) { if (!string.IsNullOrWhiteSpace(filePath)) { @@ -51,7 +52,7 @@ public void LoadPolicy(IModel model) } } - public async Task LoadPolicyAsync(IModel model) + public async Task LoadPolicyAsync(IModel model, CancellationToken cancellationToken = default) { if (!string.IsNullOrWhiteSpace(filePath)) { @@ -68,7 +69,7 @@ public async Task LoadPolicyAsync(IModel model) } } - public void SavePolicy(IModel model) + public void SavePolicy(IModel model, CancellationToken cancellationToken = default) { if (_byteArrayInputStream != null && _readOnly) { @@ -84,7 +85,7 @@ public void SavePolicy(IModel model) SavePolicyFile(string.Join("\n", policy)); } - public async Task SavePolicyAsync(IModel model) + public async Task SavePolicyAsync(IModel model, CancellationToken cancellationToken = default) { if (_byteArrayInputStream != null && _readOnly) { @@ -158,14 +159,14 @@ private async Task SavePolicyFileAsync(string text) } } - public void AddPolicies(string sec, string ptype, IEnumerable> rules) => throw new NotImplementedException(); - public Task AddPoliciesAsync(string sec, string ptype, IEnumerable> rules) => throw new NotImplementedException(); - public void RemovePolicies(string sec, string ptype, IEnumerable> rules) => throw new NotImplementedException(); - public Task RemovePoliciesAsync(string sec, string ptype, IEnumerable> rules) => throw new NotImplementedException(); - public void AddPolicy(string sec, string ptype, IEnumerable rule) => throw new NotImplementedException(); - public Task AddPolicyAsync(string sec, string ptype, IEnumerable rule) => throw new NotImplementedException(); - public void RemovePolicy(string sec, string ptype, IEnumerable rule) => throw new NotImplementedException(); - public Task RemovePolicyAsync(string sec, string ptype, IEnumerable rule) => throw new NotImplementedException(); + public void AddPolicies(string sec, string ptype, IEnumerable> rules, CancellationToken cancellationToken = default) => throw new NotImplementedException(); + public Task AddPoliciesAsync(string sec, string ptype, IEnumerable> rules, CancellationToken cancellationToken = default) => throw new NotImplementedException(); + public void RemovePolicies(string sec, string ptype, IEnumerable> rules, CancellationToken cancellationToken = default) => throw new NotImplementedException(); + public Task RemovePoliciesAsync(string sec, string ptype, IEnumerable> rules, CancellationToken cancellationToken = default) => throw new NotImplementedException(); + public void AddPolicy(string sec, string ptype, IEnumerable rule, CancellationToken cancellationToken = default) => throw new NotImplementedException(); + public Task AddPolicyAsync(string sec, string ptype, IEnumerable rule, CancellationToken cancellationToken = default) => throw new NotImplementedException(); + public void RemovePolicy(string sec, string ptype, IEnumerable rule, CancellationToken cancellationToken = default) => throw new NotImplementedException(); + public Task RemovePolicyAsync(string sec, string ptype, IEnumerable rule, CancellationToken cancellationToken = default) => throw new NotImplementedException(); public void RemoveFilteredPolicy(string sec, string ptype, int fieldIndex, params string[] fieldValues) => throw new NotImplementedException(); public Task RemoveFilteredPolicyAsync(string sec, string ptype, int fieldIndex, params string[] fieldValues) => throw new NotImplementedException(); } diff --git a/NetCasbin/Model/DefaultPolicyManager.cs b/NetCasbin/Model/DefaultPolicyManager.cs index 0b152ce8..81ece4bc 100644 --- a/NetCasbin/Model/DefaultPolicyManager.cs +++ b/NetCasbin/Model/DefaultPolicyManager.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Casbin.Persist; @@ -18,6 +19,7 @@ public DefaultPolicyManager(IPolicy policy, IAdapter adapter = null) } public virtual bool IsSynchronized => false; + public CancellationToken CancellationToken { get; set; } public bool AutoSave { get; set; } public IAdapter Adapter { get; set; } public bool HasAdapter => Adapter is null; @@ -167,7 +169,7 @@ public bool AddPolicy(string section, string policyType, IEnumerable rul try { - Adapter.AddPolicy(section, policyType, ruleArray); + Adapter.AddPolicy(section, policyType, ruleArray, CancellationToken); } catch (NotImplementedException) { @@ -200,7 +202,7 @@ public bool AddPolicies(string section, string policyType, IEnumerable try { - Adapter.RemovePolicy(section, policyType, ruleArray); + Adapter.RemovePolicy(section, policyType, ruleArray, CancellationToken); } catch (NotImplementedException) { @@ -266,7 +268,7 @@ public bool RemovePolicies(string section, string policyType, IEnumerable AddPoliciesAsync(string section, string policyType, IEnu try { - await Adapter.AddPoliciesAsync(section, policyType, rulesArray); + await Adapter.AddPoliciesAsync(section, policyType, rulesArray, CancellationToken); } catch (NotImplementedException) { @@ -396,7 +398,7 @@ public async Task RemovePolicyAsync(string section, string policyType, IEn try { - await Adapter.RemovePolicyAsync(section, policyType, ruleArray); + await Adapter.RemovePolicyAsync(section, policyType, ruleArray, CancellationToken); } catch (NotImplementedException) { @@ -429,7 +431,7 @@ public async Task RemovePoliciesAsync(string section, string policyType, I try { - await Adapter.RemovePoliciesAsync(section, policyType, rulesArray); + await Adapter.RemovePoliciesAsync(section, policyType, rulesArray, CancellationToken); } catch (NotImplementedException) {