Skip to content
50 changes: 38 additions & 12 deletions src/internalEnforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ export class InternalEnforcer extends CoreEnforcer {
/**
* addPolicyInternal adds a rule to the current policy.
*/
protected async addPolicyInternal(sec: string, ptype: string, rule: string[], useWatcher: boolean): Promise<boolean> {
protected async addPolicyInternal(
sec: string,
ptype: string,
rule: string[],
useWatcher: boolean,
useAdapter: boolean
): Promise<boolean> {
if (this.model.hasPolicy(sec, ptype, rule)) {
return false;
}

if (this.adapter && this.autoSave) {
if (this.adapter && this.autoSave && useAdapter) {
try {
await this.adapter.addPolicy(sec, ptype, rule);
} catch (e) {
Expand Down Expand Up @@ -60,14 +66,20 @@ export class InternalEnforcer extends CoreEnforcer {

// addPolicies adds rules to the current policy.
// removePolicies removes rules from the current policy.
protected async addPoliciesInternal(sec: string, ptype: string, rules: string[][], useWatcher: boolean): Promise<boolean> {
protected async addPoliciesInternal(
sec: string,
ptype: string,
rules: string[][],
useWatcher: boolean,
useAdapter: boolean
): Promise<boolean> {
for (const rule of rules) {
if (this.model.hasPolicy(sec, ptype, rule)) {
return false;
}
}

if (this.autoSave) {
if (this.autoSave && useAdapter) {
if ('addPolicies' in this.adapter) {
try {
await this.adapter.addPolicies(sec, ptype, rules);
Expand Down Expand Up @@ -107,13 +119,14 @@ export class InternalEnforcer extends CoreEnforcer {
ptype: string,
oldRule: string[],
newRule: string[],
useWatcher: boolean
useWatcher: boolean,
useAdapter: boolean
): Promise<boolean> {
if (!this.model.hasPolicy(sec, ptype, oldRule)) {
return false;
}

if (this.autoSave) {
if (this.autoSave && useAdapter) {
if ('updatePolicy' in this.adapter) {
try {
await this.adapter.updatePolicy(sec, ptype, oldRule, newRule);
Expand Down Expand Up @@ -149,12 +162,18 @@ export class InternalEnforcer extends CoreEnforcer {
/**
* removePolicyInternal removes a rule from the current policy.
*/
protected async removePolicyInternal(sec: string, ptype: string, rule: string[], useWatcher: boolean): Promise<boolean> {
protected async removePolicyInternal(
sec: string,
ptype: string,
rule: string[],
useWatcher: boolean,
useAdapter: boolean
): Promise<boolean> {
if (!this.model.hasPolicy(sec, ptype, rule)) {
return false;
}

if (this.adapter && this.autoSave) {
if (this.adapter && this.autoSave && useAdapter) {
try {
await this.adapter.removePolicy(sec, ptype, rule);
} catch (e) {
Expand Down Expand Up @@ -183,14 +202,20 @@ export class InternalEnforcer extends CoreEnforcer {
}

// removePolicies removes rules from the current policy.
protected async removePoliciesInternal(sec: string, ptype: string, rules: string[][], useWatcher: boolean): Promise<boolean> {
protected async removePoliciesInternal(
sec: string,
ptype: string,
rules: string[][],
useWatcher: boolean,
useAdapter: boolean
): Promise<boolean> {
for (const rule of rules) {
if (!this.model.hasPolicy(sec, ptype, rule)) {
return false;
}
}

if (this.autoSave) {
if (this.autoSave && useAdapter) {
if ('removePolicies' in this.adapter) {
try {
await this.adapter.removePolicies(sec, ptype, rules);
Expand Down Expand Up @@ -230,9 +255,10 @@ export class InternalEnforcer extends CoreEnforcer {
ptype: string,
fieldIndex: number,
fieldValues: string[],
useWatcher: boolean
useWatcher: boolean,
useAdapter: boolean
): Promise<boolean> {
if (this.adapter && this.autoSave) {
if (this.adapter && this.autoSave && useAdapter) {
try {
await this.adapter.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);
} catch (e) {
Expand Down
Loading