casbin 5.45.2 → 5.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -116,26 +116,9 @@ https://casbin.org/docs/role-managers
116
116
  This project exists thanks to all the people who contribute.
117
117
  <a href="https://github.com/casbin/node-casbin/graphs/contributors"><img src="https://opencollective.com/node-casbin/contributors.svg?width=890&button=false" /></a>
118
118
 
119
- ## Backers
119
+ ## Star History
120
120
 
121
- Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/casbin#backer)]
122
-
123
- <a href="https://opencollective.com/casbin#backers" target="_blank"><img src="https://opencollective.com/casbin/backers.svg?width=890"></a>
124
-
125
- ## Sponsors
126
-
127
- Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/casbin#sponsor)]
128
-
129
- <a href="https://opencollective.com/casbin/sponsor/0/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/0/avatar.svg"></a>
130
- <a href="https://opencollective.com/casbin/sponsor/1/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/1/avatar.svg"></a>
131
- <a href="https://opencollective.com/casbin/sponsor/2/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/2/avatar.svg"></a>
132
- <a href="https://opencollective.com/casbin/sponsor/3/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/3/avatar.svg"></a>
133
- <a href="https://opencollective.com/casbin/sponsor/4/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/4/avatar.svg"></a>
134
- <a href="https://opencollective.com/casbin/sponsor/5/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/5/avatar.svg"></a>
135
- <a href="https://opencollective.com/casbin/sponsor/6/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/6/avatar.svg"></a>
136
- <a href="https://opencollective.com/casbin/sponsor/7/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/7/avatar.svg"></a>
137
- <a href="https://opencollective.com/casbin/sponsor/8/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/8/avatar.svg"></a>
138
- <a href="https://opencollective.com/casbin/sponsor/9/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/9/avatar.svg"></a>
121
+ [![Star History Chart](https://api.star-history.com/svg?repos=casbin/node-casbin&type=Date)](https://star-history.com/#casbin/node-casbin&Date)
139
122
 
140
123
  ## License
141
124
 
@@ -144,7 +127,5 @@ This project is licensed under the [Apache 2.0 license](LICENSE).
144
127
  ## Contact
145
128
 
146
129
  If you have any issues or feature requests, please contact us. PR is welcomed.
147
-
148
130
  - https://github.com/casbin/node-casbin/issues
149
- - hsluoyz@gmail.com
150
- - Tencent QQ group: [546057381](//shang.qq.com/wpa/qunwpa?idkey=8ac8b91fc97ace3d383d0035f7aa06f7d670fd8e8d4837347354a31c18fac885)
131
+ - https://discord.gg/S5UjpzGZjN
@@ -8,6 +8,12 @@ export declare class InternalEnforcer extends CoreEnforcer {
8
8
  */
9
9
  protected addPolicyInternal(sec: string, ptype: string, rule: string[], useWatcher: boolean): Promise<boolean>;
10
10
  protected addPoliciesInternal(sec: string, ptype: string, rules: string[][], useWatcher: boolean): Promise<boolean>;
11
+ /**
12
+ * addPoliciesInternalEx adds rules to the current policy.
13
+ * Unlike addPoliciesInternal, this method will filter out rules that already exist
14
+ * and continue to add the remaining rules instead of returning false immediately.
15
+ */
16
+ protected addPoliciesInternalEx(sec: string, ptype: string, rules: string[][], useWatcher: boolean): Promise<boolean>;
11
17
  /**
12
18
  * updatePolicyInternal updates a rule from the current policy.
13
19
  */
@@ -94,6 +94,50 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
94
94
  }
95
95
  return ok;
96
96
  }
97
+ /**
98
+ * addPoliciesInternalEx adds rules to the current policy.
99
+ * Unlike addPoliciesInternal, this method will filter out rules that already exist
100
+ * and continue to add the remaining rules instead of returning false immediately.
101
+ */
102
+ async addPoliciesInternalEx(sec, ptype, rules, useWatcher) {
103
+ // Filter out existing rules
104
+ const newRules = rules.filter((rule) => !this.model.hasPolicy(sec, ptype, rule));
105
+ // If no new rules to add, return false
106
+ if (newRules.length === 0) {
107
+ return false;
108
+ }
109
+ if (this.autoSave) {
110
+ if ('addPolicies' in this.adapter) {
111
+ try {
112
+ await this.adapter.addPolicies(sec, ptype, newRules);
113
+ }
114
+ catch (e) {
115
+ if (e.message !== 'not implemented') {
116
+ throw e;
117
+ }
118
+ }
119
+ }
120
+ else {
121
+ throw new Error('cannot save policy, the adapter does not implement the BatchAdapter');
122
+ }
123
+ }
124
+ if (useWatcher) {
125
+ if (this.autoNotifyWatcher) {
126
+ // error intentionally ignored
127
+ if (this.watcherEx) {
128
+ this.watcherEx.updateForAddPolicies(sec, ptype, ...newRules);
129
+ }
130
+ else if (this.watcher) {
131
+ this.watcher.update();
132
+ }
133
+ }
134
+ }
135
+ const [ok, effects] = await this.model.addPolicies(sec, ptype, newRules);
136
+ if (sec === 'g' && ok && (effects === null || effects === void 0 ? void 0 : effects.length)) {
137
+ await this.buildIncrementalRoleLinks(model_1.PolicyOp.PolicyAdd, ptype, effects);
138
+ }
139
+ return ok;
140
+ }
97
141
  /**
98
142
  * updatePolicyInternal updates a rule from the current policy.
99
143
  */
@@ -195,6 +195,25 @@ export declare class ManagementEnforcer extends InternalEnforcer {
195
195
  * @return succeeds or not.
196
196
  */
197
197
  addNamedPolicies(ptype: string, rules: string[][]): Promise<boolean>;
198
+ /**
199
+ * addPoliciesEx adds authorization rules to the current policy.
200
+ * If a rule already exists, the function will skip it and continue to add the remaining rules.
201
+ * The function returns true if at least one rule was added successfully.
202
+ *
203
+ * @param rules the "p" policy rules, ptype "p" is implicitly used.
204
+ * @return succeeds or not.
205
+ */
206
+ addPoliciesEx(rules: string[][]): Promise<boolean>;
207
+ /**
208
+ * addNamedPoliciesEx adds authorization rules to the current named policy.
209
+ * If a rule already exists, the function will skip it and continue to add the remaining rules.
210
+ * The function returns true if at least one rule was added successfully.
211
+ *
212
+ * @param ptype the policy type, can be "p", "p2", "p3", ..
213
+ * @param rules the "p" policy rules.
214
+ * @return succeeds or not.
215
+ */
216
+ addNamedPoliciesEx(ptype: string, rules: string[][]): Promise<boolean>;
198
217
  /**
199
218
  * updatePolicy updates an authorization rule from the current policy.
200
219
  * If the rule not exists, the function returns false.
@@ -318,6 +337,25 @@ export declare class ManagementEnforcer extends InternalEnforcer {
318
337
  * @return succeeds or not.
319
338
  */
320
339
  addNamedGroupingPolicies(ptype: string, rules: string[][]): Promise<boolean>;
340
+ /**
341
+ * addGroupingPoliciesEx adds role inheritance rules to the current policy.
342
+ * If a rule already exists, the function will skip it and continue to add the remaining rules.
343
+ * The function returns true if at least one rule was added successfully.
344
+ *
345
+ * @param rules the "g" policy rules, ptype "g" is implicitly used.
346
+ * @return succeeds or not.
347
+ */
348
+ addGroupingPoliciesEx(rules: string[][]): Promise<boolean>;
349
+ /**
350
+ * addNamedGroupingPoliciesEx adds named role inheritance rules to the current policy.
351
+ * If a rule already exists, the function will skip it and continue to add the remaining rules.
352
+ * The function returns true if at least one rule was added successfully.
353
+ *
354
+ * @param ptype the policy type, can be "g", "g2", "g3", ..
355
+ * @param rules the "g" policy rules.
356
+ * @return succeeds or not.
357
+ */
358
+ addNamedGroupingPoliciesEx(ptype: string, rules: string[][]): Promise<boolean>;
321
359
  /**
322
360
  * removeGroupingPolicy removes a role inheritance rule from the current policy.
323
361
  *
@@ -254,6 +254,29 @@ class ManagementEnforcer extends internalEnforcer_1.InternalEnforcer {
254
254
  async addNamedPolicies(ptype, rules) {
255
255
  return this.addPoliciesInternal('p', ptype, rules, true);
256
256
  }
257
+ /**
258
+ * addPoliciesEx adds authorization rules to the current policy.
259
+ * If a rule already exists, the function will skip it and continue to add the remaining rules.
260
+ * The function returns true if at least one rule was added successfully.
261
+ *
262
+ * @param rules the "p" policy rules, ptype "p" is implicitly used.
263
+ * @return succeeds or not.
264
+ */
265
+ async addPoliciesEx(rules) {
266
+ return this.addNamedPoliciesEx('p', rules);
267
+ }
268
+ /**
269
+ * addNamedPoliciesEx adds authorization rules to the current named policy.
270
+ * If a rule already exists, the function will skip it and continue to add the remaining rules.
271
+ * The function returns true if at least one rule was added successfully.
272
+ *
273
+ * @param ptype the policy type, can be "p", "p2", "p3", ..
274
+ * @param rules the "p" policy rules.
275
+ * @return succeeds or not.
276
+ */
277
+ async addNamedPoliciesEx(ptype, rules) {
278
+ return this.addPoliciesInternalEx('p', ptype, rules, true);
279
+ }
257
280
  /**
258
281
  * updatePolicy updates an authorization rule from the current policy.
259
282
  * If the rule not exists, the function returns false.
@@ -405,6 +428,29 @@ class ManagementEnforcer extends internalEnforcer_1.InternalEnforcer {
405
428
  async addNamedGroupingPolicies(ptype, rules) {
406
429
  return this.addPoliciesInternal('g', ptype, rules, true);
407
430
  }
431
+ /**
432
+ * addGroupingPoliciesEx adds role inheritance rules to the current policy.
433
+ * If a rule already exists, the function will skip it and continue to add the remaining rules.
434
+ * The function returns true if at least one rule was added successfully.
435
+ *
436
+ * @param rules the "g" policy rules, ptype "g" is implicitly used.
437
+ * @return succeeds or not.
438
+ */
439
+ async addGroupingPoliciesEx(rules) {
440
+ return this.addNamedGroupingPoliciesEx('g', rules);
441
+ }
442
+ /**
443
+ * addNamedGroupingPoliciesEx adds named role inheritance rules to the current policy.
444
+ * If a rule already exists, the function will skip it and continue to add the remaining rules.
445
+ * The function returns true if at least one rule was added successfully.
446
+ *
447
+ * @param ptype the policy type, can be "g", "g2", "g3", ..
448
+ * @param rules the "g" policy rules.
449
+ * @return succeeds or not.
450
+ */
451
+ async addNamedGroupingPoliciesEx(ptype, rules) {
452
+ return this.addPoliciesInternalEx('g', ptype, rules, true);
453
+ }
408
454
  /**
409
455
  * removeGroupingPolicy removes a role inheritance rule from the current policy.
410
456
  *
@@ -8,6 +8,12 @@ export declare class InternalEnforcer extends CoreEnforcer {
8
8
  */
9
9
  protected addPolicyInternal(sec: string, ptype: string, rule: string[], useWatcher: boolean): Promise<boolean>;
10
10
  protected addPoliciesInternal(sec: string, ptype: string, rules: string[][], useWatcher: boolean): Promise<boolean>;
11
+ /**
12
+ * addPoliciesInternalEx adds rules to the current policy.
13
+ * Unlike addPoliciesInternal, this method will filter out rules that already exist
14
+ * and continue to add the remaining rules instead of returning false immediately.
15
+ */
16
+ protected addPoliciesInternalEx(sec: string, ptype: string, rules: string[][], useWatcher: boolean): Promise<boolean>;
11
17
  /**
12
18
  * updatePolicyInternal updates a rule from the current policy.
13
19
  */
@@ -91,6 +91,50 @@ export class InternalEnforcer extends CoreEnforcer {
91
91
  }
92
92
  return ok;
93
93
  }
94
+ /**
95
+ * addPoliciesInternalEx adds rules to the current policy.
96
+ * Unlike addPoliciesInternal, this method will filter out rules that already exist
97
+ * and continue to add the remaining rules instead of returning false immediately.
98
+ */
99
+ async addPoliciesInternalEx(sec, ptype, rules, useWatcher) {
100
+ // Filter out existing rules
101
+ const newRules = rules.filter((rule) => !this.model.hasPolicy(sec, ptype, rule));
102
+ // If no new rules to add, return false
103
+ if (newRules.length === 0) {
104
+ return false;
105
+ }
106
+ if (this.autoSave) {
107
+ if ('addPolicies' in this.adapter) {
108
+ try {
109
+ await this.adapter.addPolicies(sec, ptype, newRules);
110
+ }
111
+ catch (e) {
112
+ if (e.message !== 'not implemented') {
113
+ throw e;
114
+ }
115
+ }
116
+ }
117
+ else {
118
+ throw new Error('cannot save policy, the adapter does not implement the BatchAdapter');
119
+ }
120
+ }
121
+ if (useWatcher) {
122
+ if (this.autoNotifyWatcher) {
123
+ // error intentionally ignored
124
+ if (this.watcherEx) {
125
+ this.watcherEx.updateForAddPolicies(sec, ptype, ...newRules);
126
+ }
127
+ else if (this.watcher) {
128
+ this.watcher.update();
129
+ }
130
+ }
131
+ }
132
+ const [ok, effects] = await this.model.addPolicies(sec, ptype, newRules);
133
+ if (sec === 'g' && ok && (effects === null || effects === void 0 ? void 0 : effects.length)) {
134
+ await this.buildIncrementalRoleLinks(PolicyOp.PolicyAdd, ptype, effects);
135
+ }
136
+ return ok;
137
+ }
94
138
  /**
95
139
  * updatePolicyInternal updates a rule from the current policy.
96
140
  */
@@ -195,6 +195,25 @@ export declare class ManagementEnforcer extends InternalEnforcer {
195
195
  * @return succeeds or not.
196
196
  */
197
197
  addNamedPolicies(ptype: string, rules: string[][]): Promise<boolean>;
198
+ /**
199
+ * addPoliciesEx adds authorization rules to the current policy.
200
+ * If a rule already exists, the function will skip it and continue to add the remaining rules.
201
+ * The function returns true if at least one rule was added successfully.
202
+ *
203
+ * @param rules the "p" policy rules, ptype "p" is implicitly used.
204
+ * @return succeeds or not.
205
+ */
206
+ addPoliciesEx(rules: string[][]): Promise<boolean>;
207
+ /**
208
+ * addNamedPoliciesEx adds authorization rules to the current named policy.
209
+ * If a rule already exists, the function will skip it and continue to add the remaining rules.
210
+ * The function returns true if at least one rule was added successfully.
211
+ *
212
+ * @param ptype the policy type, can be "p", "p2", "p3", ..
213
+ * @param rules the "p" policy rules.
214
+ * @return succeeds or not.
215
+ */
216
+ addNamedPoliciesEx(ptype: string, rules: string[][]): Promise<boolean>;
198
217
  /**
199
218
  * updatePolicy updates an authorization rule from the current policy.
200
219
  * If the rule not exists, the function returns false.
@@ -318,6 +337,25 @@ export declare class ManagementEnforcer extends InternalEnforcer {
318
337
  * @return succeeds or not.
319
338
  */
320
339
  addNamedGroupingPolicies(ptype: string, rules: string[][]): Promise<boolean>;
340
+ /**
341
+ * addGroupingPoliciesEx adds role inheritance rules to the current policy.
342
+ * If a rule already exists, the function will skip it and continue to add the remaining rules.
343
+ * The function returns true if at least one rule was added successfully.
344
+ *
345
+ * @param rules the "g" policy rules, ptype "g" is implicitly used.
346
+ * @return succeeds or not.
347
+ */
348
+ addGroupingPoliciesEx(rules: string[][]): Promise<boolean>;
349
+ /**
350
+ * addNamedGroupingPoliciesEx adds named role inheritance rules to the current policy.
351
+ * If a rule already exists, the function will skip it and continue to add the remaining rules.
352
+ * The function returns true if at least one rule was added successfully.
353
+ *
354
+ * @param ptype the policy type, can be "g", "g2", "g3", ..
355
+ * @param rules the "g" policy rules.
356
+ * @return succeeds or not.
357
+ */
358
+ addNamedGroupingPoliciesEx(ptype: string, rules: string[][]): Promise<boolean>;
321
359
  /**
322
360
  * removeGroupingPolicy removes a role inheritance rule from the current policy.
323
361
  *
@@ -251,6 +251,29 @@ export class ManagementEnforcer extends InternalEnforcer {
251
251
  async addNamedPolicies(ptype, rules) {
252
252
  return this.addPoliciesInternal('p', ptype, rules, true);
253
253
  }
254
+ /**
255
+ * addPoliciesEx adds authorization rules to the current policy.
256
+ * If a rule already exists, the function will skip it and continue to add the remaining rules.
257
+ * The function returns true if at least one rule was added successfully.
258
+ *
259
+ * @param rules the "p" policy rules, ptype "p" is implicitly used.
260
+ * @return succeeds or not.
261
+ */
262
+ async addPoliciesEx(rules) {
263
+ return this.addNamedPoliciesEx('p', rules);
264
+ }
265
+ /**
266
+ * addNamedPoliciesEx adds authorization rules to the current named policy.
267
+ * If a rule already exists, the function will skip it and continue to add the remaining rules.
268
+ * The function returns true if at least one rule was added successfully.
269
+ *
270
+ * @param ptype the policy type, can be "p", "p2", "p3", ..
271
+ * @param rules the "p" policy rules.
272
+ * @return succeeds or not.
273
+ */
274
+ async addNamedPoliciesEx(ptype, rules) {
275
+ return this.addPoliciesInternalEx('p', ptype, rules, true);
276
+ }
254
277
  /**
255
278
  * updatePolicy updates an authorization rule from the current policy.
256
279
  * If the rule not exists, the function returns false.
@@ -402,6 +425,29 @@ export class ManagementEnforcer extends InternalEnforcer {
402
425
  async addNamedGroupingPolicies(ptype, rules) {
403
426
  return this.addPoliciesInternal('g', ptype, rules, true);
404
427
  }
428
+ /**
429
+ * addGroupingPoliciesEx adds role inheritance rules to the current policy.
430
+ * If a rule already exists, the function will skip it and continue to add the remaining rules.
431
+ * The function returns true if at least one rule was added successfully.
432
+ *
433
+ * @param rules the "g" policy rules, ptype "g" is implicitly used.
434
+ * @return succeeds or not.
435
+ */
436
+ async addGroupingPoliciesEx(rules) {
437
+ return this.addNamedGroupingPoliciesEx('g', rules);
438
+ }
439
+ /**
440
+ * addNamedGroupingPoliciesEx adds named role inheritance rules to the current policy.
441
+ * If a rule already exists, the function will skip it and continue to add the remaining rules.
442
+ * The function returns true if at least one rule was added successfully.
443
+ *
444
+ * @param ptype the policy type, can be "g", "g2", "g3", ..
445
+ * @param rules the "g" policy rules.
446
+ * @return succeeds or not.
447
+ */
448
+ async addNamedGroupingPoliciesEx(ptype, rules) {
449
+ return this.addPoliciesInternalEx('g', ptype, rules, true);
450
+ }
405
451
  /**
406
452
  * removeGroupingPolicy removes a role inheritance rule from the current policy.
407
453
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "casbin",
3
- "version": "5.45.2",
3
+ "version": "5.46.0",
4
4
  "description": "An authorization library that supports access control models like ACL, RBAC, ABAC in Node.JS",
5
5
  "main": "lib/cjs/index.js",
6
6
  "typings": "lib/cjs/index.d.ts",