Skip to content

Commit

Permalink
feat: create new CasbinRule instance every time sequelize Adapter ope…
Browse files Browse the repository at this point in the history
…ns connection (#80)

* fix: create new CasbinRule instance every time sequelize Adapter opens connection

* fix: indentation

---------

Co-authored-by: tsoultanopoulos <tsoultanopoulos@wings-ict-solutions.eu>
  • Loading branch information
tsoultanopoulos1991 and tsoultanopoulos authored Jun 23, 2023
1 parent 1724f2e commit 4dccf8d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
30 changes: 17 additions & 13 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import { Adapter, Helper, Model } from 'casbin';
import { Op } from 'sequelize';
import { Sequelize, SequelizeOptions } from 'sequelize-typescript';
import { CasbinRule, updateCasbinRule } from './casbinRule';
import { createCasbinRule, CasbinRule } from './casbinRule';

export interface SequelizeAdapterOptions extends SequelizeOptions {
tableName?: string;
Expand All @@ -30,6 +30,7 @@ export class SequelizeAdapter implements Adapter {
private sequelize: Sequelize;
private filtered = false;
private autoCreateTable = true;
private CasbinRule: typeof CasbinRule;

constructor(option: SequelizeAdapterOptions, autoCreateTable = true) {
this.option = option;
Expand Down Expand Up @@ -60,9 +61,12 @@ export class SequelizeAdapter implements Adapter {

private async open(): Promise<void> {
this.sequelize = new Sequelize(this.option);
updateCasbinRule(this.option.tableName, this.option.schema);
this.CasbinRule = createCasbinRule(
this.option.tableName,
this.option.schema
); // Set the property here
await this.sequelize.authenticate();
this.sequelize.addModels([CasbinRule]);
this.sequelize.addModels([this.CasbinRule]);
if (this.autoCreateTable) {
await this.createTable();
}
Expand Down Expand Up @@ -90,15 +94,15 @@ export class SequelizeAdapter implements Adapter {
* loadPolicy loads all policy rules from the storage.
*/
public async loadPolicy(model: Model): Promise<void> {
const lines = await this.sequelize.getRepository(CasbinRule).findAll();
const lines = await this.sequelize.getRepository(this.CasbinRule).findAll();

for (const line of lines) {
this.loadPolicyLine(line, model);
}
}

private savePolicyLine(ptype: string, rule: string[]): CasbinRule {
const line = new CasbinRule();
const line = new this.CasbinRule();

line.ptype = ptype;
if (rule.length > 0) {
Expand Down Expand Up @@ -130,7 +134,7 @@ export class SequelizeAdapter implements Adapter {
await this.sequelize.transaction(async (tx) => {
// truncate casbin table
await this.sequelize
.getRepository(CasbinRule)
.getRepository(this.CasbinRule)
.destroy({ where: {}, truncate: true, transaction: tx });

const lines: CasbinRule[] = [];
Expand All @@ -151,7 +155,7 @@ export class SequelizeAdapter implements Adapter {
}
}

await CasbinRule.bulkCreate(
await this.CasbinRule.bulkCreate(
lines.map((l) => l.get({ plain: true })),
{ transaction: tx }
);
Expand Down Expand Up @@ -185,7 +189,7 @@ export class SequelizeAdapter implements Adapter {
lines.push(line);
}
await this.sequelize.transaction(async (tx) => {
await CasbinRule.bulkCreate(
await this.CasbinRule.bulkCreate(
lines.map((l) => l.get({ plain: true })),
{ transaction: tx }
);
Expand All @@ -210,7 +214,7 @@ export class SequelizeAdapter implements Adapter {
where[key] = line[key];
});

await this.sequelize.getRepository(CasbinRule).destroy({ where });
await this.sequelize.getRepository(this.CasbinRule).destroy({ where });
}

/**
Expand All @@ -234,7 +238,7 @@ export class SequelizeAdapter implements Adapter {
});

await this.sequelize
.getRepository(CasbinRule)
.getRepository(this.CasbinRule)
.destroy({ where, transaction: tx });
}
});
Expand Down Expand Up @@ -271,7 +275,7 @@ export class SequelizeAdapter implements Adapter {
};

const lines = await this.sequelize
.getRepository(CasbinRule)
.getRepository(this.CasbinRule)
.findAll({ where });

lines.forEach((line) => this.loadPolicyLine(line, model));
Expand All @@ -287,7 +291,7 @@ export class SequelizeAdapter implements Adapter {
fieldIndex: number,
...fieldValues: string[]
): Promise<void> {
const line = new CasbinRule();
const line = new this.CasbinRule();
line.ptype = ptype;

const idx = fieldIndex + fieldValues.length;
Expand Down Expand Up @@ -319,7 +323,7 @@ export class SequelizeAdapter implements Adapter {
where[key] = line[key];
});

await this.sequelize.getRepository(CasbinRule).destroy({
await this.sequelize.getRepository(this.CasbinRule).destroy({
where,
});
}
Expand Down
12 changes: 8 additions & 4 deletions src/casbinRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ export class CasbinRule extends Model<CasbinRule> {
public v5: string;
}

export function updateCasbinRule(
export function createCasbinRule(
tableName = 'casbin_rule',
schema?: string
): void {
const options = getOptions(CasbinRule.prototype);
): typeof CasbinRule {
class CustomCasbinRule extends CasbinRule {}

const options = getOptions(CustomCasbinRule.prototype);
options!.tableName = tableName;
options!.schema = schema;
setOptions(CasbinRule.prototype, options!);
setOptions(CustomCasbinRule.prototype, options!);

return CustomCasbinRule;
}

0 comments on commit 4dccf8d

Please sign in to comment.