From 10d7086c810ff18d9a3c792a3ec1173744bceeef Mon Sep 17 00:00:00 2001 From: Shivansh Yadav Date: Thu, 18 Aug 2022 20:53:24 +0530 Subject: [PATCH] feat: add WatcherEx (#381) --- src/coreEnforcer.ts | 16 ++++++++++++++-- src/internalEnforcer.ts | 19 ++++++++++++------- src/persist/index.ts | 1 + src/persist/watcherEx.ts | 29 +++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 src/persist/watcherEx.ts diff --git a/src/coreEnforcer.ts b/src/coreEnforcer.ts index 813de1d..2f7b488 100644 --- a/src/coreEnforcer.ts +++ b/src/coreEnforcer.ts @@ -16,7 +16,7 @@ import { compile, compileAsync, addBinaryOp } from 'expression-eval'; import { DefaultEffector, Effect, Effector } from './effect'; import { FunctionMap, Model, newModel, PolicyOp } from './model'; -import { Adapter, FilteredAdapter, Watcher, BatchAdapter, UpdatableAdapter } from './persist'; +import { Adapter, FilteredAdapter, Watcher, BatchAdapter, UpdatableAdapter, WatcherEx } from './persist'; import { DefaultRoleManager, RoleManager } from './rbac'; import { escapeAssertion, @@ -49,6 +49,7 @@ export class CoreEnforcer { protected adapter: UpdatableAdapter | FilteredAdapter | Adapter | BatchAdapter; protected watcher: Watcher | null = null; + protected watcherEx: WatcherEx | null = null; protected rmMap: Map; protected enabled = true; @@ -127,6 +128,15 @@ export class CoreEnforcer { watcher.setUpdateCallback(async () => await this.loadPolicy()); } + /** + * setWatcherEx sets the current watcherEx. + * + * @param watcherEx the watcherEx. + */ + public setWatcherEx(watcherEx: WatcherEx): void { + this.watcherEx = watcherEx; + } + /** * setRoleManager sets the current role manager. * @@ -261,7 +271,9 @@ export class CoreEnforcer { if (!flag) { return false; } - if (this.watcher) { + if (this.watcherEx) { + return await this.watcherEx.updateForSavePolicy(this.model); + } else if (this.watcher) { return await this.watcher.update(); } return true; diff --git a/src/internalEnforcer.ts b/src/internalEnforcer.ts index 1d63979..b6320ca 100644 --- a/src/internalEnforcer.ts +++ b/src/internalEnforcer.ts @@ -39,9 +39,10 @@ export class InternalEnforcer extends CoreEnforcer { } } - if (this.watcher && this.autoNotifyWatcher) { + if (this.autoNotifyWatcher) { // error intentionally ignored - this.watcher.update(); + if (this.watcherEx) await this.watcherEx.updateForAddPolicy(sec, ptype, ...rule); + else if (this.watcher) await this.watcher.update(); } const ok = this.model.addPolicy(sec, ptype, rule); @@ -75,9 +76,10 @@ export class InternalEnforcer extends CoreEnforcer { } } - if (this.watcher && this.autoNotifyWatcher) { + if (this.autoNotifyWatcher) { // error intentionally ignored - this.watcher.update(); + if (this.watcherEx) await this.watcherEx.updateForAddPolicies(sec, ptype, ...rules); + else if (this.watcher) this.watcher.update(); } const [ok, effects] = await this.model.addPolicies(sec, ptype, rules); @@ -144,7 +146,8 @@ export class InternalEnforcer extends CoreEnforcer { if (this.watcher && this.autoNotifyWatcher) { // error intentionally ignored - this.watcher.update(); + if (this.watcherEx) await this.watcherEx.updateForRemovePolicy(sec, ptype, ...rule); + else if (this.watcher) await this.watcher.update(); } const ok = await this.model.removePolicy(sec, ptype, rule); @@ -178,7 +181,8 @@ export class InternalEnforcer extends CoreEnforcer { if (this.watcher && this.autoNotifyWatcher) { // error intentionally ignored - this.watcher.update(); + if (this.watcherEx) await this.watcherEx.updateForRemovePolicies(sec, ptype, ...rules); + else if (this.watcher) await this.watcher.update(); } const [ok, effects] = this.model.removePolicies(sec, ptype, rules); @@ -204,7 +208,8 @@ export class InternalEnforcer extends CoreEnforcer { if (this.watcher && this.autoNotifyWatcher) { // error intentionally ignored - this.watcher.update(); + if (this.watcherEx) await this.watcherEx.updateForRemoveFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues); + else if (this.watcher) await this.watcher.update(); } const [ok, effects] = this.model.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues); diff --git a/src/persist/index.ts b/src/persist/index.ts index 59d0b4d..eda2fa3 100644 --- a/src/persist/index.ts +++ b/src/persist/index.ts @@ -3,6 +3,7 @@ export * from './fileAdapter'; export * from './stringAdapter'; export * from './helper'; export * from './watcher'; +export * from './watcherEx'; export * from './filteredAdapter'; export * from './defaultFilteredAdapter'; export * from './batchAdapter'; diff --git a/src/persist/watcherEx.ts b/src/persist/watcherEx.ts new file mode 100644 index 0000000..337bba7 --- /dev/null +++ b/src/persist/watcherEx.ts @@ -0,0 +1,29 @@ +// Copyright 2022 The Casbin Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { Model } from '../model'; + +export interface WatcherEx { + updateForAddPolicy(sec: string, ptype: string, ...params: string[]): Promise; + + updateForRemovePolicy(sec: string, ptype: string, ...params: string[]): Promise; + + updateForRemoveFilteredPolicy(sec: string, ptype: string, fieldIndex: number, ...fieldValues: string[]): Promise; + + updateForSavePolicy(model: Model): Promise; + + updateForAddPolicies(sec: string, ptype: string, ...rules: string[][]): Promise; + + updateForRemovePolicies(sec: string, ptype: string, ...rules: string[][]): Promise; +}