Skip to content

Commit

Permalink
feat: Add GetPolicyTypes APIs of IReadOnlyPolicyStore
Browse files Browse the repository at this point in the history
  • Loading branch information
AsakusaRinne authored and sagilio committed Oct 4, 2022
1 parent 5c007fd commit a6c464e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Casbin/Abstractions/Model/IReadOnlyPolicyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ public interface IReadOnlyPolicyStore

public IEnumerable<IPolicyValues> GetPolicy(string section, string policyType);

public IDictionary<string, IEnumerable<IPolicyValues>> GetPolicyAllType(string section);
public IEnumerable<string> GetPolicyTypes(string section);

public IDictionary<string, IEnumerable<string>> GetPolicyTypesAllSections();

public IDictionary<string, IEnumerable<IPolicyValues>> GetPolicyAllType(string section);

public IEnumerable<IPolicyValues> GetFilteredPolicy(string section, string policyType, int fieldIndex,
IPolicyValues fieldValues);
Expand Down
13 changes: 13 additions & 0 deletions Casbin/Model/DefaultPolicyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ public bool AddNode(string section, string type, PolicyAssertion policyAssertion
public IEnumerable<IPolicyValues> GetPolicy(string section, string policyType)
=> GetNode(section, policyType).GetPolicy();

public IEnumerable<string> GetPolicyTypes(string section)
=> GetNodes(section).Select(x => x.Key);

public IDictionary<string, IEnumerable<string>> GetPolicyTypesAllSections()
{
Dictionary<string, IEnumerable<string>> res = new Dictionary<string, IEnumerable<string>>();
foreach (var keyValuePair in _nodesMap)
{
res.Add(keyValuePair.Key, keyValuePair.Value.Select(x => x.Key));
}
return res;
}

public IDictionary<string, IEnumerable<IPolicyValues>> GetPolicyAllType(string section)
=> GetNodes(section).ToDictionary(kv =>
kv.Key, x => GetPolicy(section, x.Key));
Expand Down

0 comments on commit a6c464e

Please sign in to comment.