• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

casbin / Casbin.NET / 64

01 Nov 2025 03:09AM UTC coverage: 60.655% (-0.1%) from 60.754%
64

push

github

web-flow
feat: add LoadIncrementalFilteredPolicy() API for incremental filtered policy loading (#392)

3202 of 5279 relevant lines covered (60.66%)

36537.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/Casbin/Extensions/Model/ModelExtension.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Threading.Tasks;
4
using Casbin.Functions;
5
using Casbin.Persist;
6
using Casbin.Rbac;
7

8
namespace Casbin.Model
9
{
10
    public static class ModelExtension
11
    {
12
        public static bool SortPolicy(this IModel model)
13
        {
14
            string value = model.Sections.GetValue(PermConstants.Section.PolicyEffectSection,
336✔
15
                PermConstants.DefaultPolicyEffectType);
336✔
16
            switch (value)
17
            {
18
                case PermConstants.PolicyEffect.Priority:
19
                case PermConstants.PolicyEffect.PriorityDenyOverride:
20
                    return model.PolicyStoreHolder.PolicyStore.SortPolicyByPriority(PermConstants.Section.PolicySection,
12✔
21
                        PermConstants.DefaultPolicyType);
12✔
22
                case PermConstants.PolicyEffect.SubjectPriority:
23
                    Dictionary<string, int> map = model.Sections.GetRoleAssertion(PermConstants.DefaultRoleType)
4✔
24
                        .GetSubjectHierarchyMap();
4✔
25
                    return model.PolicyStoreHolder.PolicyStore.SortPolicyBySubjectHierarchy(
4✔
26
                        PermConstants.Section.PolicySection,
4✔
27
                        PermConstants.DefaultPolicyType, map);
4✔
28
                default:
29
                    return false;
320✔
30
            }
31
        }
32

33
        public static bool LoadPolicy(this IModel model)
34
        {
35
            if (model.AdapterHolder.Adapter is null)
186✔
36
            {
37
                return false;
133✔
38
            }
39

40
            if (model.AdapterHolder.EpochAdapter is null)
53✔
41
            {
42
                return false;
×
43
            }
44

45
            DefaultPolicyStore policyStore = new();
53✔
46
            foreach (KeyValuePair<string, PolicyAssertion> pair in model.Sections.GetPolicyAssertions())
212✔
47
            {
48
                policyStore.AddNode(PermConstants.Section.PolicySection, pair.Key, pair.Value);
53✔
49
            }
50

51
            if (model.Sections.ContainsSection(PermConstants.Section.RoleSection))
53✔
52
            {
53
                foreach (KeyValuePair<string, RoleAssertion> pair in model.Sections.GetRoleAssertions())
106✔
54
                {
55
                    policyStore.AddNode(PermConstants.Section.RoleSection, pair.Key, pair.Value);
27✔
56
                }
57
            }
58

59
            model.AdapterHolder.EpochAdapter.LoadPolicy(policyStore);
53✔
60
            model.PolicyStoreHolder.PolicyStore = policyStore;
53✔
61
            return true;
53✔
62
        }
63

64
        public static async Task<bool> LoadPolicyAsync(this IModel model)
65
        {
66
            if (model.AdapterHolder.Adapter is null)
9✔
67
            {
68
                return false;
×
69
            }
70

71
            if (model.AdapterHolder.EpochAdapter is null)
9✔
72
            {
73
                return false;
×
74
            }
75

76
            DefaultPolicyStore policyStore = new();
9✔
77
            foreach (KeyValuePair<string, PolicyAssertion> pair in model.Sections.GetPolicyAssertions())
36✔
78
            {
79
                policyStore.AddNode(PermConstants.Section.PolicySection, pair.Key, pair.Value);
9✔
80
            }
81

82
            if (model.Sections.ContainsSection(PermConstants.Section.RoleSection))
9✔
83
            {
84
                foreach (KeyValuePair<string, RoleAssertion> pair in model.Sections.GetRoleAssertions())
4✔
85
                {
86
                    policyStore.AddNode(PermConstants.Section.RoleSection, pair.Key, pair.Value);
1✔
87
                }
88
            }
89

90
            await model.AdapterHolder.EpochAdapter.LoadPolicyAsync(policyStore);
9✔
91
            model.PolicyStoreHolder.PolicyStore = policyStore;
9✔
92
            return true;
9✔
93
        }
9✔
94

95
        public static bool LoadFilteredPolicy(this IModel model, IPolicyFilter filter)
96
        {
97
            model.PolicyStoreHolder.PolicyStore?.ClearPolicy();
7✔
98
            return LoadFilteredPolicyInternal(model, filter);
7✔
99
        }
100

101
        public static async Task<bool> LoadFilteredPolicyAsync(this IModel model, IPolicyFilter filter)
102
        {
103
            model.PolicyStoreHolder.PolicyStore?.ClearPolicy();
7✔
104
            return await LoadFilteredPolicyInternalAsync(model, filter);
7✔
105
        }
7✔
106

107
        public static bool LoadIncrementalFilteredPolicy(this IModel model, IPolicyFilter filter)
108
        {
109
            return LoadFilteredPolicyInternal(model, filter);
3✔
110
        }
111

112
        public static async Task<bool> LoadIncrementalFilteredPolicyAsync(this IModel model, IPolicyFilter filter)
113
        {
114
            return await LoadFilteredPolicyInternalAsync(model, filter);
3✔
115
        }
3✔
116

117
        private static bool LoadFilteredPolicyInternal(IModel model, IPolicyFilter filter)
118
        {
119
            if (model.AdapterHolder.Adapter is null)
10✔
120
            {
121
                return false;
×
122
            }
123

124
            if (model.AdapterHolder.FilteredAdapter is null)
10✔
125
            {
126
                return false;
×
127
            }
128

129
            // Initialize policy store if it doesn't exist
130
            if (model.PolicyStoreHolder.PolicyStore is null)
10✔
131
            {
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;
×
146
            }
147

148
            model.AdapterHolder.FilteredAdapter.LoadFilteredPolicy(model.PolicyStoreHolder.PolicyStore, filter);
10✔
149
            return true;
10✔
150
        }
151

152
        private static async Task<bool> LoadFilteredPolicyInternalAsync(IModel model, IPolicyFilter filter)
153
        {
154
            if (model.AdapterHolder.Adapter is null)
10✔
155
            {
156
                return false;
×
157
            }
158

159
            if (model.AdapterHolder.FilteredAdapter is null)
10✔
160
            {
161
                return false;
×
162
            }
163

164
            // Initialize policy store if it doesn't exist
165
            if (model.PolicyStoreHolder.PolicyStore is null)
10✔
166
            {
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;
×
181
            }
182

183
            await model.AdapterHolder.FilteredAdapter.LoadFilteredPolicyAsync(model.PolicyStoreHolder.PolicyStore, filter);
10✔
184
            return true;
10✔
185
        }
10✔
186

187
        public static bool SavePolicy(this IModel model)
188
        {
189
            if (model.AdapterHolder.Adapter is null)
4✔
190
            {
191
                return false;
×
192
            }
193

194
            if (model.AdapterHolder.EpochAdapter is null)
4✔
195
            {
196
                throw new InvalidOperationException("Cannot save policy when use a readonly adapter");
×
197
            }
198

199
            if (model.AdapterHolder.FilteredAdapter is not null && model.AdapterHolder.FilteredAdapter.IsFiltered)
4✔
200
            {
201
                throw new InvalidOperationException("Cannot save filtered policies");
×
202
            }
203

204
            model.AdapterHolder.EpochAdapter.SavePolicy(model.PolicyStoreHolder.PolicyStore);
4✔
205
            return true;
4✔
206
        }
207

208
        public static async Task<bool> SavePolicyAsync(this IModel model)
209
        {
210
            if (model.AdapterHolder.Adapter is null)
4✔
211
            {
212
                return false;
×
213
            }
214

215
            if (model.AdapterHolder.EpochAdapter is null)
4✔
216
            {
217
                throw new InvalidOperationException("Cannot save policy when use a readonly adapter");
×
218
            }
219

220
            if (model.AdapterHolder.FilteredAdapter is not null && model.AdapterHolder.FilteredAdapter.IsFiltered)
4✔
221
            {
222
                throw new InvalidOperationException("Cannot save filtered policies");
×
223
            }
224

225
            await model.AdapterHolder.EpochAdapter.SavePolicyAsync(model.PolicyStoreHolder.PolicyStore);
4✔
226
            return true;
4✔
227
        }
4✔
228

229
        public static void SetAutoSave(this IModel model, bool autoSave)
230
        {
231
            foreach (KeyValuePair<string, PolicyAssertion> pair in model.Sections.GetPolicyAssertions())
16✔
232
            {
233
                pair.Value.PolicyManager.AutoSave = autoSave;
4✔
234
            }
235
        }
4✔
236

237
        public static IPolicyManager GetPolicyManager(this IModel model, string section,
238
            string policyType = PermConstants.DefaultPolicyType) =>
239
            model.Sections.GetPolicyAssertion(section, policyType).PolicyManager;
5,267✔
240

241
        public static IRoleManager
242
            GetRoleManager(this IModel model, string roleType = PermConstants.DefaultRoleType) =>
243
            model.Sections.GetRoleAssertion(roleType).RoleManager;
206✔
244

245
        public static void SetRoleManager(this IModel model, string roleType, IRoleManager roleManager)
246
        {
247
            RoleAssertion assertion = model.Sections.GetRoleAssertion(roleType);
5✔
248
            assertion.RoleManager = roleManager;
5✔
249
            model.EnforceCache.Clear();
5✔
250
            model.GFunctionCachePool.Clear(roleType);
5✔
251
            model.ExpressionHandler.SetFunction(roleType, BuiltInFunctions.GenerateGFunction(
5✔
252
                assertion.RoleManager, model.GFunctionCachePool.GetCache(roleType)));
5✔
253
        }
5✔
254

255
        /// <summary>
256
        ///     Initializes the roles in RBAC.
257
        /// </summary>
258
        public static void BuildRoleLinks(this IModel model, string roleType = null)
259
        {
260
            if (model.Sections.ContainsSection(PermConstants.Section.RoleSection) is false)
129✔
261
            {
262
                return;
39✔
263
            }
264

265
            if (roleType is not null)
90✔
266
            {
267
                RoleAssertion assertion = model.Sections.GetRoleAssertion(roleType);
×
268
                assertion.RoleManager.Clear();
×
269
                assertion.BuildRoleLinks();
×
270
                model.GFunctionCachePool.Clear(roleType);
×
271
                return;
×
272
            }
273

274
            foreach (KeyValuePair<string, RoleAssertion> pair in model.Sections.GetRoleAssertions())
382✔
275
            {
276
                string name = pair.Key;
101✔
277
                RoleAssertion assertion = pair.Value;
101✔
278
                assertion.RoleManager.Clear();
101✔
279
                model.GFunctionCachePool.Clear(name);
101✔
280
            }
281

282
            foreach (KeyValuePair<string, RoleAssertion> pair in model.Sections.GetRoleAssertions())
382✔
283
            {
284
                RoleAssertion assertion = pair.Value;
101✔
285
                assertion.BuildRoleLinks();
101✔
286
            }
287
        }
90✔
288

289
        /// <summary>
290
        ///     Provides incremental build the role inheritance relation.
291
        /// </summary>
292
        /// <param name="model"></param>
293
        /// <param name="policyOperation"></param>
294
        /// <param name="roleType"></param>
295
        /// <param name="rule"></param>
296
        public static void BuildIncrementalRoleLink(this IModel model, PolicyOperation policyOperation,
297
            string roleType, IPolicyValues rule)
298
        {
299
            if (model.Sections.ContainsSection(PermConstants.Section.RoleSection) is false)
59✔
300
            {
301
                return;
×
302
            }
303

304
            RoleAssertion assertion = model.Sections.GetRoleAssertion(roleType);
59✔
305
            assertion.BuildIncrementalRoleLink(policyOperation, rule);
59✔
306
            model.GFunctionCachePool.Clear(roleType);
59✔
307
        }
59✔
308

309
        /// <summary>
310
        ///     Provides incremental build the role inheritance relation.
311
        /// </summary>
312
        /// <param name="model"></param>
313
        /// <param name="policyOperation"></param>
314
        /// <param name="roleType"></param>
315
        /// <param name="oldRule"></param>
316
        /// <param name="newRule"></param>
317
        public static void BuildIncrementalRoleLink(this IModel model, PolicyOperation policyOperation,
318
            string roleType, IPolicyValues oldRule, IPolicyValues newRule)
319
        {
320
            if (model.Sections.ContainsSection(PermConstants.Section.RoleSection) is false)
4✔
321
            {
322
                return;
×
323
            }
324

325
            RoleAssertion assertion = model.Sections.GetRoleAssertion(roleType);
4✔
326
            assertion.BuildIncrementalRoleLink(policyOperation, oldRule, newRule);
4✔
327
            model.GFunctionCachePool.Clear(roleType);
4✔
328
        }
4✔
329

330
        /// <summary>
331
        ///     Provides incremental build the role inheritance relations.
332
        /// </summary>
333
        /// <param name="model"></param>
334
        /// <param name="policyOperation"></param>
335
        /// <param name="roleType"></param>
336
        /// <param name="rules"></param>
337
        public static void BuildIncrementalRoleLinks(this IModel model, PolicyOperation policyOperation,
338
            string roleType, IEnumerable<IPolicyValues> rules)
339
        {
340
            if (model.Sections.ContainsSection(PermConstants.Section.RoleSection) is false)
1,022✔
341
            {
342
                return;
×
343
            }
344

345
            RoleAssertion assertion = model.Sections.GetRoleAssertion(roleType);
1,022✔
346
            assertion.BuildIncrementalRoleLinks(policyOperation, rules);
1,022✔
347
            model.GFunctionCachePool.Clear(roleType);
1,022✔
348
        }
1,022✔
349

350
        /// <summary>
351
        ///     Provides incremental build the role inheritance relations.
352
        /// </summary>
353
        /// <param name="model"></param>
354
        /// <param name="policyOperation"></param>
355
        /// <param name="roleType"></param>
356
        /// <param name="oldRules"></param>
357
        /// <param name="newRules"></param>
358
        public static void BuildIncrementalRoleLinks(this IModel model, PolicyOperation policyOperation,
359
            string roleType, IEnumerable<IPolicyValues> oldRules, IEnumerable<IPolicyValues> newRules)
360
        {
361
            if (model.Sections.ContainsSection(PermConstants.Section.RoleSection) is false)
2✔
362
            {
363
                return;
×
364
            }
365

366
            RoleAssertion assertion = model.Sections.GetRoleAssertion(roleType);
2✔
367
            assertion.BuildIncrementalRoleLinks(policyOperation, oldRules, newRules);
2✔
368
            model.GFunctionCachePool.Clear(roleType);
2✔
369
        }
2✔
370
    }
371
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc