@@ -94,68 +94,27 @@ 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-
102- if ( model . AdapterHolder . FilteredAdapter is null )
103- {
104- return false ;
105- }
106-
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-
121- model . AdapterHolder . FilteredAdapter . LoadFilteredPolicy ( policyStore , filter ) ;
122- model . PolicyStoreHolder . PolicyStore = policyStore ;
123- return true ;
97+ model . PolicyStoreHolder . PolicyStore ? . ClearPolicy ( ) ;
98+ return LoadFilteredPolicyInternal ( model , filter ) ;
12499 }
125100
126101 public static async Task < bool > LoadFilteredPolicyAsync ( this IModel model , IPolicyFilter filter )
127102 {
128- if ( model . AdapterHolder . Adapter is null )
129- {
130- return false ;
131- }
132-
133- if ( model . AdapterHolder . FilteredAdapter is null )
134- {
135- return false ;
136- }
137-
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- }
103+ model . PolicyStoreHolder . PolicyStore ? . ClearPolicy ( ) ;
104+ return await LoadFilteredPolicyInternalAsync ( model , filter ) ;
105+ }
143106
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- }
107+ public static bool LoadIncrementalFilteredPolicy ( this IModel model , IPolicyFilter filter )
108+ {
109+ return LoadFilteredPolicyInternal ( model , filter ) ;
110+ }
151111
152- await model . AdapterHolder . FilteredAdapter . LoadFilteredPolicyAsync ( policyStore ,
153- filter ) ;
154- model . PolicyStoreHolder . PolicyStore = policyStore ;
155- return true ;
112+ public static async Task < bool > LoadIncrementalFilteredPolicyAsync ( this IModel model , IPolicyFilter filter )
113+ {
114+ return await LoadFilteredPolicyInternalAsync ( model , filter ) ;
156115 }
157116
158- public static bool LoadIncrementalFilteredPolicy ( this IModel model , IPolicyFilter filter )
117+ private static bool LoadFilteredPolicyInternal ( IModel model , IPolicyFilter filter )
159118 {
160119 if ( model . AdapterHolder . Adapter is null )
161120 {
@@ -167,19 +126,30 @@ public static bool LoadIncrementalFilteredPolicy(this IModel model, IPolicyFilte
167126 return false ;
168127 }
169128
129+ // Initialize policy store if it doesn't exist
170130 if ( model . PolicyStoreHolder . PolicyStore is null )
171131 {
172- throw new InvalidOperationException (
173- "Cannot load incremental filtered policy when policy store is not initialized. " +
174- "Call LoadPolicy() or LoadFilteredPolicy() first." ) ;
132+ DefaultPolicyStore policyStore = new ( ) ;
133+ foreach ( KeyValuePair < string , PolicyAssertion > pair in model . Sections . GetPolicyAssertions ( ) )
134+ {
135+ policyStore . AddNode ( PermConstants . Section . PolicySection , pair . Key , pair . Value ) ;
136+ }
137+
138+ if ( model . Sections . ContainsSection ( PermConstants . Section . RoleSection ) )
139+ {
140+ foreach ( KeyValuePair < string , RoleAssertion > pair in model . Sections . GetRoleAssertions ( ) )
141+ {
142+ policyStore . AddNode ( PermConstants . Section . RoleSection , pair . Key , pair . Value ) ;
143+ }
144+ }
145+ model . PolicyStoreHolder . PolicyStore = policyStore ;
175146 }
176147
177- model . AdapterHolder . FilteredAdapter . LoadIncrementalFilteredPolicy (
178- model . PolicyStoreHolder . PolicyStore , filter ) ;
148+ model . AdapterHolder . FilteredAdapter . LoadFilteredPolicy ( model . PolicyStoreHolder . PolicyStore , filter ) ;
179149 return true ;
180150 }
181151
182- public static async Task < bool > LoadIncrementalFilteredPolicyAsync ( this IModel model , IPolicyFilter filter )
152+ private static async Task < bool > LoadFilteredPolicyInternalAsync ( IModel model , IPolicyFilter filter )
183153 {
184154 if ( model . AdapterHolder . Adapter is null )
185155 {
@@ -191,15 +161,26 @@ public static async Task<bool> LoadIncrementalFilteredPolicyAsync(this IModel mo
191161 return false ;
192162 }
193163
164+ // Initialize policy store if it doesn't exist
194165 if ( model . PolicyStoreHolder . PolicyStore is null )
195166 {
196- throw new InvalidOperationException (
197- "Cannot load incremental filtered policy when policy store is not initialized. " +
198- "Call LoadPolicy() or LoadFilteredPolicy() first." ) ;
167+ DefaultPolicyStore policyStore = new ( ) ;
168+ foreach ( KeyValuePair < string , PolicyAssertion > pair in model . Sections . GetPolicyAssertions ( ) )
169+ {
170+ policyStore . AddNode ( PermConstants . Section . PolicySection , pair . Key , pair . Value ) ;
171+ }
172+
173+ if ( model . Sections . ContainsSection ( PermConstants . Section . RoleSection ) )
174+ {
175+ foreach ( KeyValuePair < string , RoleAssertion > pair in model . Sections . GetRoleAssertions ( ) )
176+ {
177+ policyStore . AddNode ( PermConstants . Section . RoleSection , pair . Key , pair . Value ) ;
178+ }
179+ }
180+ model . PolicyStoreHolder . PolicyStore = policyStore ;
199181 }
200182
201- await model . AdapterHolder . FilteredAdapter . LoadIncrementalFilteredPolicyAsync (
202- model . PolicyStoreHolder . PolicyStore , filter ) ;
183+ await model . AdapterHolder . FilteredAdapter . LoadFilteredPolicyAsync ( model . PolicyStoreHolder . PolicyStore , filter ) ;
203184 return true ;
204185 }
205186
0 commit comments