From 0e9ccc6b2e4387b9130df8af4fa0e23f7e73958b Mon Sep 17 00:00:00 2001 From: Zixuan Liu Date: Tue, 21 Apr 2020 14:07:10 +0800 Subject: [PATCH] fix: improve update into adapter before model --- src/internalEnforcer.ts | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/src/internalEnforcer.ts b/src/internalEnforcer.ts index 1176900..c165164 100644 --- a/src/internalEnforcer.ts +++ b/src/internalEnforcer.ts @@ -22,9 +22,8 @@ export class InternalEnforcer extends CoreEnforcer { * addPolicyInternal adds a rule to the current policy. */ public async addPolicyInternal(sec: string, ptype: string, rule: string[]): Promise { - const ruleAdded = this.model.addPolicy(sec, ptype, rule); - if (!ruleAdded) { - return ruleAdded; + if (this.model.hasPolicy(sec, ptype, rule)) { + return false; } if (this.adapter && this.autoSave) { @@ -39,19 +38,18 @@ export class InternalEnforcer extends CoreEnforcer { if (this.watcher && this.autoNotifyWatcher) { // error intentionally ignored - await this.watcher.update(); + this.watcher.update(); } - return ruleAdded; + return this.model.addPolicy(sec, ptype, rule); } /** * removePolicyInternal removes a rule from the current policy. */ public async removePolicyInternal(sec: string, ptype: string, rule: string[]): Promise { - const ruleRemoved = this.model.removePolicy(sec, ptype, rule); - if (!ruleRemoved) { - return ruleRemoved; + if (!this.model.hasPolicy(sec, ptype, rule)) { + return false; } if (this.adapter && this.autoSave) { @@ -66,21 +64,16 @@ export class InternalEnforcer extends CoreEnforcer { if (this.watcher && this.autoNotifyWatcher) { // error intentionally ignored - await this.watcher.update(); + this.watcher.update(); } - return ruleRemoved; + return this.model.removePolicy(sec, ptype, rule); } /** * removeFilteredPolicyInternal removes rules based on field filters from the current policy. */ public async removeFilteredPolicyInternal(sec: string, ptype: string, fieldIndex: number, fieldValues: string[]): Promise { - const ruleRemoved = this.model.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues); - if (!ruleRemoved) { - return ruleRemoved; - } - if (this.adapter && this.autoSave) { try { await this.adapter.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues); @@ -93,9 +86,9 @@ export class InternalEnforcer extends CoreEnforcer { if (this.watcher && this.autoNotifyWatcher) { // error intentionally ignored - await this.watcher.update(); + this.watcher.update(); } - return ruleRemoved; + return this.model.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues); } }