Skip to content

Commit 10d7086

Browse files
feat: add WatcherEx (#381)
1 parent 0c440b9 commit 10d7086

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

src/coreEnforcer.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { compile, compileAsync, addBinaryOp } from 'expression-eval';
1616

1717
import { DefaultEffector, Effect, Effector } from './effect';
1818
import { FunctionMap, Model, newModel, PolicyOp } from './model';
19-
import { Adapter, FilteredAdapter, Watcher, BatchAdapter, UpdatableAdapter } from './persist';
19+
import { Adapter, FilteredAdapter, Watcher, BatchAdapter, UpdatableAdapter, WatcherEx } from './persist';
2020
import { DefaultRoleManager, RoleManager } from './rbac';
2121
import {
2222
escapeAssertion,
@@ -49,6 +49,7 @@ export class CoreEnforcer {
4949

5050
protected adapter: UpdatableAdapter | FilteredAdapter | Adapter | BatchAdapter;
5151
protected watcher: Watcher | null = null;
52+
protected watcherEx: WatcherEx | null = null;
5253
protected rmMap: Map<string, RoleManager>;
5354

5455
protected enabled = true;
@@ -127,6 +128,15 @@ export class CoreEnforcer {
127128
watcher.setUpdateCallback(async () => await this.loadPolicy());
128129
}
129130

131+
/**
132+
* setWatcherEx sets the current watcherEx.
133+
*
134+
* @param watcherEx the watcherEx.
135+
*/
136+
public setWatcherEx(watcherEx: WatcherEx): void {
137+
this.watcherEx = watcherEx;
138+
}
139+
130140
/**
131141
* setRoleManager sets the current role manager.
132142
*
@@ -261,7 +271,9 @@ export class CoreEnforcer {
261271
if (!flag) {
262272
return false;
263273
}
264-
if (this.watcher) {
274+
if (this.watcherEx) {
275+
return await this.watcherEx.updateForSavePolicy(this.model);
276+
} else if (this.watcher) {
265277
return await this.watcher.update();
266278
}
267279
return true;

src/internalEnforcer.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ export class InternalEnforcer extends CoreEnforcer {
3939
}
4040
}
4141

42-
if (this.watcher && this.autoNotifyWatcher) {
42+
if (this.autoNotifyWatcher) {
4343
// error intentionally ignored
44-
this.watcher.update();
44+
if (this.watcherEx) await this.watcherEx.updateForAddPolicy(sec, ptype, ...rule);
45+
else if (this.watcher) await this.watcher.update();
4546
}
4647

4748
const ok = this.model.addPolicy(sec, ptype, rule);
@@ -75,9 +76,10 @@ export class InternalEnforcer extends CoreEnforcer {
7576
}
7677
}
7778

78-
if (this.watcher && this.autoNotifyWatcher) {
79+
if (this.autoNotifyWatcher) {
7980
// error intentionally ignored
80-
this.watcher.update();
81+
if (this.watcherEx) await this.watcherEx.updateForAddPolicies(sec, ptype, ...rules);
82+
else if (this.watcher) this.watcher.update();
8183
}
8284

8385
const [ok, effects] = await this.model.addPolicies(sec, ptype, rules);
@@ -144,7 +146,8 @@ export class InternalEnforcer extends CoreEnforcer {
144146

145147
if (this.watcher && this.autoNotifyWatcher) {
146148
// error intentionally ignored
147-
this.watcher.update();
149+
if (this.watcherEx) await this.watcherEx.updateForRemovePolicy(sec, ptype, ...rule);
150+
else if (this.watcher) await this.watcher.update();
148151
}
149152

150153
const ok = await this.model.removePolicy(sec, ptype, rule);
@@ -178,7 +181,8 @@ export class InternalEnforcer extends CoreEnforcer {
178181

179182
if (this.watcher && this.autoNotifyWatcher) {
180183
// error intentionally ignored
181-
this.watcher.update();
184+
if (this.watcherEx) await this.watcherEx.updateForRemovePolicies(sec, ptype, ...rules);
185+
else if (this.watcher) await this.watcher.update();
182186
}
183187

184188
const [ok, effects] = this.model.removePolicies(sec, ptype, rules);
@@ -204,7 +208,8 @@ export class InternalEnforcer extends CoreEnforcer {
204208

205209
if (this.watcher && this.autoNotifyWatcher) {
206210
// error intentionally ignored
207-
this.watcher.update();
211+
if (this.watcherEx) await this.watcherEx.updateForRemoveFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);
212+
else if (this.watcher) await this.watcher.update();
208213
}
209214

210215
const [ok, effects] = this.model.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);

src/persist/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * from './fileAdapter';
33
export * from './stringAdapter';
44
export * from './helper';
55
export * from './watcher';
6+
export * from './watcherEx';
67
export * from './filteredAdapter';
78
export * from './defaultFilteredAdapter';
89
export * from './batchAdapter';

src/persist/watcherEx.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2022 The Casbin Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import { Model } from '../model';
16+
17+
export interface WatcherEx {
18+
updateForAddPolicy(sec: string, ptype: string, ...params: string[]): Promise<void>;
19+
20+
updateForRemovePolicy(sec: string, ptype: string, ...params: string[]): Promise<void>;
21+
22+
updateForRemoveFilteredPolicy(sec: string, ptype: string, fieldIndex: number, ...fieldValues: string[]): Promise<void>;
23+
24+
updateForSavePolicy(model: Model): Promise<boolean>;
25+
26+
updateForAddPolicies(sec: string, ptype: string, ...rules: string[][]): Promise<void>;
27+
28+
updateForRemovePolicies(sec: string, ptype: string, ...rules: string[][]): Promise<void>;
29+
}

0 commit comments

Comments
 (0)