Skip to content

Commit a33b6e3

Browse files
committed
feat: added methods to enforcer for adding, removing and updating policies without usage of adapter (even if autoSave is true)
1 parent 83d74da commit a33b6e3

File tree

4 files changed

+617
-31
lines changed

4 files changed

+617
-31
lines changed

src/internalEnforcer.ts

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,18 @@ export class InternalEnforcer extends CoreEnforcer {
2424
/**
2525
* addPolicyInternal adds a rule to the current policy.
2626
*/
27-
protected async addPolicyInternal(sec: string, ptype: string, rule: string[], useWatcher: boolean): Promise<boolean> {
27+
protected async addPolicyInternal(
28+
sec: string,
29+
ptype: string,
30+
rule: string[],
31+
useWatcher: boolean,
32+
useAdapter: boolean
33+
): Promise<boolean> {
2834
if (this.model.hasPolicy(sec, ptype, rule)) {
2935
return false;
3036
}
3137

32-
if (this.adapter && this.autoSave) {
38+
if (this.adapter && this.autoSave && useAdapter) {
3339
try {
3440
await this.adapter.addPolicy(sec, ptype, rule);
3541
} catch (e) {
@@ -60,14 +66,20 @@ export class InternalEnforcer extends CoreEnforcer {
6066

6167
// addPolicies adds rules to the current policy.
6268
// removePolicies removes rules from the current policy.
63-
protected async addPoliciesInternal(sec: string, ptype: string, rules: string[][], useWatcher: boolean): Promise<boolean> {
69+
protected async addPoliciesInternal(
70+
sec: string,
71+
ptype: string,
72+
rules: string[][],
73+
useWatcher: boolean,
74+
useAdapter: boolean
75+
): Promise<boolean> {
6476
for (const rule of rules) {
6577
if (this.model.hasPolicy(sec, ptype, rule)) {
6678
return false;
6779
}
6880
}
6981

70-
if (this.autoSave) {
82+
if (this.autoSave && useAdapter) {
7183
if ('addPolicies' in this.adapter) {
7284
try {
7385
await this.adapter.addPolicies(sec, ptype, rules);
@@ -107,13 +119,14 @@ export class InternalEnforcer extends CoreEnforcer {
107119
ptype: string,
108120
oldRule: string[],
109121
newRule: string[],
110-
useWatcher: boolean
122+
useWatcher: boolean,
123+
useAdapter: boolean
111124
): Promise<boolean> {
112125
if (!this.model.hasPolicy(sec, ptype, oldRule)) {
113126
return false;
114127
}
115128

116-
if (this.autoSave) {
129+
if (this.autoSave && useAdapter) {
117130
if ('updatePolicy' in this.adapter) {
118131
try {
119132
await this.adapter.updatePolicy(sec, ptype, oldRule, newRule);
@@ -149,12 +162,18 @@ export class InternalEnforcer extends CoreEnforcer {
149162
/**
150163
* removePolicyInternal removes a rule from the current policy.
151164
*/
152-
protected async removePolicyInternal(sec: string, ptype: string, rule: string[], useWatcher: boolean): Promise<boolean> {
165+
protected async removePolicyInternal(
166+
sec: string,
167+
ptype: string,
168+
rule: string[],
169+
useWatcher: boolean,
170+
useAdapter: boolean
171+
): Promise<boolean> {
153172
if (!this.model.hasPolicy(sec, ptype, rule)) {
154173
return false;
155174
}
156175

157-
if (this.adapter && this.autoSave) {
176+
if (this.adapter && this.autoSave && useAdapter) {
158177
try {
159178
await this.adapter.removePolicy(sec, ptype, rule);
160179
} catch (e) {
@@ -183,14 +202,20 @@ export class InternalEnforcer extends CoreEnforcer {
183202
}
184203

185204
// removePolicies removes rules from the current policy.
186-
protected async removePoliciesInternal(sec: string, ptype: string, rules: string[][], useWatcher: boolean): Promise<boolean> {
205+
protected async removePoliciesInternal(
206+
sec: string,
207+
ptype: string,
208+
rules: string[][],
209+
useWatcher: boolean,
210+
useAdapter: boolean
211+
): Promise<boolean> {
187212
for (const rule of rules) {
188213
if (!this.model.hasPolicy(sec, ptype, rule)) {
189214
return false;
190215
}
191216
}
192217

193-
if (this.autoSave) {
218+
if (this.autoSave && useAdapter) {
194219
if ('removePolicies' in this.adapter) {
195220
try {
196221
await this.adapter.removePolicies(sec, ptype, rules);
@@ -230,9 +255,10 @@ export class InternalEnforcer extends CoreEnforcer {
230255
ptype: string,
231256
fieldIndex: number,
232257
fieldValues: string[],
233-
useWatcher: boolean
258+
useWatcher: boolean,
259+
useAdapter: boolean
234260
): Promise<boolean> {
235-
if (this.adapter && this.autoSave) {
261+
if (this.adapter && this.autoSave && useAdapter) {
236262
try {
237263
await this.adapter.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);
238264
} catch (e) {

0 commit comments

Comments
 (0)