Skip to content

Commit 612a9c9

Browse files
fix: Fix failing typeorm-adapter with ACL model (#61)
Signed-off-by: Oleksandr Andriienko <[email protected]>
1 parent 0d056ab commit 612a9c9

File tree

7 files changed

+153
-6
lines changed

7 files changed

+153
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_modules
44
lib
55
yarn-error.log
66
package-lock.json
7+
coverage

examples/acl_model.conf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[request_definition]
2+
r = sub, obj, act
3+
4+
[policy_definition]
5+
p = sub, obj, act
6+
7+
[policy_effect]
8+
e = some(where (p.eft == allow))
9+
10+
[matchers]
11+
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act

examples/acl_policy.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
p, alice, data1, read
2+
p, bob, data2, write

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"precommit": "lint-staged",
99
"prepublish": "yarn run lint && yarn build",
1010
"build": "rimraf lib && tsc",
11-
"coverage": "jest --coverage",
11+
"coverage": "jest --coverage --runInBand",
1212
"lint": "tslint \"src/**/*.ts\"",
1313
"fix": "tslint \"src/**/*.ts\" --fix",
1414
"test": "jest --runInBand",

src/adapter.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,13 @@ export default class TypeORMAdapter implements FilteredAdapter {
195195
}
196196

197197
astMap = model.model.get('g');
198-
// @ts-ignore
199-
for (const [ptype, ast] of astMap) {
200-
for (const rule of ast.policy) {
201-
const line = this.savePolicyLine(ptype, rule);
202-
lines.push(line);
198+
if (astMap) {
199+
// @ts-ignore
200+
for (const [ptype, ast] of astMap) {
201+
for (const rule of ast.policy) {
202+
const line = this.savePolicyLine(ptype, rule);
203+
lines.push(line);
204+
}
203205
}
204206
}
205207

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright 2018 The Casbin Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import { Enforcer, Util } from 'casbin';
16+
import TypeORMAdapter from '../src/index';
17+
import { connectionConfig } from './config';
18+
19+
test(
20+
'TestAdapter',
21+
async () => {
22+
const a = await TypeORMAdapter.newAdapter(connectionConfig);
23+
try {
24+
// Because the DB is empty at first,
25+
// so we need to load the policy from the file adapter (.CSV) first.
26+
let e = new Enforcer();
27+
28+
await e.initWithFile(
29+
'examples/acl_model.conf',
30+
'examples/acl_policy.csv',
31+
);
32+
33+
// This is a trick to save the current policy to the DB.
34+
// We can't call e.savePolicy() because the adapter in the enforcer is still the file adapter.
35+
// The current policy means the policy in the Node-Casbin enforcer (aka in memory).
36+
await a.savePolicy(e.getModel());
37+
38+
// Clear the current policy.
39+
e.clearPolicy();
40+
expect(await e.getPolicy()).toEqual([]);
41+
42+
// Load the policy from DB.
43+
await a.loadPolicy(e.getModel());
44+
expect(await e.getPolicy()).toEqual([
45+
['alice', 'data1', 'read'],
46+
['bob', 'data2', 'write'],
47+
]);
48+
49+
// Note: you don't need to look at the above code
50+
// if you already have a working DB with policy inside.
51+
52+
// Now the DB has policy, so we can provide a normal use case.
53+
// Create an adapter and an enforcer.
54+
// newEnforcer() will load the policy automatically.
55+
e = new Enforcer();
56+
await e.initWithAdapter('examples/rbac_model.conf', a);
57+
expect(await e.getPolicy()).toEqual([
58+
['alice', 'data1', 'read'],
59+
['bob', 'data2', 'write'],
60+
]);
61+
62+
// load filtered policies
63+
e.clearPolicy();
64+
await a.loadFilteredPolicy(e.getModel(), { ptype: 'p', v0: 'alice' });
65+
expect(await e.getFilteredNamedPolicy('p', 0, 'alice')).toEqual([
66+
['alice', 'data1', 'read'],
67+
]);
68+
69+
// Add policy to DB
70+
await a.addPolicy('', 'p', ['role', 'res', 'action']);
71+
e = new Enforcer();
72+
await e.initWithAdapter('examples/rbac_model.conf', a);
73+
expect(await e.getPolicy()).toEqual([
74+
['alice', 'data1', 'read'],
75+
['bob', 'data2', 'write'],
76+
['role', 'res', 'action'],
77+
]);
78+
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 = new Enforcer();
87+
await e.initWithAdapter('examples/rbac_model.conf', a);
88+
expect(await e.getPolicy()).toEqual([
89+
['alice', 'data1', 'read'],
90+
['bob', 'data2', 'write'],
91+
['role', 'res', 'action'],
92+
['role1', 'res1', 'action1'],
93+
['role2', 'res2', 'action2'],
94+
['role3', 'res3', 'action3'],
95+
['role4', 'res4', 'action4'],
96+
['role5', 'res5', 'action5'],
97+
]);
98+
99+
// Remove policy from DB
100+
await a.removePolicy('', 'p', ['role', 'res', 'action']);
101+
e = new Enforcer();
102+
await e.initWithAdapter('examples/rbac_model.conf', a);
103+
expect(await e.getPolicy()).toEqual([
104+
['alice', 'data1', 'read'],
105+
['bob', 'data2', 'write'],
106+
['role1', 'res1', 'action1'],
107+
['role2', 'res2', 'action2'],
108+
['role3', 'res3', 'action3'],
109+
['role4', 'res4', 'action4'],
110+
['role5', 'res5', 'action5'],
111+
]);
112+
113+
await a.removePolicies('', 'p', [
114+
['role1', 'res1', 'action1'],
115+
['role2', 'res2', 'action2'],
116+
['role3', 'res3', 'action3'],
117+
['role4', 'res4', 'action4'],
118+
['role5', 'res5', 'action5'],
119+
]);
120+
e = new Enforcer();
121+
await e.initWithAdapter('examples/rbac_model.conf', a);
122+
expect(await e.getPolicy()).toEqual([
123+
['alice', 'data1', 'read'],
124+
['bob', 'data2', 'write'],
125+
]);
126+
} finally {
127+
a.close();
128+
}
129+
},
130+
60 * 1000,
131+
);
File renamed without changes.

0 commit comments

Comments
 (0)