Skip to content

Commit

Permalink
feat: add WatcherEx (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivansh-yadav13 authored Aug 18, 2022
1 parent 0c440b9 commit 10d7086
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
16 changes: 14 additions & 2 deletions src/coreEnforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<string, RoleManager>;

protected enabled = true;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 12 additions & 7 deletions src/internalEnforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/persist/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
29 changes: 29 additions & 0 deletions src/persist/watcherEx.ts
Original file line number Diff line number Diff line change
@@ -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<void>;

updateForRemovePolicy(sec: string, ptype: string, ...params: string[]): Promise<void>;

updateForRemoveFilteredPolicy(sec: string, ptype: string, fieldIndex: number, ...fieldValues: string[]): Promise<void>;

updateForSavePolicy(model: Model): Promise<boolean>;

updateForAddPolicies(sec: string, ptype: string, ...rules: string[][]): Promise<void>;

updateForRemovePolicies(sec: string, ptype: string, ...rules: string[][]): Promise<void>;
}

0 comments on commit 10d7086

Please sign in to comment.