Skip to content

Commit 8ef3deb

Browse files
feat: ensure eft and priority are respected in p2, r2, etc (#475)
In a few places, it was still hardcoded to only look for the priority and effect of p, instead of using the ptype variable. Fixed so that it now has the same behaviour regardless of the order fix #474
1 parent c9914bd commit 8ef3deb

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/coreEnforcer.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,18 @@ export class CoreEnforcer {
217217
}
218218

219219
public sortPolicies(): void {
220-
const policy = this.model.model.get('p')?.get('p')?.policy;
221-
const tokens = this.model.model.get('p')?.get('p')?.tokens;
222-
223-
if (policy && tokens) {
224-
const priorityIndex = tokens.indexOf('p_priority');
225-
if (priorityIndex !== -1) {
226-
policy.sort((a, b) => {
227-
return parseInt(a[priorityIndex], 10) - parseInt(b[priorityIndex], 10);
228-
});
220+
this.model.model.get('p')?.forEach((value, key) => {
221+
const policy = value.policy;
222+
const tokens = value.tokens;
223+
if (policy && tokens) {
224+
const priorityIndex = tokens.indexOf(`${key}_priority`);
225+
if (priorityIndex !== -1) {
226+
policy.sort((a, b) => {
227+
return parseInt(a[priorityIndex], 10) - parseInt(b[priorityIndex], 10);
228+
});
229+
}
229230
}
230-
}
231+
});
231232
}
232233

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

531-
const eft = parameters['p_eft'];
532+
const eft = parameters[`${enforceContext.pType}_eft`];
532533
if (eft && eftRes === Effect.Allow) {
533534
if (eft === 'allow') {
534535
eftRes = Effect.Allow;

src/model/model.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export class Model {
251251
const policy = ast.policy;
252252
const tokens = ast.tokens;
253253

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

256256
if (priorityIndex !== -1) {
257257
const priorityRule = rule[priorityIndex];
@@ -284,7 +284,7 @@ export class Model {
284284
}
285285
}
286286

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

289289
if (priorityFlag) {
290290
rules.forEach((rule) => {
@@ -309,7 +309,7 @@ export class Model {
309309
return false;
310310
}
311311

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

314314
if (priorityIndex !== -1) {
315315
if (oldRule[priorityIndex] === newRule[priorityIndex]) {

0 commit comments

Comments
 (0)