Skip to content

Commit 63a6305

Browse files
committed
Add batch check method
1 parent c581e75 commit 63a6305

File tree

3 files changed

+85
-29
lines changed

3 files changed

+85
-29
lines changed

src/modules/Authorization.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import {
2+
BatchCheckParams,
23
Check,
4+
CheckOp,
35
CheckParams,
46
CheckManyParams,
7+
CheckResult,
58
FeatureCheckParams,
69
PermissionCheckParams,
710
checkWarrantParamsToCheckWarrant,
@@ -39,6 +42,16 @@ export default class Authorization {
3942
return this.makeCheckRequest(check, options);
4043
}
4144

45+
public static async batchCheck(checkParams: BatchCheckParams, options: WarrantRequestOptions = {}): Promise<boolean[]> {
46+
const check: Check = {
47+
op: CheckOp.Batch,
48+
warrants: checkParams.warrants.map((checkWarrantParams) => checkWarrantParamsToCheckWarrant(checkWarrantParams)),
49+
debug: checkParams.debug
50+
};
51+
52+
return this.makeBatchCheckRequest(check, options);
53+
}
54+
4255
public static async hasFeature(featureCheckParams: FeatureCheckParams, options: WarrantRequestOptions = {}): Promise<boolean> {
4356
return this.check({
4457
object: {
@@ -94,4 +107,14 @@ export default class Authorization {
94107
throw e;
95108
}
96109
}
110+
111+
private static async makeBatchCheckRequest(check: Check, options: WarrantRequestOptions = {}): Promise<boolean[]> {
112+
const response = await WarrantClient.httpClient.post({
113+
url: `/${API_VERSION}/check`,
114+
data: check,
115+
options,
116+
});
117+
118+
return response.map((checkResult: CheckResult) => checkResult.code === 200);
119+
}
97120
}

src/types/Check.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
export enum CheckOp {
1313
AllOf = "allOf",
1414
AnyOf = "anyOf",
15+
Batch = "batch",
1516
}
1617

1718
export interface CheckWarrantParams {
@@ -31,6 +32,11 @@ export interface CheckManyParams {
3132
debug?: boolean;
3233
}
3334

35+
export interface BatchCheckParams {
36+
warrants: CheckWarrantParams[];
37+
debug?: boolean;
38+
}
39+
3440
export interface FeatureCheckParams {
3541
featureId: string;
3642
subject: WarrantObject | Subject;
@@ -68,3 +74,9 @@ export function checkWarrantParamsToCheckWarrant(checkWarrantParams: CheckWarran
6874
context: checkWarrantParams.context
6975
}
7076
}
77+
78+
export interface CheckResult {
79+
code?: number;
80+
result: string;
81+
isImplicit: boolean;
82+
}

test/LiveTest.spec.ts

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -743,28 +743,29 @@ describe.skip('Live Test', function () {
743743
assert(warrantToken);
744744
});
745745

746-
it('batch create/delete warrants', async function () {
746+
it('batch create/delete/check warrants', async function () {
747747
const newUser = await this.warrant.User.create();
748748
const permission1 = await this.warrant.Permission.create({ permissionId: "perm1", meta: { name: "Permission 1", description: "Permission 1" }});
749749
const permission2 = await this.warrant.Permission.create({ permissionId: "perm2", meta: { name: "Permission 2", description: "Permission 2" }});
750750

751-
let userHasPermission1 = await this.warrant.Authorization.check({
752-
object: permission1,
753-
relation: "member",
754-
subject: newUser
751+
let userHasPermissions = await this.warrant.Authorization.batchCheck({
752+
warrants: [
753+
{
754+
object: permission1,
755+
relation: "member",
756+
subject: newUser
757+
},
758+
{
759+
object: permission2,
760+
relation: "member",
761+
subject: newUser
762+
}
763+
]
755764
}, {
756765
warrantToken: "latest"
757766
});
758-
assert.strictEqual(userHasPermission1, false);
759-
760-
let userHasPermission2 = await this.warrant.Authorization.check({
761-
object: permission2,
762-
relation: "member",
763-
subject: newUser
764-
}, {
765-
warrantToken: "latest"
766-
});
767-
assert.strictEqual(userHasPermission2, false);
767+
assert.strictEqual(userHasPermissions[0], false);
768+
assert.strictEqual(userHasPermissions[1], false);
768769

769770
const warrants = await this.warrant.Warrant.batchCreate([
770771
{
@@ -783,23 +784,24 @@ describe.skip('Live Test', function () {
783784
assert(warrant.warrantToken);
784785
}
785786

786-
userHasPermission1 = await this.warrant.Authorization.check({
787-
object: permission1,
788-
relation: "member",
789-
subject: newUser
790-
}, {
791-
warrantToken: "latest"
792-
});
793-
assert.strictEqual(userHasPermission1, true);
794-
795-
userHasPermission2 = await this.warrant.Authorization.check({
796-
object: permission2,
797-
relation: "member",
798-
subject: newUser
787+
userHasPermissions = await this.warrant.Authorization.batchCheck({
788+
warrants: [
789+
{
790+
object: permission1,
791+
relation: "member",
792+
subject: newUser
793+
},
794+
{
795+
object: permission2,
796+
relation: "member",
797+
subject: newUser
798+
}
799+
]
799800
}, {
800801
warrantToken: "latest"
801802
});
802-
assert.strictEqual(userHasPermission2, true);
803+
assert.strictEqual(userHasPermissions[0], true);
804+
assert.strictEqual(userHasPermissions[1], true);
803805

804806
let warrantToken = await this.warrant.Warrant.batchDelete([
805807
{
@@ -820,6 +822,25 @@ describe.skip('Live Test', function () {
820822
{ object: { objectType: "user", objectId: newUser.userId } },
821823
]);
822824
assert(warrantToken);
825+
826+
userHasPermissions = await this.warrant.Authorization.batchCheck({
827+
warrants: [
828+
{
829+
object: permission1,
830+
relation: "member",
831+
subject: newUser
832+
},
833+
{
834+
object: permission2,
835+
relation: "member",
836+
subject: newUser
837+
}
838+
]
839+
}, {
840+
warrantToken: "latest"
841+
});
842+
assert.strictEqual(userHasPermissions[0], false);
843+
assert.strictEqual(userHasPermissions[1], false);
823844
});
824845

825846
it('warrant with policy', async function () {

0 commit comments

Comments
 (0)