Skip to content

Commit

Permalink
fix: add full supoort for in operator (#310)
Browse files Browse the repository at this point in the history
Signed-off-by: Zxilly <[email protected]>
  • Loading branch information
Zxilly authored Aug 13, 2021
1 parent 7d9599f commit 446f8c7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/in_operator_model.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ p = sub, obj, act
e = some(where (p.eft == allow))

[matchers]
m = r.sub.Owner == r.obj.Owner && r.sub.Doc in(r.obj.Docs)
m = r.sub.Owner == r.obj.Owner && r.sub.Doc in (r.obj.Docs)
15 changes: 11 additions & 4 deletions src/coreEnforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ import { DefaultEffector, Effect, Effector } from './effect';
import { FunctionMap, Model, newModel, PolicyOp } from './model';
import { Adapter, FilteredAdapter, Watcher, BatchAdapter, UpdatableAdapter } from './persist';
import { DefaultRoleManager, RoleManager } from './rbac';
import { escapeAssertion, generateGFunction, getEvalValue, hasEval, replaceEval, generatorRunSync, generatorRunAsync } from './util';
import {
escapeAssertion,
generateGFunction,
getEvalValue,
hasEval,
replaceEval,
generatorRunSync,
generatorRunAsync,
customIn,
} from './util';
import { getLogger, logPrint } from './log';
import { MatchingFunc } from './rbac';

Expand Down Expand Up @@ -48,9 +57,7 @@ export class CoreEnforcer {
private getExpression(asyncCompile: boolean, exp: string): Matcher {
const matcherKey = `${asyncCompile ? 'ASYNC[' : 'SYNC['}${exp}]`;

addBinaryOp('in', 1, (a, b) => {
return (((b as unknown) as Array<any>).includes(a) as unknown) as number;
});
addBinaryOp('in', 1, customIn);

let expression = this.matcherMap.get(matcherKey);
if (!expression) {
Expand Down
8 changes: 8 additions & 0 deletions src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ function deepCopy(obj: Array<any> | any): any {
return newObj;
}

function customIn(a: number | string, b: number | string): number {
if ((b as any) instanceof Array) {
return (((b as any) as Array<any>).includes(a) as unknown) as number;
}
return ((a in (b as any)) as unknown) as number;
}

export {
escapeAssertion,
removeComments,
Expand All @@ -187,4 +194,5 @@ export {
generatorRunSync,
generatorRunAsync,
deepCopy,
customIn,
};
12 changes: 6 additions & 6 deletions test/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,26 +425,26 @@ test('ABACModelWithInOperator', async () => {

class TestRule1 {
public Owner: string;
public Doc: number;
public Doc: string;

constructor(Owner: string, Doc: number) {
constructor(Owner: string, Doc: string) {
this.Owner = Owner;
this.Doc = Doc;
}
}

class TestRule2 {
public Owner: string;
public Docs: Array<number>;
public Docs: Array<string>;

constructor(Owner: string, Doc: Array<number>) {
constructor(Owner: string, Doc: string[]) {
this.Owner = Owner;
this.Docs = Doc;
}
}

const rule1 = new TestRule1('alice', 1);
const rule2 = new TestRule2('alice', [1, 2]);
const rule1 = new TestRule1('alice', 'aa');
const rule2 = new TestRule2('alice', ['aa', 'bb']);

await expect(e.enforce(rule1, rule2)).resolves.toBe(true);
});

0 comments on commit 446f8c7

Please sign in to comment.