Skip to content

Commit 638e39f

Browse files
committed
fix: err filter logic
Signed-off-by: sagilio <[email protected]>
1 parent 67e927a commit 638e39f

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

Casbin/EnforceOptions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ public class EnforcerOptions
1010
public bool AutoNotifyWatcher { get; set; } = true;
1111
public bool AutoCleanEnforceCache { get; set; } = true;
1212
public bool AutoLoadPolicy { get; set; } = true;
13-
public IPolicyFilter AutoLoadPolicyFilter { get; set; } = new Filter();
13+
public IPolicyFilter AutoLoadPolicyFilter { get; set; } = null;
1414
}
1515

1616

17+
18+

Casbin/Extensions/Model/ModelExtension.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,64 @@ public static async Task<bool> LoadPolicyAsync(this IModel model)
9494

9595
public static bool LoadFilteredPolicy(this IModel model, IPolicyFilter filter)
9696
{
97+
if (model.AdapterHolder.Adapter is null)
98+
{
99+
return false;
100+
}
101+
97102
if (model.AdapterHolder.FilteredAdapter is null)
98103
{
99104
return false;
100105
}
101106

107+
DefaultPolicyStore policyStore = new();
108+
foreach (KeyValuePair<string, PolicyAssertion> pair in model.Sections.GetPolicyAssertions())
109+
{
110+
policyStore.AddNode(PermConstants.Section.PolicySection, pair.Key, pair.Value);
111+
}
112+
113+
if (model.Sections.ContainsSection(PermConstants.Section.RoleSection))
114+
{
115+
foreach (KeyValuePair<string, RoleAssertion> pair in model.Sections.GetRoleAssertions())
116+
{
117+
policyStore.AddNode(PermConstants.Section.RoleSection, pair.Key, pair.Value);
118+
}
119+
}
120+
102121
model.AdapterHolder.FilteredAdapter.LoadFilteredPolicy(model.PolicyStoreHolder.PolicyStore, filter);
122+
model.PolicyStoreHolder.PolicyStore = policyStore;
103123
return true;
104124
}
105125

106126
public static async Task<bool> LoadFilteredPolicyAsync(this IModel model, IPolicyFilter filter)
107127
{
128+
if (model.AdapterHolder.Adapter is null)
129+
{
130+
return false;
131+
}
132+
108133
if (model.AdapterHolder.FilteredAdapter is null)
109134
{
110135
return false;
111136
}
112137

138+
DefaultPolicyStore policyStore = new();
139+
foreach (KeyValuePair<string, PolicyAssertion> pair in model.Sections.GetPolicyAssertions())
140+
{
141+
policyStore.AddNode(PermConstants.Section.PolicySection, pair.Key, pair.Value);
142+
}
143+
144+
if (model.Sections.ContainsSection(PermConstants.Section.RoleSection))
145+
{
146+
foreach (KeyValuePair<string, RoleAssertion> pair in model.Sections.GetRoleAssertions())
147+
{
148+
policyStore.AddNode(PermConstants.Section.RoleSection, pair.Key, pair.Value);
149+
}
150+
}
151+
113152
await model.AdapterHolder.FilteredAdapter.LoadFilteredPolicyAsync(model.PolicyStoreHolder.PolicyStore,
114153
filter);
154+
model.PolicyStoreHolder.PolicyStore = policyStore;
115155
return true;
116156
}
117157

Casbin/Persist/Filter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public IEnumerable<string> P
2222
{
2323
_p = value;
2424
_filterP = new PolicyFilter(PermConstants.DefaultPolicyType, 0,
25-
Policy.CreateValues(value));
25+
Policy.ValuesFrom(value));
2626
}
2727
}
2828

@@ -33,7 +33,7 @@ public IEnumerable<string> G
3333
{
3434
_g = value;
3535
_filterG = new PolicyFilter(PermConstants.DefaultGroupingPolicyType, 0,
36-
Policy.CreateValues(value));
36+
Policy.ValuesFrom(value));
3737
}
3838
}
3939

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

7070

7171

72+

0 commit comments

Comments
 (0)