Skip to content

Commit

Permalink
Merge pull request #79 from nodece/master
Browse files Browse the repository at this point in the history
Improve matcher escaping
  • Loading branch information
hsluoyz authored Jul 6, 2019
2 parents 8c91862 + e78575f commit 3670495
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,40 @@ export class Model {

if (sec === 'r' || sec === 'p') {
const tokens = value.split(', ');

for (let i = 0; i < tokens.length; i++) {
tokens[i] = key + '_' + tokens[i];
}

ast.tokens = tokens;
} else if (sec === 'm') {
const stringArguments = value.match(/\"(.*?)\"/g) || [];

stringArguments.forEach((n, index) => {
value = value.replace(n, `$<${index}>`);
});

value = util.removeComments(util.escapeAssertion(value));

stringArguments.forEach((n, index) => {
value = value.replace(`$<${index}>`, n);
});

ast.value = value;
} else {
ast.value = util.removeComments(util.escapeAssertion(value));
}

const nodeMap = this.model.get(sec);

if (nodeMap) {
nodeMap.set(key, ast);
} else {
const assertionMap = new Map<string, Assertion>();
assertionMap.set(key, ast);
this.model.set(sec, assertionMap);
}

return true;
}

Expand Down
10 changes: 9 additions & 1 deletion test/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import * as _ from 'lodash';
import { newEnforcer, Enforcer } from '../src';
import { newEnforcer, Enforcer, newModel } from '../src';

async function testEnforce(e: Enforcer, sub: string, obj: any, act: string, res: boolean) {
await expect(e.enforce(sub, obj, act)).resolves.toBe(res);
Expand Down Expand Up @@ -274,3 +274,11 @@ test('TestPriorityModelIndeterminate', async () => {

await testEnforce(e, 'alice', 'data1', 'read', false);
});

test('TestMatcher', async () => {
const m = newModel();

m.addDef('m', 'm', 'keyMatch(r.obj, ".*get$") || regexMatch(r.act, ".user.")');

expect(m.model.get('m')!.get('m')!.value).toEqual(`keyMatch(r_obj, ".*get$") || regexMatch(r_act, ".user.")`);
});

0 comments on commit 3670495

Please sign in to comment.