Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions src/internalEnforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ 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 = true): Promise<boolean> {
if (this.model.hasPolicy(sec, ptype, rule)) {
return false;
}

if (this.adapter && this.autoSave) {
if (useAdapter && this.adapter && this.autoSave) {
try {
await this.adapter.addPolicy(sec, ptype, rule);
} catch (e) {
Expand Down Expand Up @@ -60,14 +60,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 = true
): Promise<boolean> {
for (const rule of rules) {
if (this.model.hasPolicy(sec, ptype, rule)) {
return false;
}
}

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

if (this.autoSave) {
if (useAdapter && this.autoSave) {
if ('updatePolicy' in this.adapter) {
try {
await this.adapter.updatePolicy(sec, ptype, oldRule, newRule);
Expand Down Expand Up @@ -149,12 +156,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 = true
): Promise<boolean> {
if (!this.model.hasPolicy(sec, ptype, rule)) {
return false;
}

if (this.adapter && this.autoSave) {
if (useAdapter && this.adapter && this.autoSave) {
try {
await this.adapter.removePolicy(sec, ptype, rule);
} catch (e) {
Expand Down Expand Up @@ -183,14 +196,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 = true
): Promise<boolean> {
for (const rule of rules) {
if (!this.model.hasPolicy(sec, ptype, rule)) {
return false;
}
}

if (this.autoSave) {
if (useAdapter && this.autoSave) {
if ('removePolicies' in this.adapter) {
try {
await this.adapter.removePolicies(sec, ptype, rules);
Expand Down Expand Up @@ -230,9 +249,10 @@ export class InternalEnforcer extends CoreEnforcer {
ptype: string,
fieldIndex: number,
fieldValues: string[],
useWatcher: boolean
useWatcher: boolean,
useAdapter = true
): Promise<boolean> {
if (this.adapter && this.autoSave) {
if (useAdapter && this.adapter && this.autoSave) {
try {
await this.adapter.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);
} catch (e) {
Expand Down
90 changes: 82 additions & 8 deletions src/managementEnforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,27 +540,101 @@ export class ManagementEnforcer extends InternalEnforcer {
this.fm.addFunction(name, func);
}

/**
* selfAddPolicy adds a rule to the current policy (enforcer internal).
* This method does not use the adapter or watcher, even if autoSave and autoNotifyWatcher are enabled.
* It only updates the policy in the enforcer's memory.
* Useful in distributed setups with watchers to sync policies across instances without triggering
* duplicate saves or notifications.
*
* @param sec section, "p" or "g"
* @param ptype the policy type, can be "p", "p2", "p3", "g", "g2", etc.
* @param rule the policy rule
* @return succeeds or not
*/
public async selfAddPolicy(sec: string, ptype: string, rule: string[]): Promise<boolean> {
return this.addPolicyInternal(sec, ptype, rule, false);
return this.addPolicyInternal(sec, ptype, rule, false, false);
}

/**
* selfRemovePolicy removes a rule from the current policy (enforcer internal).
* This method does not use the adapter or watcher, even if autoSave and autoNotifyWatcher are enabled.
* It only updates the policy in the enforcer's memory.
* Useful in distributed setups with watchers to sync policies across instances without triggering
* duplicate saves or notifications.
*
* @param sec section, "p" or "g"
* @param ptype the policy type, can be "p", "p2", "p3", "g", "g2", etc.
* @param rule the policy rule
* @return succeeds or not
*/
public async selfRemovePolicy(sec: string, ptype: string, rule: string[]): Promise<boolean> {
return this.removePolicyInternal(sec, ptype, rule, false);
return this.removePolicyInternal(sec, ptype, rule, false, false);
}

/**
* selfRemoveFilteredPolicy removes rules based on field filters from the current policy (enforcer internal).
* This method does not use the adapter or watcher, even if autoSave and autoNotifyWatcher are enabled.
* It only updates the policy in the enforcer's memory.
* Useful in distributed setups with watchers to sync policies across instances without triggering
* duplicate saves or notifications.
*
* @param sec section, "p" or "g"
* @param ptype the policy type, can be "p", "p2", "p3", "g", "g2", etc.
* @param fieldIndex the policy rule's start index to be matched
* @param fieldValues the field values to be matched
* @return succeeds or not
*/
public async selfRemoveFilteredPolicy(sec: string, ptype: string, fieldIndex: number, fieldValues: string[]): Promise<boolean> {
return this.removeFilteredPolicyInternal(sec, ptype, fieldIndex, fieldValues, false);
return this.removeFilteredPolicyInternal(sec, ptype, fieldIndex, fieldValues, false, false);
}

/**
* selfUpdatePolicy updates a rule in the current policy (enforcer internal).
* This method does not use the adapter or watcher, even if autoSave and autoNotifyWatcher are enabled.
* It only updates the policy in the enforcer's memory.
* Useful in distributed setups with watchers to sync policies across instances without triggering
* duplicate saves or notifications.
*
* @param sec section, "p" or "g"
* @param ptype the policy type, can be "p", "p2", "p3", "g", "g2", etc.
* @param oldRule the old policy rule
* @param newRule the new policy rule
* @return succeeds or not
*/
public async selfUpdatePolicy(sec: string, ptype: string, oldRule: string[], newRule: string[]): Promise<boolean> {
return this.updatePolicyInternal(sec, ptype, oldRule, newRule, false);
return this.updatePolicyInternal(sec, ptype, oldRule, newRule, false, false);
}

public async selfAddPolicies(sec: string, ptype: string, rule: string[][]): Promise<boolean> {
return this.addPoliciesInternal(sec, ptype, rule, false);
/**
* selfAddPolicies adds rules to the current policy (enforcer internal).
* This method does not use the adapter or watcher, even if autoSave and autoNotifyWatcher are enabled.
* It only updates the policy in the enforcer's memory.
* Useful in distributed setups with watchers to sync policies across instances without triggering
* duplicate saves or notifications.
*
* @param sec section, "p" or "g"
* @param ptype the policy type, can be "p", "p2", "p3", "g", "g2", etc.
* @param rules the policy rules
* @return succeeds or not
*/
public async selfAddPolicies(sec: string, ptype: string, rules: string[][]): Promise<boolean> {
return this.addPoliciesInternal(sec, ptype, rules, false, false);
}

public async selfRemovePolicies(sec: string, ptype: string, rule: string[][]): Promise<boolean> {
return this.removePoliciesInternal(sec, ptype, rule, false);
/**
* selfRemovePolicies removes rules from the current policy (enforcer internal).
* This method does not use the adapter or watcher, even if autoSave and autoNotifyWatcher are enabled.
* It only updates the policy in the enforcer's memory.
* Useful in distributed setups with watchers to sync policies across instances without triggering
* duplicate saves or notifications.
*
* @param sec section, "p" or "g"
* @param ptype the policy type, can be "p", "p2", "p3", "g", "g2", etc.
* @param rules the policy rules
* @return succeeds or not
*/
public async selfRemovePolicies(sec: string, ptype: string, rules: string[][]): Promise<boolean> {
return this.removePoliciesInternal(sec, ptype, rules, false, false);
}
}
Loading
Loading