Skip to content

Commit

Permalink
fix: correct policy removal for filtered enforcer
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasadiyodi93 committed Feb 24, 2021
1 parent 7d0c456 commit df24721
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
42 changes: 24 additions & 18 deletions lib/casbin/JsonFilteredEnforcer.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
/* eslint-disable class-methods-use-this */
/* eslint-disable max-classes-per-file */
import { createQueryBuilder, In, Like } from 'typeorm';
import { newEnforcerWithClass } from 'casbin';
// eslint-disable-next-line import/no-cycle
import { convertJSONToStringInOrder, JsonEnforcer } from './JsonEnforcer';

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

export class JsonFilteredEnforcer extends JsonEnforcer {
private static params: any[];
public static params: any[];

public static setParams(params: any[]) {
this.params = params;
}

public static async getEnforcer() {
return newEnforcerWithClass(JsonEnforcer, ...JsonFilteredEnforcer.params);
private async getEnforcer() {
const enforcer = await newEnforcerWithClass(
JsonEnforcer,
...JsonFilteredEnforcer.params
);
enforcer.enableAutoSave(true);
enforcer.enableLog(false);
return enforcer;
}

public async loadPolicySubset(policyObj: any, enforcer: JsonEnforcer) {
private async loadPolicySubset(policyObj: any, enforcer: JsonEnforcer) {
const rawSubjects = await createQueryBuilder()
.select('casbin_rule.v1')
.from('casbin_rule', 'casbin_rule')
Expand Down Expand Up @@ -51,8 +55,7 @@ export class JsonFilteredEnforcer extends JsonEnforcer {
action: JsonAttributes
): Promise<boolean> {
// intantiate new enforcer

const enforcer = await JsonFilteredEnforcer.getEnforcer();
const enforcer = await this.getEnforcer();

// load filtered policy
await this.loadPolicySubset({ subject, resource, action }, enforcer);
Expand All @@ -72,7 +75,7 @@ export class JsonFilteredEnforcer extends JsonEnforcer {
resource: JsonAttributes,
action: JsonAttributes
) {
const enforcer = await JsonFilteredEnforcer.getEnforcer();
const enforcer = await this.getEnforcer();
await enforcer.addPolicy(
convertJSONToStringInOrder(subject),
convertJSONToStringInOrder(resource),
Expand All @@ -81,7 +84,7 @@ export class JsonFilteredEnforcer extends JsonEnforcer {
}

public async addStrPolicy(subject: string, resource: string, action: string) {
const enforcer = await JsonFilteredEnforcer.getEnforcer();
const enforcer = await this.getEnforcer();
await enforcer.addPolicy(subject, resource, action);
}

Expand All @@ -90,8 +93,10 @@ export class JsonFilteredEnforcer extends JsonEnforcer {
resource: JsonAttributes,
action: JsonAttributes
) {
const enforcer = await JsonFilteredEnforcer.getEnforcer();
await enforcer.removePolicy(
const enforcer = await this.getEnforcer();
await enforcer.removeFilteredNamedPolicy(
'p',
0,
convertJSONToStringInOrder(subject),
convertJSONToStringInOrder(resource),
convertJSONToStringInOrder(action)
Expand All @@ -102,7 +107,7 @@ export class JsonFilteredEnforcer extends JsonEnforcer {
subject: OneKey<T>,
jsonAttributes: JsonAttributes
) {
const enforcer = await JsonFilteredEnforcer.getEnforcer();
const enforcer = await this.getEnforcer();
await enforcer.addNamedGroupingPolicy(
'g',
convertJSONToStringInOrder(subject),
Expand All @@ -115,9 +120,10 @@ export class JsonFilteredEnforcer extends JsonEnforcer {
subject: OneKey<T>,
jsonAttributes: JsonAttributes
) {
const enforcer = await JsonFilteredEnforcer.getEnforcer();
await enforcer.removeNamedGroupingPolicy(
const enforcer = await this.getEnforcer();
await enforcer.removeFilteredNamedGroupingPolicy(
'g',
0,
convertJSONToStringInOrder(subject),
convertJSONToStringInOrder(jsonAttributes),
'subject'
Expand All @@ -128,7 +134,7 @@ export class JsonFilteredEnforcer extends JsonEnforcer {
resource: OneKey<T>,
jsonAttributes: JsonAttributes
) {
const enforcer = await JsonFilteredEnforcer.getEnforcer();
const enforcer = await this.getEnforcer();
await enforcer.addNamedGroupingPolicy(
'g2',
convertJSONToStringInOrder(resource),
Expand All @@ -149,7 +155,7 @@ export class JsonFilteredEnforcer extends JsonEnforcer {
public async removeAllResourceGroupingJsonPolicy<T extends string>(
resource: OneKey<T>
) {
const enforcer = await JsonFilteredEnforcer.getEnforcer();
const enforcer = await this.getEnforcer();
await enforcer.removeFilteredNamedGroupingPolicy(
'g2',
0,
Expand All @@ -161,7 +167,7 @@ export class JsonFilteredEnforcer extends JsonEnforcer {
action: OneKey<T>,
jsonAttributes: JsonAttributes
) {
const enforcer = await JsonFilteredEnforcer.getEnforcer();
const enforcer = await this.getEnforcer();
await enforcer.addNamedGroupingPolicy(
'g3',
convertJSONToStringInOrder(action),
Expand Down
2 changes: 0 additions & 2 deletions lib/casbin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ class CasbinSingleton {
policyAdapter: any
) {
this.enforcer = await newJsonFilteredEnforcer(modelPath, policyAdapter);
this.enforcer.enableAutoSave(true);
this.enforcer.enableLog(false);
}

public static async create(dbConnectionUrl: string) {
Expand Down

0 comments on commit df24721

Please sign in to comment.