Skip to content

Commit 591233f

Browse files
feat: add management api for watcherEx callbacks (#384)
1 parent 46937a3 commit 591233f

File tree

3 files changed

+105
-42
lines changed

3 files changed

+105
-42
lines changed

src/internalEnforcer.ts

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class InternalEnforcer extends CoreEnforcer {
2424
/**
2525
* addPolicyInternal adds a rule to the current policy.
2626
*/
27-
public async addPolicyInternal(sec: string, ptype: string, rule: string[]): Promise<boolean> {
27+
protected async addPolicyInternal(sec: string, ptype: string, rule: string[], useWatcher: boolean): Promise<boolean> {
2828
if (this.model.hasPolicy(sec, ptype, rule)) {
2929
return false;
3030
}
@@ -39,10 +39,15 @@ export class InternalEnforcer extends CoreEnforcer {
3939
}
4040
}
4141

42-
if (this.autoNotifyWatcher) {
43-
// error intentionally ignored
44-
if (this.watcherEx) await this.watcherEx.updateForAddPolicy(sec, ptype, ...rule);
45-
else if (this.watcher) await this.watcher.update();
42+
if (useWatcher) {
43+
if (this.autoNotifyWatcher) {
44+
// error intentionally ignored
45+
if (this.watcherEx) {
46+
this.watcherEx.updateForAddPolicy(sec, ptype, ...rule);
47+
} else if (this.watcher) {
48+
this.watcher.update();
49+
}
50+
}
4651
}
4752

4853
const ok = this.model.addPolicy(sec, ptype, rule);
@@ -55,7 +60,7 @@ export class InternalEnforcer extends CoreEnforcer {
5560

5661
// addPolicies adds rules to the current policy.
5762
// removePolicies removes rules from the current policy.
58-
public async addPoliciesInternal(sec: string, ptype: string, rules: string[][]): Promise<boolean> {
63+
protected async addPoliciesInternal(sec: string, ptype: string, rules: string[][], useWatcher: boolean): Promise<boolean> {
5964
for (const rule of rules) {
6065
if (this.model.hasPolicy(sec, ptype, rule)) {
6166
return false;
@@ -76,10 +81,15 @@ export class InternalEnforcer extends CoreEnforcer {
7681
}
7782
}
7883

79-
if (this.autoNotifyWatcher) {
80-
// error intentionally ignored
81-
if (this.watcherEx) await this.watcherEx.updateForAddPolicies(sec, ptype, ...rules);
82-
else if (this.watcher) this.watcher.update();
84+
if (useWatcher) {
85+
if (this.autoNotifyWatcher) {
86+
// error intentionally ignored
87+
if (this.watcherEx) {
88+
this.watcherEx.updateForAddPolicies(sec, ptype, ...rules);
89+
} else if (this.watcher) {
90+
this.watcher.update();
91+
}
92+
}
8393
}
8494

8595
const [ok, effects] = await this.model.addPolicies(sec, ptype, rules);
@@ -92,7 +102,13 @@ export class InternalEnforcer extends CoreEnforcer {
92102
/**
93103
* updatePolicyInternal updates a rule from the current policy.
94104
*/
95-
public async updatePolicyInternal(sec: string, ptype: string, oldRule: string[], newRule: string[]): Promise<boolean> {
105+
protected async updatePolicyInternal(
106+
sec: string,
107+
ptype: string,
108+
oldRule: string[],
109+
newRule: string[],
110+
useWatcher: boolean
111+
): Promise<boolean> {
96112
if (!this.model.hasPolicy(sec, ptype, oldRule)) {
97113
return false;
98114
}
@@ -111,10 +127,12 @@ export class InternalEnforcer extends CoreEnforcer {
111127
}
112128
}
113129

114-
if (this.watcher && this.autoNotifyWatcher) {
115-
// In fact I think it should wait for the respond, but they implement add_policy() like this
116-
// error intentionally ignored
117-
this.watcher.update();
130+
if (useWatcher) {
131+
if (this.watcher && this.autoNotifyWatcher) {
132+
// In fact I think it should wait for the respond, but they implement add_policy() like this
133+
// error intentionally ignored
134+
this.watcher.update();
135+
}
118136
}
119137

120138
const ok = this.model.updatePolicy(sec, ptype, oldRule, newRule);
@@ -129,7 +147,7 @@ export class InternalEnforcer extends CoreEnforcer {
129147
/**
130148
* removePolicyInternal removes a rule from the current policy.
131149
*/
132-
public async removePolicyInternal(sec: string, ptype: string, rule: string[]): Promise<boolean> {
150+
protected async removePolicyInternal(sec: string, ptype: string, rule: string[], useWatcher: boolean): Promise<boolean> {
133151
if (!this.model.hasPolicy(sec, ptype, rule)) {
134152
return false;
135153
}
@@ -144,10 +162,15 @@ export class InternalEnforcer extends CoreEnforcer {
144162
}
145163
}
146164

147-
if (this.watcher && this.autoNotifyWatcher) {
148-
// error intentionally ignored
149-
if (this.watcherEx) await this.watcherEx.updateForRemovePolicy(sec, ptype, ...rule);
150-
else if (this.watcher) await this.watcher.update();
165+
if (useWatcher) {
166+
if (this.watcher && this.autoNotifyWatcher) {
167+
// error intentionally ignored
168+
if (this.watcherEx) {
169+
this.watcherEx.updateForRemovePolicy(sec, ptype, ...rule);
170+
} else if (this.watcher) {
171+
this.watcher.update();
172+
}
173+
}
151174
}
152175

153176
const ok = await this.model.removePolicy(sec, ptype, rule);
@@ -158,7 +181,7 @@ export class InternalEnforcer extends CoreEnforcer {
158181
}
159182

160183
// removePolicies removes rules from the current policy.
161-
public async removePoliciesInternal(sec: string, ptype: string, rules: string[][]): Promise<boolean> {
184+
protected async removePoliciesInternal(sec: string, ptype: string, rules: string[][], useWatcher: boolean): Promise<boolean> {
162185
for (const rule of rules) {
163186
if (!this.model.hasPolicy(sec, ptype, rule)) {
164187
return false;
@@ -179,10 +202,15 @@ export class InternalEnforcer extends CoreEnforcer {
179202
}
180203
}
181204

182-
if (this.watcher && this.autoNotifyWatcher) {
183-
// error intentionally ignored
184-
if (this.watcherEx) await this.watcherEx.updateForRemovePolicies(sec, ptype, ...rules);
185-
else if (this.watcher) await this.watcher.update();
205+
if (useWatcher) {
206+
if (this.watcher && this.autoNotifyWatcher) {
207+
// error intentionally ignored
208+
if (this.watcherEx) {
209+
this.watcherEx.updateForRemovePolicies(sec, ptype, ...rules);
210+
} else if (this.watcher) {
211+
this.watcher.update();
212+
}
213+
}
186214
}
187215

188216
const [ok, effects] = this.model.removePolicies(sec, ptype, rules);
@@ -195,7 +223,13 @@ export class InternalEnforcer extends CoreEnforcer {
195223
/**
196224
* removeFilteredPolicyInternal removes rules based on field filters from the current policy.
197225
*/
198-
public async removeFilteredPolicyInternal(sec: string, ptype: string, fieldIndex: number, fieldValues: string[]): Promise<boolean> {
226+
protected async removeFilteredPolicyInternal(
227+
sec: string,
228+
ptype: string,
229+
fieldIndex: number,
230+
fieldValues: string[],
231+
useWatcher: boolean
232+
): Promise<boolean> {
199233
if (this.adapter && this.autoSave) {
200234
try {
201235
await this.adapter.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);
@@ -206,10 +240,15 @@ export class InternalEnforcer extends CoreEnforcer {
206240
}
207241
}
208242

209-
if (this.watcher && this.autoNotifyWatcher) {
210-
// error intentionally ignored
211-
if (this.watcherEx) await this.watcherEx.updateForRemoveFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);
212-
else if (this.watcher) await this.watcher.update();
243+
if (useWatcher) {
244+
if (this.watcher && this.autoNotifyWatcher) {
245+
// error intentionally ignored
246+
if (this.watcherEx) {
247+
this.watcherEx.updateForRemoveFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);
248+
} else if (this.watcher) {
249+
this.watcher.update();
250+
}
251+
}
213252
}
214253

215254
const [ok, effects] = this.model.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);

src/managementEnforcer.ts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ export class ManagementEnforcer extends InternalEnforcer {
260260
* @return succeeds or not.
261261
*/
262262
public async addNamedPolicy(ptype: string, ...params: string[]): Promise<boolean> {
263-
return this.addPolicyInternal('p', ptype, params);
263+
return this.addPolicyInternal('p', ptype, params, true);
264264
}
265265

266266
/**
@@ -273,7 +273,7 @@ export class ManagementEnforcer extends InternalEnforcer {
273273
* @return succeeds or not.
274274
*/
275275
public async addNamedPolicies(ptype: string, rules: string[][]): Promise<boolean> {
276-
return this.addPoliciesInternal('p', ptype, rules);
276+
return this.addPoliciesInternal('p', ptype, rules, true);
277277
}
278278

279279
/**
@@ -300,7 +300,7 @@ export class ManagementEnforcer extends InternalEnforcer {
300300
* @return succeeds or not.
301301
*/
302302
public async updateNamedPolicy(ptype: string, oldRule: string[], newRule: string[]): Promise<boolean> {
303-
return this.updatePolicyInternal('p', ptype, oldRule, newRule);
303+
return this.updatePolicyInternal('p', ptype, oldRule, newRule, true);
304304
}
305305

306306
/**
@@ -343,7 +343,7 @@ export class ManagementEnforcer extends InternalEnforcer {
343343
* @return succeeds or not.
344344
*/
345345
public async removeNamedPolicy(ptype: string, ...params: string[]): Promise<boolean> {
346-
return this.removePolicyInternal('p', ptype, params);
346+
return this.removePolicyInternal('p', ptype, params, true);
347347
}
348348

349349
/**
@@ -354,7 +354,7 @@ export class ManagementEnforcer extends InternalEnforcer {
354354
* @return succeeds or not.
355355
*/
356356
public async removeNamedPolicies(ptype: string, rules: string[][]): Promise<boolean> {
357-
return this.removePoliciesInternal('p', ptype, rules);
357+
return this.removePoliciesInternal('p', ptype, rules, true);
358358
}
359359

360360
/**
@@ -367,7 +367,7 @@ export class ManagementEnforcer extends InternalEnforcer {
367367
* @return succeeds or not.
368368
*/
369369
public async removeFilteredNamedPolicy(ptype: string, fieldIndex: number, ...fieldValues: string[]): Promise<boolean> {
370-
return this.removeFilteredPolicyInternal('p', ptype, fieldIndex, fieldValues);
370+
return this.removeFilteredPolicyInternal('p', ptype, fieldIndex, fieldValues, true);
371371
}
372372

373373
/**
@@ -425,7 +425,7 @@ export class ManagementEnforcer extends InternalEnforcer {
425425
* @return succeeds or not.
426426
*/
427427
public async addNamedGroupingPolicy(ptype: string, ...params: string[]): Promise<boolean> {
428-
return this.addPolicyInternal('g', ptype, params);
428+
return this.addPolicyInternal('g', ptype, params, true);
429429
}
430430

431431
/**
@@ -438,7 +438,7 @@ export class ManagementEnforcer extends InternalEnforcer {
438438
* @return succeeds or not.
439439
*/
440440
public async addNamedGroupingPolicies(ptype: string, rules: string[][]): Promise<boolean> {
441-
return this.addPoliciesInternal('g', ptype, rules);
441+
return this.addPoliciesInternal('g', ptype, rules, true);
442442
}
443443

444444
/**
@@ -481,7 +481,7 @@ export class ManagementEnforcer extends InternalEnforcer {
481481
* @return succeeds or not.
482482
*/
483483
public async removeNamedGroupingPolicy(ptype: string, ...params: string[]): Promise<boolean> {
484-
return this.removePolicyInternal('g', ptype, params);
484+
return this.removePolicyInternal('g', ptype, params, true);
485485
}
486486

487487
/**
@@ -492,7 +492,7 @@ export class ManagementEnforcer extends InternalEnforcer {
492492
* @return succeeds or not.
493493
*/
494494
public async removeNamedGroupingPolicies(ptype: string, rules: string[][]): Promise<boolean> {
495-
return this.removePoliciesInternal('g', ptype, rules);
495+
return this.removePoliciesInternal('g', ptype, rules, true);
496496
}
497497

498498
/**
@@ -505,7 +505,7 @@ export class ManagementEnforcer extends InternalEnforcer {
505505
* @return succeeds or not.
506506
*/
507507
public async removeFilteredNamedGroupingPolicy(ptype: string, fieldIndex: number, ...fieldValues: string[]): Promise<boolean> {
508-
return this.removeFilteredPolicyInternal('g', ptype, fieldIndex, fieldValues);
508+
return this.removeFilteredPolicyInternal('g', ptype, fieldIndex, fieldValues, true);
509509
}
510510

511511
/**
@@ -516,4 +516,28 @@ export class ManagementEnforcer extends InternalEnforcer {
516516
public async addFunction(name: string, func: MatchingFunction): Promise<void> {
517517
this.fm.addFunction(name, func);
518518
}
519+
520+
public async selfAddPolicy(sec: string, ptype: string, rule: string[]): Promise<boolean> {
521+
return this.addPolicyInternal(sec, ptype, rule, false);
522+
}
523+
524+
public async selfRemovePolicy(sec: string, ptype: string, rule: string[]): Promise<boolean> {
525+
return this.removePolicyInternal(sec, ptype, rule, false);
526+
}
527+
528+
public async selfRemoveFilteredPolicy(sec: string, ptype: string, fieldIndex: number, fieldValues: string[]): Promise<boolean> {
529+
return this.removeFilteredPolicyInternal(sec, ptype, fieldIndex, fieldValues, false);
530+
}
531+
532+
public async selfUpdatePolicy(sec: string, ptype: string, oldRule: string[], newRule: string[]): Promise<boolean> {
533+
return this.updatePolicyInternal(sec, ptype, oldRule, newRule, false);
534+
}
535+
536+
public async selfAddPolicies(sec: string, ptype: string, rule: string[][]): Promise<boolean> {
537+
return this.addPoliciesInternal(sec, ptype, rule, false);
538+
}
539+
540+
public async selfRemovePolicies(sec: string, ptype: string, rule: string[][]): Promise<boolean> {
541+
return this.removePoliciesInternal(sec, ptype, rule, false);
542+
}
519543
}

src/syncedEnforcer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ export class SyncedEnforcer extends Enforcer {
366366
*/
367367
public async removeNamedPolicy(ptype: string, ...params: string[]): Promise<boolean> {
368368
await this.lock.acquireAsync();
369-
return this.removePolicyInternal('p', ptype, params).finally(() => this.lock.release());
369+
return this.removePolicyInternal('p', ptype, params, true).finally(() => this.lock.release());
370370
}
371371

372372
/**

0 commit comments

Comments
 (0)