Skip to content

Commit

Permalink
feat: introduce batch enforce json policies
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasadiyodi93 committed Feb 24, 2021
1 parent df24721 commit 935f4b8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
21 changes: 11 additions & 10 deletions app/user/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ export const getListWithFilters = async (policyFilters: JSObj) => {
const { resource = {}, action = {} } = extractResourceAction(policyFilters);

// 3) run each user through casbin enforcer based on the specifed params
const enforcedUsers = await Promise.all(
allUsersWithAllPolicies.map(async (user: any) => {
const hasAccess = await CasbinSingleton?.enforcer?.enforceJson(
{ user: user.id },
resource,
action
);
return R.assoc('hasAccess', hasAccess, user);
})
const policiesToBatchEnforce = allUsersWithAllPolicies.map((user: any) => ({
subject: { user: user.id },
resource,
action
}));
const batchEnforceResults = await CasbinSingleton?.enforcer?.batchEnforceJson(
policiesToBatchEnforce
);

const usersWithAccesss = enforcedUsers.filter((user: any) => user.hasAccess);
const usersWithAccesss = allUsersWithAllPolicies.filter(
(user: any, index: number) =>
batchEnforceResults && batchEnforceResults[index]
);

if (R.isEmpty(resource)) return usersWithAccesss;

Expand Down
19 changes: 17 additions & 2 deletions lib/casbin/JsonEnforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ export const convertJSONToStringInOrder = (
return JSON.stringify(orderedJSON);
};

type OneKey<K extends string> = Record<K, unknown>;
type JsonAttributes = Record<string, unknown>;
export type OneKey<K extends string> = Record<K, unknown>;
export type JsonAttributes = Record<string, unknown>;

export type PolicyObj = {
subject: JsonAttributes;
resource: JsonAttributes;
action: JsonAttributes;
};

export class JsonEnforcer extends CachedEnforcer {
constructor() {
Expand All @@ -35,6 +41,15 @@ export class JsonEnforcer extends CachedEnforcer {
);
}

public async batchEnforceJson(policies: PolicyObj[]) {
const enforceBatchResult = await Promise.all(
policies.map(async (policy: PolicyObj) =>
this.enforceJson(policy.subject, policy.resource, policy.action)
)
);
return enforceBatchResult;
}

public async addJsonPolicy(
subject: JsonAttributes,
resource: JsonAttributes,
Expand Down
20 changes: 16 additions & 4 deletions lib/casbin/JsonFilteredEnforcer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* eslint-disable class-methods-use-this */
import { createQueryBuilder, In, Like } from 'typeorm';
import { newEnforcerWithClass } from 'casbin';
import { convertJSONToStringInOrder, JsonEnforcer } from './JsonEnforcer';

type JsonAttributes = Record<string, unknown>;
type OneKey<K extends string> = Record<K, unknown>;
import {
convertJSONToStringInOrder,
JsonEnforcer,
JsonAttributes,
OneKey,
PolicyObj
} from './JsonEnforcer';

export class JsonFilteredEnforcer extends JsonEnforcer {
public static params: any[];
Expand Down Expand Up @@ -70,6 +73,15 @@ export class JsonFilteredEnforcer extends JsonEnforcer {
return hasAccess;
}

public async batchEnforceJson(policies: PolicyObj[]) {
const enforceBatchResult = await Promise.all(
policies.map(async (policy: PolicyObj) =>
this.enforceJson(policy.subject, policy.resource, policy.action)
)
);
return enforceBatchResult;
}

public async addJsonPolicy(
subject: JsonAttributes,
resource: JsonAttributes,
Expand Down
6 changes: 3 additions & 3 deletions test/app/user/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ lab.experiment('User::resource', () => {
const result = (await Resource.list(filter)).map(removeTimestamps);

const expectedResult = [
{ ...users[0], policies: [], hasAccess: true },
{ ...users[1], policies: [], hasAccess: true },
{ ...users[2], policies: [userEntityPolicy], hasAccess: true }
{ ...users[0], policies: [] },
{ ...users[1], policies: [] },
{ ...users[2], policies: [userEntityPolicy] }
].map(removeTimestamps);

// ? We need to sort before checking because [1, 2, 3] != [2, 1, 3]
Expand Down

0 comments on commit 935f4b8

Please sign in to comment.