Skip to content
Open
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
86 changes: 71 additions & 15 deletions src/internalEnforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ import { PolicyOp } from './model';
export class InternalEnforcer extends CoreEnforcer {
/**
* addPolicyInternal adds a rule to the current policy.
* @param sec section name
* @param ptype policy type
* @param rule policy rule
* @param useWatcher whether to notify the watcher
* @param useAdapter whether to call the adapter to persist the policy (defaults to true)
*/
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 @@ -58,16 +63,29 @@ export class InternalEnforcer extends CoreEnforcer {
return ok;
}

// 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> {
/**
* addPolicies adds rules to the current policy.
* removePolicies removes rules from the current policy.
* @param sec section name
* @param ptype policy type
* @param rules policy rules
* @param useWatcher whether to notify the watcher
* @param useAdapter whether to call the adapter to persist the policies (defaults to true)
*/
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 @@ -101,19 +119,26 @@ export class InternalEnforcer extends CoreEnforcer {

/**
* updatePolicyInternal updates a rule from the current policy.
* @param sec section name
* @param ptype policy type
* @param oldRule old policy rule
* @param newRule new policy rule
* @param useWatcher whether to notify the watcher
* @param useAdapter whether to call the adapter to persist the policy update (defaults to true)
*/
protected async updatePolicyInternal(
sec: string,
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 @@ -148,13 +173,24 @@ export class InternalEnforcer extends CoreEnforcer {

/**
* removePolicyInternal removes a rule from the current policy.
* @param sec section name
* @param ptype policy type
* @param rule policy rule
* @param useWatcher whether to notify the watcher
* @param useAdapter whether to call the adapter to persist the policy removal (defaults to true)
*/
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 @@ -182,15 +218,28 @@ export class InternalEnforcer extends CoreEnforcer {
return ok;
}

// removePolicies removes rules from the current policy.
protected async removePoliciesInternal(sec: string, ptype: string, rules: string[][], useWatcher: boolean): Promise<boolean> {
/**
* removePolicies removes rules from the current policy.
* @param sec section name
* @param ptype policy type
* @param rules policy rules
* @param useWatcher whether to notify the watcher
* @param useAdapter whether to call the adapter to persist the policy removals (defaults to true)
*/
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 @@ -224,15 +273,22 @@ export class InternalEnforcer extends CoreEnforcer {

/**
* removeFilteredPolicyInternal removes rules based on field filters from the current policy.
* @param sec section name
* @param ptype policy type
* @param fieldIndex field index
* @param fieldValues field values
* @param useWatcher whether to notify the watcher
* @param useAdapter whether to call the adapter to persist the filtered policy removal (defaults to true)
*/
protected async removeFilteredPolicyInternal(
sec: string,
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
12 changes: 6 additions & 6 deletions src/managementEnforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,26 +541,26 @@ export class ManagementEnforcer extends InternalEnforcer {
}

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);
}

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);
}

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);
}

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);
return this.addPoliciesInternal(sec, ptype, rule, false, false);
}

public async selfRemovePolicies(sec: string, ptype: string, rule: string[][]): Promise<boolean> {
return this.removePoliciesInternal(sec, ptype, rule, false);
return this.removePoliciesInternal(sec, ptype, rule, false, false);
}
}
Loading
Loading