Skip to content

Commit 7f4bd0f

Browse files
Copilothsluoyz
andcommitted
Add self policy management methods that skip adapter and watcher calls
Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
1 parent da6b861 commit 7f4bd0f

3 files changed

Lines changed: 842 additions & 12 deletions

File tree

src/internalEnforcer.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,103 @@ export class InternalEnforcer extends CoreEnforcer {
274274
const assertion = this.model.model.get('p')?.get(ptype);
275275
assertion?.fieldIndexMap.set(field, index);
276276
}
277+
278+
/**
279+
* addPolicySelf adds a rule to the in-memory policy only.
280+
* This method does not call the adapter or watcher.
281+
*/
282+
protected async addPolicySelf(sec: string, ptype: string, rule: string[]): Promise<boolean> {
283+
if (this.model.hasPolicy(sec, ptype, rule)) {
284+
return false;
285+
}
286+
287+
const ok = this.model.addPolicy(sec, ptype, rule);
288+
289+
if (sec === 'g' && ok) {
290+
await this.buildIncrementalRoleLinks(PolicyOp.PolicyAdd, ptype, [rule]);
291+
}
292+
return ok;
293+
}
294+
295+
/**
296+
* addPoliciesSelf adds rules to the in-memory policy only.
297+
* This method does not call the adapter or watcher.
298+
*/
299+
protected async addPoliciesSelf(sec: string, ptype: string, rules: string[][]): Promise<boolean> {
300+
for (const rule of rules) {
301+
if (this.model.hasPolicy(sec, ptype, rule)) {
302+
return false;
303+
}
304+
}
305+
306+
const [ok, effects] = await this.model.addPolicies(sec, ptype, rules);
307+
if (sec === 'g' && ok && effects?.length) {
308+
await this.buildIncrementalRoleLinks(PolicyOp.PolicyAdd, ptype, effects);
309+
}
310+
return ok;
311+
}
312+
313+
/**
314+
* updatePolicySelf updates a rule in the in-memory policy only.
315+
* This method does not call the adapter or watcher.
316+
*/
317+
protected async updatePolicySelf(sec: string, ptype: string, oldRule: string[], newRule: string[]): Promise<boolean> {
318+
if (!this.model.hasPolicy(sec, ptype, oldRule)) {
319+
return false;
320+
}
321+
322+
const ok = this.model.updatePolicy(sec, ptype, oldRule, newRule);
323+
if (sec === 'g' && ok) {
324+
await this.buildIncrementalRoleLinks(PolicyOp.PolicyRemove, ptype, [oldRule]);
325+
await this.buildIncrementalRoleLinks(PolicyOp.PolicyAdd, ptype, [newRule]);
326+
}
327+
328+
return ok;
329+
}
330+
331+
/**
332+
* removePolicySelf removes a rule from the in-memory policy only.
333+
* This method does not call the adapter or watcher.
334+
*/
335+
protected async removePolicySelf(sec: string, ptype: string, rule: string[]): Promise<boolean> {
336+
if (!this.model.hasPolicy(sec, ptype, rule)) {
337+
return false;
338+
}
339+
340+
const ok = await this.model.removePolicy(sec, ptype, rule);
341+
if (sec === 'g' && ok) {
342+
await this.buildIncrementalRoleLinks(PolicyOp.PolicyRemove, ptype, [rule]);
343+
}
344+
return ok;
345+
}
346+
347+
/**
348+
* removePoliciesSelf removes rules from the in-memory policy only.
349+
* This method does not call the adapter or watcher.
350+
*/
351+
protected async removePoliciesSelf(sec: string, ptype: string, rules: string[][]): Promise<boolean> {
352+
for (const rule of rules) {
353+
if (!this.model.hasPolicy(sec, ptype, rule)) {
354+
return false;
355+
}
356+
}
357+
358+
const [ok, effects] = this.model.removePolicies(sec, ptype, rules);
359+
if (sec === 'g' && ok && effects?.length) {
360+
await this.buildIncrementalRoleLinks(PolicyOp.PolicyRemove, ptype, effects);
361+
}
362+
return ok;
363+
}
364+
365+
/**
366+
* removeFilteredPolicySelf removes rules based on field filters from the in-memory policy only.
367+
* This method does not call the adapter or watcher.
368+
*/
369+
protected async removeFilteredPolicySelf(sec: string, ptype: string, fieldIndex: number, fieldValues: string[]): Promise<boolean> {
370+
const [ok, effects] = this.model.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);
371+
if (sec === 'g' && ok && effects?.length) {
372+
await this.buildIncrementalRoleLinks(PolicyOp.PolicyRemove, ptype, effects);
373+
}
374+
return ok;
375+
}
277376
}

0 commit comments

Comments
 (0)