From a6c464e3b1d92b24fd6fe1f25c413b1543d60f15 Mon Sep 17 00:00:00 2001 From: AsakusaRinne Date: Mon, 19 Sep 2022 16:05:44 +0800 Subject: [PATCH] feat: Add GetPolicyTypes APIs of IReadOnlyPolicyStore --- Casbin/Abstractions/Model/IReadOnlyPolicyStore.cs | 6 +++++- Casbin/Model/DefaultPolicyStore.cs | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Casbin/Abstractions/Model/IReadOnlyPolicyStore.cs b/Casbin/Abstractions/Model/IReadOnlyPolicyStore.cs index b2881d7..3e8616e 100644 --- a/Casbin/Abstractions/Model/IReadOnlyPolicyStore.cs +++ b/Casbin/Abstractions/Model/IReadOnlyPolicyStore.cs @@ -12,7 +12,11 @@ public interface IReadOnlyPolicyStore public IEnumerable GetPolicy(string section, string policyType); - public IDictionary> GetPolicyAllType(string section); + public IEnumerable GetPolicyTypes(string section); + + public IDictionary> GetPolicyTypesAllSections(); + + public IDictionary> GetPolicyAllType(string section); public IEnumerable GetFilteredPolicy(string section, string policyType, int fieldIndex, IPolicyValues fieldValues); diff --git a/Casbin/Model/DefaultPolicyStore.cs b/Casbin/Model/DefaultPolicyStore.cs index e73c783..570b8ba 100644 --- a/Casbin/Model/DefaultPolicyStore.cs +++ b/Casbin/Model/DefaultPolicyStore.cs @@ -40,6 +40,19 @@ public PolicyScanner Scan(string section, string policyType) => public IEnumerable GetPolicy(string section, string policyType) => GetNode(section, policyType).GetPolicy(); + public IEnumerable GetPolicyTypes(string section) + => GetNodes(section).Select(x => x.Key); + + public IDictionary> GetPolicyTypesAllSections() + { + Dictionary> res = new Dictionary>(); + foreach (var keyValuePair in _nodesMap) + { + res.Add(keyValuePair.Key, keyValuePair.Value.Select(x => x.Key)); + } + return res; + } + public IDictionary> GetPolicyAllType(string section) => GetNodes(section).ToDictionary(kv => kv.Key, x => GetPolicy(section, x.Key));