casbin 5.16.0 → 5.18.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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [5.18.0](https://github.com/casbin/node-casbin/compare/v5.17.0...v5.18.0) (2022-09-14)
2
+
3
+
4
+ ### Features
5
+
6
+ * add management api for watcherEx callbacks ([#384](https://github.com/casbin/node-casbin/issues/384)) ([591233f](https://github.com/casbin/node-casbin/commit/591233f4b80dec422a7dc299c605f7a0e22dba9f))
7
+
8
+ # [5.17.0](https://github.com/casbin/node-casbin/compare/v5.16.0...v5.17.0) (2022-08-18)
9
+
10
+
11
+ ### Features
12
+
13
+ * add WatcherEx ([#381](https://github.com/casbin/node-casbin/issues/381)) ([10d7086](https://github.com/casbin/node-casbin/commit/10d7086c810ff18d9a3c792a3ec1173744bceeef))
14
+
1
15
  # [5.16.0](https://github.com/casbin/node-casbin/compare/v5.15.2...v5.16.0) (2022-08-11)
2
16
 
3
17
 
@@ -1,6 +1,6 @@
1
1
  import { Effector } from './effect';
2
2
  import { FunctionMap, Model, PolicyOp } from './model';
3
- import { Adapter, FilteredAdapter, Watcher, BatchAdapter, UpdatableAdapter } from './persist';
3
+ import { Adapter, FilteredAdapter, Watcher, BatchAdapter, UpdatableAdapter, WatcherEx } from './persist';
4
4
  import { RoleManager } from './rbac';
5
5
  import { MatchingFunc } from './rbac';
6
6
  /**
@@ -14,6 +14,7 @@ export declare class CoreEnforcer {
14
14
  private matcherMap;
15
15
  protected adapter: UpdatableAdapter | FilteredAdapter | Adapter | BatchAdapter;
16
16
  protected watcher: Watcher | null;
17
+ protected watcherEx: WatcherEx | null;
17
18
  protected rmMap: Map<string, RoleManager>;
18
19
  protected enabled: boolean;
19
20
  protected autoSave: boolean;
@@ -56,6 +57,12 @@ export declare class CoreEnforcer {
56
57
  * @param watcher the watcher.
57
58
  */
58
59
  setWatcher(watcher: Watcher): void;
60
+ /**
61
+ * setWatcherEx sets the current watcherEx.
62
+ *
63
+ * @param watcherEx the watcherEx.
64
+ */
65
+ setWatcherEx(watcherEx: WatcherEx): void;
59
66
  /**
60
67
  * setRoleManager sets the current role manager.
61
68
  *
@@ -29,6 +29,7 @@ class CoreEnforcer {
29
29
  this.eft = new effect_1.DefaultEffector();
30
30
  this.matcherMap = new Map();
31
31
  this.watcher = null;
32
+ this.watcherEx = null;
32
33
  this.enabled = true;
33
34
  this.autoSave = true;
34
35
  this.autoBuildRoleLinks = true;
@@ -96,6 +97,14 @@ class CoreEnforcer {
96
97
  this.watcher = watcher;
97
98
  watcher.setUpdateCallback(async () => await this.loadPolicy());
98
99
  }
100
+ /**
101
+ * setWatcherEx sets the current watcherEx.
102
+ *
103
+ * @param watcherEx the watcherEx.
104
+ */
105
+ setWatcherEx(watcherEx) {
106
+ this.watcherEx = watcherEx;
107
+ }
99
108
  /**
100
109
  * setRoleManager sets the current role manager.
101
110
  *
@@ -215,7 +224,10 @@ class CoreEnforcer {
215
224
  if (!flag) {
216
225
  return false;
217
226
  }
218
- if (this.watcher) {
227
+ if (this.watcherEx) {
228
+ return await this.watcherEx.updateForSavePolicy(this.model);
229
+ }
230
+ else if (this.watcher) {
219
231
  return await this.watcher.update();
220
232
  }
221
233
  return true;
@@ -6,19 +6,19 @@ export declare class InternalEnforcer extends CoreEnforcer {
6
6
  /**
7
7
  * addPolicyInternal adds a rule to the current policy.
8
8
  */
9
- addPolicyInternal(sec: string, ptype: string, rule: string[]): Promise<boolean>;
10
- addPoliciesInternal(sec: string, ptype: string, rules: string[][]): Promise<boolean>;
9
+ protected addPolicyInternal(sec: string, ptype: string, rule: string[], useWatcher: boolean): Promise<boolean>;
10
+ protected addPoliciesInternal(sec: string, ptype: string, rules: string[][], useWatcher: boolean): Promise<boolean>;
11
11
  /**
12
12
  * updatePolicyInternal updates a rule from the current policy.
13
13
  */
14
- updatePolicyInternal(sec: string, ptype: string, oldRule: string[], newRule: string[]): Promise<boolean>;
14
+ protected updatePolicyInternal(sec: string, ptype: string, oldRule: string[], newRule: string[], useWatcher: boolean): Promise<boolean>;
15
15
  /**
16
16
  * removePolicyInternal removes a rule from the current policy.
17
17
  */
18
- removePolicyInternal(sec: string, ptype: string, rule: string[]): Promise<boolean>;
19
- removePoliciesInternal(sec: string, ptype: string, rules: string[][]): Promise<boolean>;
18
+ protected removePolicyInternal(sec: string, ptype: string, rule: string[], useWatcher: boolean): Promise<boolean>;
19
+ protected removePoliciesInternal(sec: string, ptype: string, rules: string[][], useWatcher: boolean): Promise<boolean>;
20
20
  /**
21
21
  * removeFilteredPolicyInternal removes rules based on field filters from the current policy.
22
22
  */
23
- removeFilteredPolicyInternal(sec: string, ptype: string, fieldIndex: number, fieldValues: string[]): Promise<boolean>;
23
+ protected removeFilteredPolicyInternal(sec: string, ptype: string, fieldIndex: number, fieldValues: string[], useWatcher: boolean): Promise<boolean>;
24
24
  }
@@ -23,7 +23,7 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
23
23
  /**
24
24
  * addPolicyInternal adds a rule to the current policy.
25
25
  */
26
- async addPolicyInternal(sec, ptype, rule) {
26
+ async addPolicyInternal(sec, ptype, rule, useWatcher) {
27
27
  if (this.model.hasPolicy(sec, ptype, rule)) {
28
28
  return false;
29
29
  }
@@ -37,9 +37,16 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
37
37
  }
38
38
  }
39
39
  }
40
- if (this.watcher && this.autoNotifyWatcher) {
41
- // error intentionally ignored
42
- this.watcher.update();
40
+ if (useWatcher) {
41
+ if (this.autoNotifyWatcher) {
42
+ // error intentionally ignored
43
+ if (this.watcherEx) {
44
+ this.watcherEx.updateForAddPolicy(sec, ptype, ...rule);
45
+ }
46
+ else if (this.watcher) {
47
+ this.watcher.update();
48
+ }
49
+ }
43
50
  }
44
51
  const ok = this.model.addPolicy(sec, ptype, rule);
45
52
  if (sec === 'g' && ok) {
@@ -49,7 +56,7 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
49
56
  }
50
57
  // addPolicies adds rules to the current policy.
51
58
  // removePolicies removes rules from the current policy.
52
- async addPoliciesInternal(sec, ptype, rules) {
59
+ async addPoliciesInternal(sec, ptype, rules, useWatcher) {
53
60
  for (const rule of rules) {
54
61
  if (this.model.hasPolicy(sec, ptype, rule)) {
55
62
  return false;
@@ -70,9 +77,16 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
70
77
  throw new Error('cannot to save policy, the adapter does not implement the BatchAdapter');
71
78
  }
72
79
  }
73
- if (this.watcher && this.autoNotifyWatcher) {
74
- // error intentionally ignored
75
- this.watcher.update();
80
+ if (useWatcher) {
81
+ if (this.autoNotifyWatcher) {
82
+ // error intentionally ignored
83
+ if (this.watcherEx) {
84
+ this.watcherEx.updateForAddPolicies(sec, ptype, ...rules);
85
+ }
86
+ else if (this.watcher) {
87
+ this.watcher.update();
88
+ }
89
+ }
76
90
  }
77
91
  const [ok, effects] = await this.model.addPolicies(sec, ptype, rules);
78
92
  if (sec === 'g' && ok && (effects === null || effects === void 0 ? void 0 : effects.length)) {
@@ -83,7 +97,7 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
83
97
  /**
84
98
  * updatePolicyInternal updates a rule from the current policy.
85
99
  */
86
- async updatePolicyInternal(sec, ptype, oldRule, newRule) {
100
+ async updatePolicyInternal(sec, ptype, oldRule, newRule, useWatcher) {
87
101
  if (!this.model.hasPolicy(sec, ptype, oldRule)) {
88
102
  return false;
89
103
  }
@@ -102,10 +116,12 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
102
116
  throw new Error('cannot to update policy, the adapter does not implement the UpdatableAdapter');
103
117
  }
104
118
  }
105
- if (this.watcher && this.autoNotifyWatcher) {
106
- // In fact I think it should wait for the respond, but they implement add_policy() like this
107
- // error intentionally ignored
108
- this.watcher.update();
119
+ if (useWatcher) {
120
+ if (this.watcher && this.autoNotifyWatcher) {
121
+ // In fact I think it should wait for the respond, but they implement add_policy() like this
122
+ // error intentionally ignored
123
+ this.watcher.update();
124
+ }
109
125
  }
110
126
  const ok = this.model.updatePolicy(sec, ptype, oldRule, newRule);
111
127
  if (sec === 'g' && ok) {
@@ -117,7 +133,7 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
117
133
  /**
118
134
  * removePolicyInternal removes a rule from the current policy.
119
135
  */
120
- async removePolicyInternal(sec, ptype, rule) {
136
+ async removePolicyInternal(sec, ptype, rule, useWatcher) {
121
137
  if (!this.model.hasPolicy(sec, ptype, rule)) {
122
138
  return false;
123
139
  }
@@ -131,9 +147,16 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
131
147
  }
132
148
  }
133
149
  }
134
- if (this.watcher && this.autoNotifyWatcher) {
135
- // error intentionally ignored
136
- this.watcher.update();
150
+ if (useWatcher) {
151
+ if (this.watcher && this.autoNotifyWatcher) {
152
+ // error intentionally ignored
153
+ if (this.watcherEx) {
154
+ this.watcherEx.updateForRemovePolicy(sec, ptype, ...rule);
155
+ }
156
+ else if (this.watcher) {
157
+ this.watcher.update();
158
+ }
159
+ }
137
160
  }
138
161
  const ok = await this.model.removePolicy(sec, ptype, rule);
139
162
  if (sec === 'g' && ok) {
@@ -142,7 +165,7 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
142
165
  return ok;
143
166
  }
144
167
  // removePolicies removes rules from the current policy.
145
- async removePoliciesInternal(sec, ptype, rules) {
168
+ async removePoliciesInternal(sec, ptype, rules, useWatcher) {
146
169
  for (const rule of rules) {
147
170
  if (!this.model.hasPolicy(sec, ptype, rule)) {
148
171
  return false;
@@ -163,9 +186,16 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
163
186
  throw new Error('cannot to save policy, the adapter does not implement the BatchAdapter');
164
187
  }
165
188
  }
166
- if (this.watcher && this.autoNotifyWatcher) {
167
- // error intentionally ignored
168
- this.watcher.update();
189
+ if (useWatcher) {
190
+ if (this.watcher && this.autoNotifyWatcher) {
191
+ // error intentionally ignored
192
+ if (this.watcherEx) {
193
+ this.watcherEx.updateForRemovePolicies(sec, ptype, ...rules);
194
+ }
195
+ else if (this.watcher) {
196
+ this.watcher.update();
197
+ }
198
+ }
169
199
  }
170
200
  const [ok, effects] = this.model.removePolicies(sec, ptype, rules);
171
201
  if (sec === 'g' && ok && (effects === null || effects === void 0 ? void 0 : effects.length)) {
@@ -176,7 +206,7 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
176
206
  /**
177
207
  * removeFilteredPolicyInternal removes rules based on field filters from the current policy.
178
208
  */
179
- async removeFilteredPolicyInternal(sec, ptype, fieldIndex, fieldValues) {
209
+ async removeFilteredPolicyInternal(sec, ptype, fieldIndex, fieldValues, useWatcher) {
180
210
  if (this.adapter && this.autoSave) {
181
211
  try {
182
212
  await this.adapter.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);
@@ -187,9 +217,16 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
187
217
  }
188
218
  }
189
219
  }
190
- if (this.watcher && this.autoNotifyWatcher) {
191
- // error intentionally ignored
192
- this.watcher.update();
220
+ if (useWatcher) {
221
+ if (this.watcher && this.autoNotifyWatcher) {
222
+ // error intentionally ignored
223
+ if (this.watcherEx) {
224
+ this.watcherEx.updateForRemoveFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);
225
+ }
226
+ else if (this.watcher) {
227
+ this.watcher.update();
228
+ }
229
+ }
193
230
  }
194
231
  const [ok, effects] = this.model.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);
195
232
  if (sec === 'g' && ok && (effects === null || effects === void 0 ? void 0 : effects.length)) {
@@ -373,4 +373,10 @@ export declare class ManagementEnforcer extends InternalEnforcer {
373
373
  * @param func function
374
374
  */
375
375
  addFunction(name: string, func: MatchingFunction): Promise<void>;
376
+ selfAddPolicy(sec: string, ptype: string, rule: string[]): Promise<boolean>;
377
+ selfRemovePolicy(sec: string, ptype: string, rule: string[]): Promise<boolean>;
378
+ selfRemoveFilteredPolicy(sec: string, ptype: string, fieldIndex: number, fieldValues: string[]): Promise<boolean>;
379
+ selfUpdatePolicy(sec: string, ptype: string, oldRule: string[], newRule: string[]): Promise<boolean>;
380
+ selfAddPolicies(sec: string, ptype: string, rule: string[][]): Promise<boolean>;
381
+ selfRemovePolicies(sec: string, ptype: string, rule: string[][]): Promise<boolean>;
376
382
  }
@@ -240,7 +240,7 @@ class ManagementEnforcer extends internalEnforcer_1.InternalEnforcer {
240
240
  * @return succeeds or not.
241
241
  */
242
242
  async addNamedPolicy(ptype, ...params) {
243
- return this.addPolicyInternal('p', ptype, params);
243
+ return this.addPolicyInternal('p', ptype, params, true);
244
244
  }
245
245
  /**
246
246
  * addNamedPolicies adds authorization rules to the current named policy.
@@ -252,7 +252,7 @@ class ManagementEnforcer extends internalEnforcer_1.InternalEnforcer {
252
252
  * @return succeeds or not.
253
253
  */
254
254
  async addNamedPolicies(ptype, rules) {
255
- return this.addPoliciesInternal('p', ptype, rules);
255
+ return this.addPoliciesInternal('p', ptype, rules, true);
256
256
  }
257
257
  /**
258
258
  * updatePolicy updates an authorization rule from the current policy.
@@ -277,7 +277,7 @@ class ManagementEnforcer extends internalEnforcer_1.InternalEnforcer {
277
277
  * @return succeeds or not.
278
278
  */
279
279
  async updateNamedPolicy(ptype, oldRule, newRule) {
280
- return this.updatePolicyInternal('p', ptype, oldRule, newRule);
280
+ return this.updatePolicyInternal('p', ptype, oldRule, newRule, true);
281
281
  }
282
282
  /**
283
283
  * removePolicy removes an authorization rule from the current policy.
@@ -316,7 +316,7 @@ class ManagementEnforcer extends internalEnforcer_1.InternalEnforcer {
316
316
  * @return succeeds or not.
317
317
  */
318
318
  async removeNamedPolicy(ptype, ...params) {
319
- return this.removePolicyInternal('p', ptype, params);
319
+ return this.removePolicyInternal('p', ptype, params, true);
320
320
  }
321
321
  /**
322
322
  * removeNamedPolicies removes authorization rules from the current named policy.
@@ -326,7 +326,7 @@ class ManagementEnforcer extends internalEnforcer_1.InternalEnforcer {
326
326
  * @return succeeds or not.
327
327
  */
328
328
  async removeNamedPolicies(ptype, rules) {
329
- return this.removePoliciesInternal('p', ptype, rules);
329
+ return this.removePoliciesInternal('p', ptype, rules, true);
330
330
  }
331
331
  /**
332
332
  * removeFilteredNamedPolicy removes an authorization rule from the current named policy, field filters can be specified.
@@ -338,7 +338,7 @@ class ManagementEnforcer extends internalEnforcer_1.InternalEnforcer {
338
338
  * @return succeeds or not.
339
339
  */
340
340
  async removeFilteredNamedPolicy(ptype, fieldIndex, ...fieldValues) {
341
- return this.removeFilteredPolicyInternal('p', ptype, fieldIndex, fieldValues);
341
+ return this.removeFilteredPolicyInternal('p', ptype, fieldIndex, fieldValues, true);
342
342
  }
343
343
  /**
344
344
  * hasGroupingPolicy determines whether a role inheritance rule exists.
@@ -391,7 +391,7 @@ class ManagementEnforcer extends internalEnforcer_1.InternalEnforcer {
391
391
  * @return succeeds or not.
392
392
  */
393
393
  async addNamedGroupingPolicy(ptype, ...params) {
394
- return this.addPolicyInternal('g', ptype, params);
394
+ return this.addPolicyInternal('g', ptype, params, true);
395
395
  }
396
396
  /**
397
397
  * addNamedGroupingPolicies adds named role inheritance rules to the current policy.
@@ -403,7 +403,7 @@ class ManagementEnforcer extends internalEnforcer_1.InternalEnforcer {
403
403
  * @return succeeds or not.
404
404
  */
405
405
  async addNamedGroupingPolicies(ptype, rules) {
406
- return this.addPoliciesInternal('g', ptype, rules);
406
+ return this.addPoliciesInternal('g', ptype, rules, true);
407
407
  }
408
408
  /**
409
409
  * removeGroupingPolicy removes a role inheritance rule from the current policy.
@@ -442,7 +442,7 @@ class ManagementEnforcer extends internalEnforcer_1.InternalEnforcer {
442
442
  * @return succeeds or not.
443
443
  */
444
444
  async removeNamedGroupingPolicy(ptype, ...params) {
445
- return this.removePolicyInternal('g', ptype, params);
445
+ return this.removePolicyInternal('g', ptype, params, true);
446
446
  }
447
447
  /**
448
448
  * removeNamedGroupingPolicies removes role inheritance rules from the current named policy.
@@ -452,7 +452,7 @@ class ManagementEnforcer extends internalEnforcer_1.InternalEnforcer {
452
452
  * @return succeeds or not.
453
453
  */
454
454
  async removeNamedGroupingPolicies(ptype, rules) {
455
- return this.removePoliciesInternal('g', ptype, rules);
455
+ return this.removePoliciesInternal('g', ptype, rules, true);
456
456
  }
457
457
  /**
458
458
  * removeFilteredNamedGroupingPolicy removes a role inheritance rule from the current named policy, field filters can be specified.
@@ -464,7 +464,7 @@ class ManagementEnforcer extends internalEnforcer_1.InternalEnforcer {
464
464
  * @return succeeds or not.
465
465
  */
466
466
  async removeFilteredNamedGroupingPolicy(ptype, fieldIndex, ...fieldValues) {
467
- return this.removeFilteredPolicyInternal('g', ptype, fieldIndex, fieldValues);
467
+ return this.removeFilteredPolicyInternal('g', ptype, fieldIndex, fieldValues, true);
468
468
  }
469
469
  /**
470
470
  * addFunction adds a customized function.
@@ -474,5 +474,23 @@ class ManagementEnforcer extends internalEnforcer_1.InternalEnforcer {
474
474
  async addFunction(name, func) {
475
475
  this.fm.addFunction(name, func);
476
476
  }
477
+ async selfAddPolicy(sec, ptype, rule) {
478
+ return this.addPolicyInternal(sec, ptype, rule, false);
479
+ }
480
+ async selfRemovePolicy(sec, ptype, rule) {
481
+ return this.removePolicyInternal(sec, ptype, rule, false);
482
+ }
483
+ async selfRemoveFilteredPolicy(sec, ptype, fieldIndex, fieldValues) {
484
+ return this.removeFilteredPolicyInternal(sec, ptype, fieldIndex, fieldValues, false);
485
+ }
486
+ async selfUpdatePolicy(sec, ptype, oldRule, newRule) {
487
+ return this.updatePolicyInternal(sec, ptype, oldRule, newRule, false);
488
+ }
489
+ async selfAddPolicies(sec, ptype, rule) {
490
+ return this.addPoliciesInternal(sec, ptype, rule, false);
491
+ }
492
+ async selfRemovePolicies(sec, ptype, rule) {
493
+ return this.removePoliciesInternal(sec, ptype, rule, false);
494
+ }
477
495
  }
478
496
  exports.ManagementEnforcer = ManagementEnforcer;
@@ -3,6 +3,7 @@ export * from './fileAdapter';
3
3
  export * from './stringAdapter';
4
4
  export * from './helper';
5
5
  export * from './watcher';
6
+ export * from './watcherEx';
6
7
  export * from './filteredAdapter';
7
8
  export * from './defaultFilteredAdapter';
8
9
  export * from './batchAdapter';
@@ -15,6 +15,7 @@ __exportStar(require("./fileAdapter"), exports);
15
15
  __exportStar(require("./stringAdapter"), exports);
16
16
  __exportStar(require("./helper"), exports);
17
17
  __exportStar(require("./watcher"), exports);
18
+ __exportStar(require("./watcherEx"), exports);
18
19
  __exportStar(require("./filteredAdapter"), exports);
19
20
  __exportStar(require("./defaultFilteredAdapter"), exports);
20
21
  __exportStar(require("./batchAdapter"), exports);
@@ -0,0 +1,9 @@
1
+ import { Model } from '../model';
2
+ export interface WatcherEx {
3
+ updateForAddPolicy(sec: string, ptype: string, ...params: string[]): Promise<void>;
4
+ updateForRemovePolicy(sec: string, ptype: string, ...params: string[]): Promise<void>;
5
+ updateForRemoveFilteredPolicy(sec: string, ptype: string, fieldIndex: number, ...fieldValues: string[]): Promise<void>;
6
+ updateForSavePolicy(model: Model): Promise<boolean>;
7
+ updateForAddPolicies(sec: string, ptype: string, ...rules: string[][]): Promise<void>;
8
+ updateForRemovePolicies(sec: string, ptype: string, ...rules: string[][]): Promise<void>;
9
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ // Copyright 2022 The Casbin Authors. All Rights Reserved.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -340,7 +340,7 @@ class SyncedEnforcer extends enforcer_1.Enforcer {
340
340
  */
341
341
  async removeNamedPolicy(ptype, ...params) {
342
342
  await this.lock.acquireAsync();
343
- return this.removePolicyInternal('p', ptype, params).finally(() => this.lock.release());
343
+ return this.removePolicyInternal('p', ptype, params, true).finally(() => this.lock.release());
344
344
  }
345
345
  /**
346
346
  * removeFilteredNamedPolicy removes an authorization rule from the current named policy, field filters can be specified.
@@ -1,6 +1,6 @@
1
1
  import { Effector } from './effect';
2
2
  import { FunctionMap, Model, PolicyOp } from './model';
3
- import { Adapter, FilteredAdapter, Watcher, BatchAdapter, UpdatableAdapter } from './persist';
3
+ import { Adapter, FilteredAdapter, Watcher, BatchAdapter, UpdatableAdapter, WatcherEx } from './persist';
4
4
  import { RoleManager } from './rbac';
5
5
  import { MatchingFunc } from './rbac';
6
6
  /**
@@ -14,6 +14,7 @@ export declare class CoreEnforcer {
14
14
  private matcherMap;
15
15
  protected adapter: UpdatableAdapter | FilteredAdapter | Adapter | BatchAdapter;
16
16
  protected watcher: Watcher | null;
17
+ protected watcherEx: WatcherEx | null;
17
18
  protected rmMap: Map<string, RoleManager>;
18
19
  protected enabled: boolean;
19
20
  protected autoSave: boolean;
@@ -56,6 +57,12 @@ export declare class CoreEnforcer {
56
57
  * @param watcher the watcher.
57
58
  */
58
59
  setWatcher(watcher: Watcher): void;
60
+ /**
61
+ * setWatcherEx sets the current watcherEx.
62
+ *
63
+ * @param watcherEx the watcherEx.
64
+ */
65
+ setWatcherEx(watcherEx: WatcherEx): void;
59
66
  /**
60
67
  * setRoleManager sets the current role manager.
61
68
  *
@@ -26,6 +26,7 @@ export class CoreEnforcer {
26
26
  this.eft = new DefaultEffector();
27
27
  this.matcherMap = new Map();
28
28
  this.watcher = null;
29
+ this.watcherEx = null;
29
30
  this.enabled = true;
30
31
  this.autoSave = true;
31
32
  this.autoBuildRoleLinks = true;
@@ -93,6 +94,14 @@ export class CoreEnforcer {
93
94
  this.watcher = watcher;
94
95
  watcher.setUpdateCallback(async () => await this.loadPolicy());
95
96
  }
97
+ /**
98
+ * setWatcherEx sets the current watcherEx.
99
+ *
100
+ * @param watcherEx the watcherEx.
101
+ */
102
+ setWatcherEx(watcherEx) {
103
+ this.watcherEx = watcherEx;
104
+ }
96
105
  /**
97
106
  * setRoleManager sets the current role manager.
98
107
  *
@@ -212,7 +221,10 @@ export class CoreEnforcer {
212
221
  if (!flag) {
213
222
  return false;
214
223
  }
215
- if (this.watcher) {
224
+ if (this.watcherEx) {
225
+ return await this.watcherEx.updateForSavePolicy(this.model);
226
+ }
227
+ else if (this.watcher) {
216
228
  return await this.watcher.update();
217
229
  }
218
230
  return true;
@@ -6,19 +6,19 @@ export declare class InternalEnforcer extends CoreEnforcer {
6
6
  /**
7
7
  * addPolicyInternal adds a rule to the current policy.
8
8
  */
9
- addPolicyInternal(sec: string, ptype: string, rule: string[]): Promise<boolean>;
10
- addPoliciesInternal(sec: string, ptype: string, rules: string[][]): Promise<boolean>;
9
+ protected addPolicyInternal(sec: string, ptype: string, rule: string[], useWatcher: boolean): Promise<boolean>;
10
+ protected addPoliciesInternal(sec: string, ptype: string, rules: string[][], useWatcher: boolean): Promise<boolean>;
11
11
  /**
12
12
  * updatePolicyInternal updates a rule from the current policy.
13
13
  */
14
- updatePolicyInternal(sec: string, ptype: string, oldRule: string[], newRule: string[]): Promise<boolean>;
14
+ protected updatePolicyInternal(sec: string, ptype: string, oldRule: string[], newRule: string[], useWatcher: boolean): Promise<boolean>;
15
15
  /**
16
16
  * removePolicyInternal removes a rule from the current policy.
17
17
  */
18
- removePolicyInternal(sec: string, ptype: string, rule: string[]): Promise<boolean>;
19
- removePoliciesInternal(sec: string, ptype: string, rules: string[][]): Promise<boolean>;
18
+ protected removePolicyInternal(sec: string, ptype: string, rule: string[], useWatcher: boolean): Promise<boolean>;
19
+ protected removePoliciesInternal(sec: string, ptype: string, rules: string[][], useWatcher: boolean): Promise<boolean>;
20
20
  /**
21
21
  * removeFilteredPolicyInternal removes rules based on field filters from the current policy.
22
22
  */
23
- removeFilteredPolicyInternal(sec: string, ptype: string, fieldIndex: number, fieldValues: string[]): Promise<boolean>;
23
+ protected removeFilteredPolicyInternal(sec: string, ptype: string, fieldIndex: number, fieldValues: string[], useWatcher: boolean): Promise<boolean>;
24
24
  }
@@ -20,7 +20,7 @@ export class InternalEnforcer extends CoreEnforcer {
20
20
  /**
21
21
  * addPolicyInternal adds a rule to the current policy.
22
22
  */
23
- async addPolicyInternal(sec, ptype, rule) {
23
+ async addPolicyInternal(sec, ptype, rule, useWatcher) {
24
24
  if (this.model.hasPolicy(sec, ptype, rule)) {
25
25
  return false;
26
26
  }
@@ -34,9 +34,16 @@ export class InternalEnforcer extends CoreEnforcer {
34
34
  }
35
35
  }
36
36
  }
37
- if (this.watcher && this.autoNotifyWatcher) {
38
- // error intentionally ignored
39
- this.watcher.update();
37
+ if (useWatcher) {
38
+ if (this.autoNotifyWatcher) {
39
+ // error intentionally ignored
40
+ if (this.watcherEx) {
41
+ this.watcherEx.updateForAddPolicy(sec, ptype, ...rule);
42
+ }
43
+ else if (this.watcher) {
44
+ this.watcher.update();
45
+ }
46
+ }
40
47
  }
41
48
  const ok = this.model.addPolicy(sec, ptype, rule);
42
49
  if (sec === 'g' && ok) {
@@ -46,7 +53,7 @@ export class InternalEnforcer extends CoreEnforcer {
46
53
  }
47
54
  // addPolicies adds rules to the current policy.
48
55
  // removePolicies removes rules from the current policy.
49
- async addPoliciesInternal(sec, ptype, rules) {
56
+ async addPoliciesInternal(sec, ptype, rules, useWatcher) {
50
57
  for (const rule of rules) {
51
58
  if (this.model.hasPolicy(sec, ptype, rule)) {
52
59
  return false;
@@ -67,9 +74,16 @@ export class InternalEnforcer extends CoreEnforcer {
67
74
  throw new Error('cannot to save policy, the adapter does not implement the BatchAdapter');
68
75
  }
69
76
  }
70
- if (this.watcher && this.autoNotifyWatcher) {
71
- // error intentionally ignored
72
- this.watcher.update();
77
+ if (useWatcher) {
78
+ if (this.autoNotifyWatcher) {
79
+ // error intentionally ignored
80
+ if (this.watcherEx) {
81
+ this.watcherEx.updateForAddPolicies(sec, ptype, ...rules);
82
+ }
83
+ else if (this.watcher) {
84
+ this.watcher.update();
85
+ }
86
+ }
73
87
  }
74
88
  const [ok, effects] = await this.model.addPolicies(sec, ptype, rules);
75
89
  if (sec === 'g' && ok && (effects === null || effects === void 0 ? void 0 : effects.length)) {
@@ -80,7 +94,7 @@ export class InternalEnforcer extends CoreEnforcer {
80
94
  /**
81
95
  * updatePolicyInternal updates a rule from the current policy.
82
96
  */
83
- async updatePolicyInternal(sec, ptype, oldRule, newRule) {
97
+ async updatePolicyInternal(sec, ptype, oldRule, newRule, useWatcher) {
84
98
  if (!this.model.hasPolicy(sec, ptype, oldRule)) {
85
99
  return false;
86
100
  }
@@ -99,10 +113,12 @@ export class InternalEnforcer extends CoreEnforcer {
99
113
  throw new Error('cannot to update policy, the adapter does not implement the UpdatableAdapter');
100
114
  }
101
115
  }
102
- if (this.watcher && this.autoNotifyWatcher) {
103
- // In fact I think it should wait for the respond, but they implement add_policy() like this
104
- // error intentionally ignored
105
- this.watcher.update();
116
+ if (useWatcher) {
117
+ if (this.watcher && this.autoNotifyWatcher) {
118
+ // In fact I think it should wait for the respond, but they implement add_policy() like this
119
+ // error intentionally ignored
120
+ this.watcher.update();
121
+ }
106
122
  }
107
123
  const ok = this.model.updatePolicy(sec, ptype, oldRule, newRule);
108
124
  if (sec === 'g' && ok) {
@@ -114,7 +130,7 @@ export class InternalEnforcer extends CoreEnforcer {
114
130
  /**
115
131
  * removePolicyInternal removes a rule from the current policy.
116
132
  */
117
- async removePolicyInternal(sec, ptype, rule) {
133
+ async removePolicyInternal(sec, ptype, rule, useWatcher) {
118
134
  if (!this.model.hasPolicy(sec, ptype, rule)) {
119
135
  return false;
120
136
  }
@@ -128,9 +144,16 @@ export class InternalEnforcer extends CoreEnforcer {
128
144
  }
129
145
  }
130
146
  }
131
- if (this.watcher && this.autoNotifyWatcher) {
132
- // error intentionally ignored
133
- this.watcher.update();
147
+ if (useWatcher) {
148
+ if (this.watcher && this.autoNotifyWatcher) {
149
+ // error intentionally ignored
150
+ if (this.watcherEx) {
151
+ this.watcherEx.updateForRemovePolicy(sec, ptype, ...rule);
152
+ }
153
+ else if (this.watcher) {
154
+ this.watcher.update();
155
+ }
156
+ }
134
157
  }
135
158
  const ok = await this.model.removePolicy(sec, ptype, rule);
136
159
  if (sec === 'g' && ok) {
@@ -139,7 +162,7 @@ export class InternalEnforcer extends CoreEnforcer {
139
162
  return ok;
140
163
  }
141
164
  // removePolicies removes rules from the current policy.
142
- async removePoliciesInternal(sec, ptype, rules) {
165
+ async removePoliciesInternal(sec, ptype, rules, useWatcher) {
143
166
  for (const rule of rules) {
144
167
  if (!this.model.hasPolicy(sec, ptype, rule)) {
145
168
  return false;
@@ -160,9 +183,16 @@ export class InternalEnforcer extends CoreEnforcer {
160
183
  throw new Error('cannot to save policy, the adapter does not implement the BatchAdapter');
161
184
  }
162
185
  }
163
- if (this.watcher && this.autoNotifyWatcher) {
164
- // error intentionally ignored
165
- this.watcher.update();
186
+ if (useWatcher) {
187
+ if (this.watcher && this.autoNotifyWatcher) {
188
+ // error intentionally ignored
189
+ if (this.watcherEx) {
190
+ this.watcherEx.updateForRemovePolicies(sec, ptype, ...rules);
191
+ }
192
+ else if (this.watcher) {
193
+ this.watcher.update();
194
+ }
195
+ }
166
196
  }
167
197
  const [ok, effects] = this.model.removePolicies(sec, ptype, rules);
168
198
  if (sec === 'g' && ok && (effects === null || effects === void 0 ? void 0 : effects.length)) {
@@ -173,7 +203,7 @@ export class InternalEnforcer extends CoreEnforcer {
173
203
  /**
174
204
  * removeFilteredPolicyInternal removes rules based on field filters from the current policy.
175
205
  */
176
- async removeFilteredPolicyInternal(sec, ptype, fieldIndex, fieldValues) {
206
+ async removeFilteredPolicyInternal(sec, ptype, fieldIndex, fieldValues, useWatcher) {
177
207
  if (this.adapter && this.autoSave) {
178
208
  try {
179
209
  await this.adapter.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);
@@ -184,9 +214,16 @@ export class InternalEnforcer extends CoreEnforcer {
184
214
  }
185
215
  }
186
216
  }
187
- if (this.watcher && this.autoNotifyWatcher) {
188
- // error intentionally ignored
189
- this.watcher.update();
217
+ if (useWatcher) {
218
+ if (this.watcher && this.autoNotifyWatcher) {
219
+ // error intentionally ignored
220
+ if (this.watcherEx) {
221
+ this.watcherEx.updateForRemoveFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);
222
+ }
223
+ else if (this.watcher) {
224
+ this.watcher.update();
225
+ }
226
+ }
190
227
  }
191
228
  const [ok, effects] = this.model.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);
192
229
  if (sec === 'g' && ok && (effects === null || effects === void 0 ? void 0 : effects.length)) {
@@ -373,4 +373,10 @@ export declare class ManagementEnforcer extends InternalEnforcer {
373
373
  * @param func function
374
374
  */
375
375
  addFunction(name: string, func: MatchingFunction): Promise<void>;
376
+ selfAddPolicy(sec: string, ptype: string, rule: string[]): Promise<boolean>;
377
+ selfRemovePolicy(sec: string, ptype: string, rule: string[]): Promise<boolean>;
378
+ selfRemoveFilteredPolicy(sec: string, ptype: string, fieldIndex: number, fieldValues: string[]): Promise<boolean>;
379
+ selfUpdatePolicy(sec: string, ptype: string, oldRule: string[], newRule: string[]): Promise<boolean>;
380
+ selfAddPolicies(sec: string, ptype: string, rule: string[][]): Promise<boolean>;
381
+ selfRemovePolicies(sec: string, ptype: string, rule: string[][]): Promise<boolean>;
376
382
  }
@@ -237,7 +237,7 @@ export class ManagementEnforcer extends InternalEnforcer {
237
237
  * @return succeeds or not.
238
238
  */
239
239
  async addNamedPolicy(ptype, ...params) {
240
- return this.addPolicyInternal('p', ptype, params);
240
+ return this.addPolicyInternal('p', ptype, params, true);
241
241
  }
242
242
  /**
243
243
  * addNamedPolicies adds authorization rules to the current named policy.
@@ -249,7 +249,7 @@ export class ManagementEnforcer extends InternalEnforcer {
249
249
  * @return succeeds or not.
250
250
  */
251
251
  async addNamedPolicies(ptype, rules) {
252
- return this.addPoliciesInternal('p', ptype, rules);
252
+ return this.addPoliciesInternal('p', ptype, rules, true);
253
253
  }
254
254
  /**
255
255
  * updatePolicy updates an authorization rule from the current policy.
@@ -274,7 +274,7 @@ export class ManagementEnforcer extends InternalEnforcer {
274
274
  * @return succeeds or not.
275
275
  */
276
276
  async updateNamedPolicy(ptype, oldRule, newRule) {
277
- return this.updatePolicyInternal('p', ptype, oldRule, newRule);
277
+ return this.updatePolicyInternal('p', ptype, oldRule, newRule, true);
278
278
  }
279
279
  /**
280
280
  * removePolicy removes an authorization rule from the current policy.
@@ -313,7 +313,7 @@ export class ManagementEnforcer extends InternalEnforcer {
313
313
  * @return succeeds or not.
314
314
  */
315
315
  async removeNamedPolicy(ptype, ...params) {
316
- return this.removePolicyInternal('p', ptype, params);
316
+ return this.removePolicyInternal('p', ptype, params, true);
317
317
  }
318
318
  /**
319
319
  * removeNamedPolicies removes authorization rules from the current named policy.
@@ -323,7 +323,7 @@ export class ManagementEnforcer extends InternalEnforcer {
323
323
  * @return succeeds or not.
324
324
  */
325
325
  async removeNamedPolicies(ptype, rules) {
326
- return this.removePoliciesInternal('p', ptype, rules);
326
+ return this.removePoliciesInternal('p', ptype, rules, true);
327
327
  }
328
328
  /**
329
329
  * removeFilteredNamedPolicy removes an authorization rule from the current named policy, field filters can be specified.
@@ -335,7 +335,7 @@ export class ManagementEnforcer extends InternalEnforcer {
335
335
  * @return succeeds or not.
336
336
  */
337
337
  async removeFilteredNamedPolicy(ptype, fieldIndex, ...fieldValues) {
338
- return this.removeFilteredPolicyInternal('p', ptype, fieldIndex, fieldValues);
338
+ return this.removeFilteredPolicyInternal('p', ptype, fieldIndex, fieldValues, true);
339
339
  }
340
340
  /**
341
341
  * hasGroupingPolicy determines whether a role inheritance rule exists.
@@ -388,7 +388,7 @@ export class ManagementEnforcer extends InternalEnforcer {
388
388
  * @return succeeds or not.
389
389
  */
390
390
  async addNamedGroupingPolicy(ptype, ...params) {
391
- return this.addPolicyInternal('g', ptype, params);
391
+ return this.addPolicyInternal('g', ptype, params, true);
392
392
  }
393
393
  /**
394
394
  * addNamedGroupingPolicies adds named role inheritance rules to the current policy.
@@ -400,7 +400,7 @@ export class ManagementEnforcer extends InternalEnforcer {
400
400
  * @return succeeds or not.
401
401
  */
402
402
  async addNamedGroupingPolicies(ptype, rules) {
403
- return this.addPoliciesInternal('g', ptype, rules);
403
+ return this.addPoliciesInternal('g', ptype, rules, true);
404
404
  }
405
405
  /**
406
406
  * removeGroupingPolicy removes a role inheritance rule from the current policy.
@@ -439,7 +439,7 @@ export class ManagementEnforcer extends InternalEnforcer {
439
439
  * @return succeeds or not.
440
440
  */
441
441
  async removeNamedGroupingPolicy(ptype, ...params) {
442
- return this.removePolicyInternal('g', ptype, params);
442
+ return this.removePolicyInternal('g', ptype, params, true);
443
443
  }
444
444
  /**
445
445
  * removeNamedGroupingPolicies removes role inheritance rules from the current named policy.
@@ -449,7 +449,7 @@ export class ManagementEnforcer extends InternalEnforcer {
449
449
  * @return succeeds or not.
450
450
  */
451
451
  async removeNamedGroupingPolicies(ptype, rules) {
452
- return this.removePoliciesInternal('g', ptype, rules);
452
+ return this.removePoliciesInternal('g', ptype, rules, true);
453
453
  }
454
454
  /**
455
455
  * removeFilteredNamedGroupingPolicy removes a role inheritance rule from the current named policy, field filters can be specified.
@@ -461,7 +461,7 @@ export class ManagementEnforcer extends InternalEnforcer {
461
461
  * @return succeeds or not.
462
462
  */
463
463
  async removeFilteredNamedGroupingPolicy(ptype, fieldIndex, ...fieldValues) {
464
- return this.removeFilteredPolicyInternal('g', ptype, fieldIndex, fieldValues);
464
+ return this.removeFilteredPolicyInternal('g', ptype, fieldIndex, fieldValues, true);
465
465
  }
466
466
  /**
467
467
  * addFunction adds a customized function.
@@ -471,4 +471,22 @@ export class ManagementEnforcer extends InternalEnforcer {
471
471
  async addFunction(name, func) {
472
472
  this.fm.addFunction(name, func);
473
473
  }
474
+ async selfAddPolicy(sec, ptype, rule) {
475
+ return this.addPolicyInternal(sec, ptype, rule, false);
476
+ }
477
+ async selfRemovePolicy(sec, ptype, rule) {
478
+ return this.removePolicyInternal(sec, ptype, rule, false);
479
+ }
480
+ async selfRemoveFilteredPolicy(sec, ptype, fieldIndex, fieldValues) {
481
+ return this.removeFilteredPolicyInternal(sec, ptype, fieldIndex, fieldValues, false);
482
+ }
483
+ async selfUpdatePolicy(sec, ptype, oldRule, newRule) {
484
+ return this.updatePolicyInternal(sec, ptype, oldRule, newRule, false);
485
+ }
486
+ async selfAddPolicies(sec, ptype, rule) {
487
+ return this.addPoliciesInternal(sec, ptype, rule, false);
488
+ }
489
+ async selfRemovePolicies(sec, ptype, rule) {
490
+ return this.removePoliciesInternal(sec, ptype, rule, false);
491
+ }
474
492
  }
@@ -3,6 +3,7 @@ export * from './fileAdapter';
3
3
  export * from './stringAdapter';
4
4
  export * from './helper';
5
5
  export * from './watcher';
6
+ export * from './watcherEx';
6
7
  export * from './filteredAdapter';
7
8
  export * from './defaultFilteredAdapter';
8
9
  export * from './batchAdapter';
@@ -3,6 +3,7 @@ export * from './fileAdapter';
3
3
  export * from './stringAdapter';
4
4
  export * from './helper';
5
5
  export * from './watcher';
6
+ export * from './watcherEx';
6
7
  export * from './filteredAdapter';
7
8
  export * from './defaultFilteredAdapter';
8
9
  export * from './batchAdapter';
@@ -0,0 +1,9 @@
1
+ import { Model } from '../model';
2
+ export interface WatcherEx {
3
+ updateForAddPolicy(sec: string, ptype: string, ...params: string[]): Promise<void>;
4
+ updateForRemovePolicy(sec: string, ptype: string, ...params: string[]): Promise<void>;
5
+ updateForRemoveFilteredPolicy(sec: string, ptype: string, fieldIndex: number, ...fieldValues: string[]): Promise<void>;
6
+ updateForSavePolicy(model: Model): Promise<boolean>;
7
+ updateForAddPolicies(sec: string, ptype: string, ...rules: string[][]): Promise<void>;
8
+ updateForRemovePolicies(sec: string, ptype: string, ...rules: string[][]): Promise<void>;
9
+ }
@@ -0,0 +1,13 @@
1
+ // Copyright 2022 The Casbin Authors. All Rights Reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
@@ -334,7 +334,7 @@ export class SyncedEnforcer extends Enforcer {
334
334
  */
335
335
  async removeNamedPolicy(ptype, ...params) {
336
336
  await this.lock.acquireAsync();
337
- return this.removePolicyInternal('p', ptype, params).finally(() => this.lock.release());
337
+ return this.removePolicyInternal('p', ptype, params, true).finally(() => this.lock.release());
338
338
  }
339
339
  /**
340
340
  * removeFilteredNamedPolicy removes an authorization rule from the current named policy, field filters can be specified.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "casbin",
3
- "version": "5.16.0",
3
+ "version": "5.18.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",