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]); +});