From 2ce07c29cd49c6da304063e8075923b739fc5145 Mon Sep 17 00:00:00 2001 From: Zixuan Liu Date: Sat, 18 Apr 2020 15:35:35 +0800 Subject: [PATCH] feat: controls whether to automatically notify Watcher --- src/coreEnforcer.ts | 10 ++++++++++ src/internalEnforcer.ts | 24 ++++++++++++------------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/coreEnforcer.ts b/src/coreEnforcer.ts index f12437f..040685d 100644 --- a/src/coreEnforcer.ts +++ b/src/coreEnforcer.ts @@ -38,6 +38,7 @@ export class CoreEnforcer { private enabled: boolean; protected autoSave: boolean; protected autoBuildRoleLinks: boolean; + protected autoNotifyWatcher: boolean; public initialize(): void { this.rm = new DefaultRoleManager(10); @@ -48,6 +49,7 @@ export class CoreEnforcer { this.enabled = true; this.autoSave = true; this.autoBuildRoleLinks = true; + this.autoNotifyWatcher = true; } /** @@ -227,6 +229,14 @@ export class CoreEnforcer { this.autoSave = autoSave; } + /** + * enableAutoNotifyWatcher controls whether to save a policy rule automatically notify the Watcher when it is added or removed. + * @param enable whether to enable the AutoNotifyWatcher feature. + */ + public enableAutoNotifyWatcher(enable: boolean): void { + this.autoNotifyWatcher = enable; + } + /** * enableAutoBuildRoleLinks controls whether to save a policy rule * automatically to the adapter when it is added or removed. diff --git a/src/internalEnforcer.ts b/src/internalEnforcer.ts index 1bb0e95..1176900 100644 --- a/src/internalEnforcer.ts +++ b/src/internalEnforcer.ts @@ -35,11 +35,11 @@ export class InternalEnforcer extends CoreEnforcer { throw e; } } + } - if (this.watcher) { - // error intentionally ignored - await this.watcher.update(); - } + if (this.watcher && this.autoNotifyWatcher) { + // error intentionally ignored + await this.watcher.update(); } return ruleAdded; @@ -62,11 +62,11 @@ export class InternalEnforcer extends CoreEnforcer { throw e; } } + } - if (this.watcher) { - // error intentionally ignored - await this.watcher.update(); - } + if (this.watcher && this.autoNotifyWatcher) { + // error intentionally ignored + await this.watcher.update(); } return ruleRemoved; @@ -89,11 +89,11 @@ export class InternalEnforcer extends CoreEnforcer { throw e; } } + } - if (this.watcher) { - // error intentionally ignored - await this.watcher.update(); - } + if (this.watcher && this.autoNotifyWatcher) { + // error intentionally ignored + await this.watcher.update(); } return ruleRemoved;