Skip to content

Commit

Permalink
feat(batchenforce): added batchEnforce (#338)
Browse files Browse the repository at this point in the history
* feat(batchenforce): added batchEnforce

Added batchEnforce to enforce multiple requests

fix ##321

* fix: fixed lint issues

fixed lint errors while running tests

* fix: lint error fixed

lint error fixed

* feat: requested changes

made requested changes

321

* feat: made requested changes

made requested changes

321
  • Loading branch information
Shivansh-yadav13 authored Jan 16, 2022
1 parent ed98a18 commit 56e55bd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/coreEnforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,4 +588,14 @@ export class CoreEnforcer {
public async enforceEx(...rvals: any[]): Promise<[boolean, string[]]> {
return generatorRunAsync(this.privateEnforce(true, true, ...rvals));
}

/**
* batchEnforce enforces each request and returns result in a bool array.
* @param rvals the request need to be mediated, usually an array
* of array of strings, can be class instances if ABAC is used.
* @returns whether to allow the requests.
*/
public async batchEnforce(rvals: any[]): Promise<boolean[]> {
return await Promise.all(rvals.map((rval) => this.enforce(...rval)));
}
}
9 changes: 9 additions & 0 deletions test/enforcer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,3 +650,12 @@ test('Test RBAC G2', async () => {
expect(await e.enforce('alice', 'data1', 'read')).toBe(false);
expect(await e.enforce('admin', 'data1', 'read')).toBe(true);
});

test('TestBatchEnforce', async () => {
const e = await newEnforcer('examples/basic_model.conf', 'examples/basic_policy.csv');
const requests: string[][] = [
['alice', 'data1', 'read'],
['bob', 'data2', 'write'],
];
expect(await e.batchEnforce(requests)).toEqual([true, true]);
});

0 comments on commit 56e55bd

Please sign in to comment.