Skip to content

Commit

Permalink
fix: err filter logic
Browse files Browse the repository at this point in the history
Signed-off-by: sagilio <sagilio@outlook.com>
  • Loading branch information
sagilio committed Nov 22, 2022
1 parent 67e927a commit b498d7a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
4 changes: 3 additions & 1 deletion Casbin/EnforceOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public class EnforcerOptions
public bool AutoNotifyWatcher { get; set; } = true;
public bool AutoCleanEnforceCache { get; set; } = true;
public bool AutoLoadPolicy { get; set; } = true;
public IPolicyFilter AutoLoadPolicyFilter { get; set; } = new Filter();
public IPolicyFilter AutoLoadPolicyFilter { get; set; } = null;
}




44 changes: 42 additions & 2 deletions Casbin/Extensions/Model/ModelExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,64 @@ public static async Task<bool> LoadPolicyAsync(this IModel model)

public static bool LoadFilteredPolicy(this IModel model, IPolicyFilter filter)
{
if (model.AdapterHolder.Adapter is null)
{
return false;
}

if (model.AdapterHolder.FilteredAdapter is null)
{
return false;
}

model.AdapterHolder.FilteredAdapter.LoadFilteredPolicy(model.PolicyStoreHolder.PolicyStore, filter);
DefaultPolicyStore policyStore = new();
foreach (KeyValuePair<string, PolicyAssertion> pair in model.Sections.GetPolicyAssertions())
{
policyStore.AddNode(PermConstants.Section.PolicySection, pair.Key, pair.Value);
}

if (model.Sections.ContainsSection(PermConstants.Section.RoleSection))
{
foreach (KeyValuePair<string, RoleAssertion> pair in model.Sections.GetRoleAssertions())
{
policyStore.AddNode(PermConstants.Section.RoleSection, pair.Key, pair.Value);
}
}

model.AdapterHolder.FilteredAdapter.LoadFilteredPolicy(policyStore, filter);
model.PolicyStoreHolder.PolicyStore = policyStore;
return true;
}

public static async Task<bool> LoadFilteredPolicyAsync(this IModel model, IPolicyFilter filter)
{
if (model.AdapterHolder.Adapter is null)
{
return false;
}

if (model.AdapterHolder.FilteredAdapter is null)
{
return false;
}

await model.AdapterHolder.FilteredAdapter.LoadFilteredPolicyAsync(model.PolicyStoreHolder.PolicyStore,
DefaultPolicyStore policyStore = new();
foreach (KeyValuePair<string, PolicyAssertion> pair in model.Sections.GetPolicyAssertions())
{
policyStore.AddNode(PermConstants.Section.PolicySection, pair.Key, pair.Value);
}

if (model.Sections.ContainsSection(PermConstants.Section.RoleSection))
{
foreach (KeyValuePair<string, RoleAssertion> pair in model.Sections.GetRoleAssertions())
{
policyStore.AddNode(PermConstants.Section.RoleSection, pair.Key, pair.Value);
}
}

await model.AdapterHolder.FilteredAdapter.LoadFilteredPolicyAsync(policyStore,
filter);
model.PolicyStoreHolder.PolicyStore = policyStore;
return true;
}

Expand Down
5 changes: 3 additions & 2 deletions Casbin/Persist/Filter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public IEnumerable<string> P
{
_p = value;
_filterP = new PolicyFilter(PermConstants.DefaultPolicyType, 0,
Policy.CreateValues(value));
Policy.ValuesFrom(value));
}
}

Expand All @@ -33,7 +33,7 @@ public IEnumerable<string> G
{
_g = value;
_filterG = new PolicyFilter(PermConstants.DefaultGroupingPolicyType, 0,
Policy.CreateValues(value));
Policy.ValuesFrom(value));
}
}

Expand Down Expand Up @@ -69,3 +69,4 @@ public IQueryable<T> Apply<T>(IQueryable<T> policies) where T : IPersistPolicy




0 comments on commit b498d7a

Please sign in to comment.