From 56e55bd58b0f5be4a45e753f5ad58b01a27ef8b2 Mon Sep 17 00:00:00 2001 From: Shivansh Yadav Date: Sun, 16 Jan 2022 15:07:05 +0530 Subject: [PATCH] feat(batchenforce): added batchEnforce (#338) * 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 --- src/coreEnforcer.ts | 10 ++++++++++ test/enforcer.test.ts | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/src/coreEnforcer.ts b/src/coreEnforcer.ts index 94f2e96..0a20280 100644 --- a/src/coreEnforcer.ts +++ b/src/coreEnforcer.ts @@ -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 { + return await Promise.all(rvals.map((rval) => this.enforce(...rval))); + } } diff --git a/test/enforcer.test.ts b/test/enforcer.test.ts index e9c406a..4fd1c66 100644 --- a/test/enforcer.test.ts +++ b/test/enforcer.test.ts @@ -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]); +});