Skip to content

Commit 7030b25

Browse files
authored
Merge pull request #22 from DivyPatel9881/batch_operations
fix: Added Batch operations.
2 parents 63c4359 + 611ee3d commit 7030b25

File tree

2 files changed

+107
-2
lines changed

2 files changed

+107
-2
lines changed

src/adapter.ts

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import {Adapter, Helper, Model} from 'casbin';
1616
import {CasbinRule} from './casbinRule';
17-
import {Connection, ConnectionOptions, createConnection, getRepository} from 'typeorm';
17+
import {Connection, ConnectionOptions, createConnection, getRepository, getConnection} from 'typeorm';
1818
import {CasbinMongoRule} from './casbinMongoRule';
1919

2020
type GenericCasbinRule = CasbinRule | CasbinMongoRule;
@@ -130,7 +130,21 @@ export default class TypeORMAdapter implements Adapter {
130130
lines.push(line);
131131
}
132132
}
133-
await getRepository(this.getCasbinRuleConstructor(), this.option.name).save(lines);
133+
134+
const queryRunner = this.typeorm.createQueryRunner();
135+
136+
await queryRunner.connect();
137+
await queryRunner.startTransaction();
138+
139+
try {
140+
await queryRunner.manager.save(lines);
141+
await queryRunner.commitTransaction();
142+
} catch (err) {
143+
await queryRunner.rollbackTransaction();
144+
throw err;
145+
} finally {
146+
await queryRunner.release();
147+
}
134148

135149
return true;
136150
}
@@ -143,6 +157,32 @@ export default class TypeORMAdapter implements Adapter {
143157
await getRepository(this.getCasbinRuleConstructor(), this.option.name).save(line);
144158
}
145159

160+
/**
161+
* addPolicies adds policy rules to the storage.
162+
*/
163+
public async addPolicies(sec: string, ptype: string, rules: string[][]) {
164+
const lines: GenericCasbinRule[] = [];
165+
for (const rule of rules) {
166+
const line = this.savePolicyLine(ptype, rule);
167+
lines.push(line);
168+
}
169+
170+
const queryRunner = this.typeorm.createQueryRunner();
171+
172+
await queryRunner.connect();
173+
await queryRunner.startTransaction();
174+
175+
try {
176+
await queryRunner.manager.save(lines);
177+
await queryRunner.commitTransaction();
178+
} catch (err) {
179+
await queryRunner.rollbackTransaction();
180+
throw err;
181+
} finally {
182+
await queryRunner.release();
183+
}
184+
}
185+
146186
/**
147187
* removePolicy removes a policy rule from the storage.
148188
*/
@@ -151,6 +191,30 @@ export default class TypeORMAdapter implements Adapter {
151191
await getRepository(this.getCasbinRuleConstructor(), this.option.name).delete(line);
152192
}
153193

194+
/**
195+
* removePolicies removes policy rules from the storage.
196+
*/
197+
public async removePolicies(sec: string, ptype: string, rules: string[][]) {
198+
const queryRunner = this.typeorm.createQueryRunner();
199+
const type = TypeORMAdapter.getCasbinRuleType(this.option.type);
200+
201+
await queryRunner.connect();
202+
await queryRunner.startTransaction();
203+
204+
try {
205+
for (const rule of rules) {
206+
const line = this.savePolicyLine(ptype, rule);
207+
await queryRunner.manager.delete(type, line);
208+
}
209+
await queryRunner.commitTransaction();
210+
} catch (err) {
211+
await queryRunner.rollbackTransaction();
212+
throw err;
213+
} finally {
214+
await queryRunner.release();
215+
}
216+
}
217+
154218
/**
155219
* removeFilteredPolicy removes policy rules that match the filter from the storage.
156220
*/

test/adapter.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,50 @@ test('TestAdapter', async () => {
7676
['data2_admin', 'data2', 'write'],
7777
['role', 'res', 'action']]);
7878

79+
await a.addPolicies('', 'p', [
80+
['role1', 'res1', 'action1'],
81+
['role2', 'res2', 'action2'],
82+
['role3', 'res3', 'action3'],
83+
['role4', 'res4', 'action4'],
84+
['role5', 'res5', 'action5']
85+
]);
86+
e = await Enforcer.newEnforcer('examples/rbac_model.conf', a);
87+
testGetPolicy(e, [
88+
['alice', 'data1', 'read'],
89+
['bob', 'data2', 'write'],
90+
['data2_admin', 'data2', 'read'],
91+
['data2_admin', 'data2', 'write'],
92+
['role', 'res', 'action'],
93+
['role1', 'res1', 'action1'],
94+
['role2', 'res2', 'action2'],
95+
['role3', 'res3', 'action3'],
96+
['role4', 'res4', 'action4'],
97+
['role5', 'res5', 'action5']
98+
]);
99+
79100
// Remove policy from DB
80101
await a.removePolicy('', 'p', ['role', 'res', 'action']);
81102
e = await Enforcer.newEnforcer('examples/rbac_model.conf', a);
103+
testGetPolicy(e, [
104+
['alice', 'data1', 'read'],
105+
['bob', 'data2', 'write'],
106+
['data2_admin', 'data2', 'read'],
107+
['data2_admin', 'data2', 'write'],
108+
['role1', 'res1', 'action1'],
109+
['role2', 'res2', 'action2'],
110+
['role3', 'res3', 'action3'],
111+
['role4', 'res4', 'action4'],
112+
['role5', 'res5', 'action5']
113+
]);
114+
115+
await a.removePolicies('', 'p', [
116+
['role1', 'res1', 'action1'],
117+
['role2', 'res2', 'action2'],
118+
['role3', 'res3', 'action3'],
119+
['role4', 'res4', 'action4'],
120+
['role5', 'res5', 'action5']
121+
]);
122+
e = await Enforcer.newEnforcer('examples/rbac_model.conf', a);
82123
testGetPolicy(e, [
83124
['alice', 'data1', 'read'],
84125
['bob', 'data2', 'write'],

0 commit comments

Comments
 (0)