Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ensure eft and priority are respected in p2, r2, etc #475

Merged
merged 1 commit into from
Apr 26, 2024
Merged
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
23 changes: 12 additions & 11 deletions src/coreEnforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,18 @@ export class CoreEnforcer {
}

public sortPolicies(): void {
const policy = this.model.model.get('p')?.get('p')?.policy;
const tokens = this.model.model.get('p')?.get('p')?.tokens;

if (policy && tokens) {
const priorityIndex = tokens.indexOf('p_priority');
if (priorityIndex !== -1) {
policy.sort((a, b) => {
return parseInt(a[priorityIndex], 10) - parseInt(b[priorityIndex], 10);
});
this.model.model.get('p')?.forEach((value, key) => {
const policy = value.policy;
const tokens = value.tokens;
if (policy && tokens) {
const priorityIndex = tokens.indexOf(`${key}_priority`);
if (priorityIndex !== -1) {
policy.sort((a, b) => {
return parseInt(a[priorityIndex], 10) - parseInt(b[priorityIndex], 10);
});
}
}
}
});
}

/**
Expand Down Expand Up @@ -528,7 +529,7 @@ export class CoreEnforcer {
throw new Error('matcher result should only be of type boolean, number, or string');
}

const eft = parameters['p_eft'];
const eft = parameters[`${enforceContext.pType}_eft`];
if (eft && eftRes === Effect.Allow) {
if (eft === 'allow') {
eftRes = Effect.Allow;
Expand Down
6 changes: 3 additions & 3 deletions src/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export class Model {
const policy = ast.policy;
const tokens = ast.tokens;

const priorityIndex = tokens.indexOf('p_priority');
const priorityIndex = tokens.indexOf(`${key}_priority`);

if (priorityIndex !== -1) {
const priorityRule = rule[priorityIndex];
Expand Down Expand Up @@ -284,7 +284,7 @@ export class Model {
}
}

const priorityFlag = ast.tokens.indexOf('p_priority') !== -1;
const priorityFlag = ast.tokens.indexOf(`${ptype}_priority`) !== -1;

if (priorityFlag) {
rules.forEach((rule) => {
Expand All @@ -309,7 +309,7 @@ export class Model {
return false;
}

const priorityIndex = ast.tokens.indexOf('p_priority');
const priorityIndex = ast.tokens.indexOf(`${ptype}_priority`);

if (priorityIndex !== -1) {
if (oldRule[priorityIndex] === newRule[priorityIndex]) {
Expand Down
Loading