Skip to content

Commit 0e9ccc6

Browse files
committed
fix: improve update into adapter before model
1 parent f46e8a9 commit 0e9ccc6

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

src/internalEnforcer.ts

+10-17
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ export class InternalEnforcer extends CoreEnforcer {
2222
* addPolicyInternal adds a rule to the current policy.
2323
*/
2424
public async addPolicyInternal(sec: string, ptype: string, rule: string[]): Promise<boolean> {
25-
const ruleAdded = this.model.addPolicy(sec, ptype, rule);
26-
if (!ruleAdded) {
27-
return ruleAdded;
25+
if (this.model.hasPolicy(sec, ptype, rule)) {
26+
return false;
2827
}
2928

3029
if (this.adapter && this.autoSave) {
@@ -39,19 +38,18 @@ export class InternalEnforcer extends CoreEnforcer {
3938

4039
if (this.watcher && this.autoNotifyWatcher) {
4140
// error intentionally ignored
42-
await this.watcher.update();
41+
this.watcher.update();
4342
}
4443

45-
return ruleAdded;
44+
return this.model.addPolicy(sec, ptype, rule);
4645
}
4746

4847
/**
4948
* removePolicyInternal removes a rule from the current policy.
5049
*/
5150
public async removePolicyInternal(sec: string, ptype: string, rule: string[]): Promise<boolean> {
52-
const ruleRemoved = this.model.removePolicy(sec, ptype, rule);
53-
if (!ruleRemoved) {
54-
return ruleRemoved;
51+
if (!this.model.hasPolicy(sec, ptype, rule)) {
52+
return false;
5553
}
5654

5755
if (this.adapter && this.autoSave) {
@@ -66,21 +64,16 @@ export class InternalEnforcer extends CoreEnforcer {
6664

6765
if (this.watcher && this.autoNotifyWatcher) {
6866
// error intentionally ignored
69-
await this.watcher.update();
67+
this.watcher.update();
7068
}
7169

72-
return ruleRemoved;
70+
return this.model.removePolicy(sec, ptype, rule);
7371
}
7472

7573
/**
7674
* removeFilteredPolicyInternal removes rules based on field filters from the current policy.
7775
*/
7876
public async removeFilteredPolicyInternal(sec: string, ptype: string, fieldIndex: number, fieldValues: string[]): Promise<boolean> {
79-
const ruleRemoved = this.model.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);
80-
if (!ruleRemoved) {
81-
return ruleRemoved;
82-
}
83-
8477
if (this.adapter && this.autoSave) {
8578
try {
8679
await this.adapter.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);
@@ -93,9 +86,9 @@ export class InternalEnforcer extends CoreEnforcer {
9386

9487
if (this.watcher && this.autoNotifyWatcher) {
9588
// error intentionally ignored
96-
await this.watcher.update();
89+
this.watcher.update();
9790
}
9891

99-
return ruleRemoved;
92+
return this.model.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);
10093
}
10194
}

0 commit comments

Comments
 (0)