Skip to content

Commit 9f12511

Browse files
committed
fix: improve load policy line
Signed-off-by: Zixuan Liu <nodeces@gmail.com>
1 parent 76e607b commit 9f12511

File tree

7 files changed

+84
-11
lines changed

7 files changed

+84
-11
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
},
5353
"dependencies": {
5454
"await-lock": "^2.0.1",
55+
"csv-parse": "^4.15.3",
5556
"expression-eval": "^2.0.0",
5657
"ip": "^1.1.5",
5758
"micromatch": "^4.0.2"

src/persist/defaultFilteredAdapter.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class DefaultFilteredAdapter extends FileAdapter implements FilteredAdapt
4141
const bodyBuf = await readFile(this.filePath);
4242
const lines = bodyBuf.toString().split('\n');
4343
lines.forEach((n: string, index: number) => {
44-
const line = n.trim();
44+
const line = n;
4545
if (!line || DefaultFilteredAdapter.filterLine(line, filter)) {
4646
return;
4747
}
@@ -88,7 +88,7 @@ export class DefaultFilteredAdapter extends FileAdapter implements FilteredAdapt
8888
}
8989
let skipLine = false;
9090
for (let i = 0; i < filter.length; i++) {
91-
if (filter[i] && filter[i].trim() !== filter[i + 1].trim()) {
91+
if (filter[i] && filter[i] !== filter[i + 1]) {
9292
skipLine = true;
9393
break;
9494
}

src/persist/fileAdapter.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Adapter } from './adapter';
22
import { Model } from '../model';
33
import { Helper } from './helper';
4-
import { readFile, writeFile, arrayToString } from '../util';
4+
import { arrayToString, readFile, writeFile } from '../util';
55

66
/**
77
* FileAdapter is the file adapter for Casbin.
@@ -30,8 +30,7 @@ export class FileAdapter implements Adapter {
3030
const bodyBuf = await readFile(this.filePath);
3131
const lines = bodyBuf.toString().split('\n');
3232
lines.forEach((n: string, index: number) => {
33-
const line = n.trim();
34-
if (!line) {
33+
if (!n) {
3534
return;
3635
}
3736
handler(n, model);

src/persist/helper.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
import { Model } from '../model';
2+
import * as parse from 'csv-parse/lib/sync';
23

34
export class Helper {
45
public static loadPolicyLine(line: string, model: Model): void {
5-
if (!line || line.trim() === '' || line.charAt(0) === '#') {
6+
if (!line || line.trimStart().charAt(0) === '#') {
67
return;
78
}
89

9-
const tokens = line.split(',').map((n) => n.trim());
10-
const key = tokens[0];
10+
const tokens = parse(line, {
11+
delimiter: ',',
12+
skip_empty_lines: true,
13+
trim: true,
14+
});
15+
16+
if (!tokens || !tokens[0]) {
17+
return;
18+
}
19+
20+
const key = tokens[0][0];
1121
const sec = key.substring(0, 1);
1222
const item = model.model.get(sec);
1323
if (!item) {
@@ -18,6 +28,6 @@ export class Helper {
1828
if (!policy) {
1929
return;
2030
}
21-
policy.policy.push(tokens.slice(1));
31+
policy.policy.push(tokens[0].slice(1));
2232
}
2333
}

src/persist/stringAdapter.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ export class StringAdapter implements Adapter {
2828
private async loadRules(model: Model, handler: (line: string, model: Model) => void): Promise<void> {
2929
const rules = this.policy.split('\n');
3030
rules.forEach((n: string, index: number) => {
31-
const line = n.trim();
32-
if (!line) {
31+
if (!n) {
3332
return;
3433
}
3534
handler(n, model);

test/persist/helper.test.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2021 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 { Helper, newModel } from '../../src';
16+
17+
test('test Helper.loadPolicyLine', async () => {
18+
const m = newModel();
19+
m.loadModelFromText(`
20+
[request_definition]
21+
r = sub, obj, act
22+
23+
[policy_definition]
24+
p = sub, obj, act
25+
26+
[policy_effect]
27+
e = some(where (p.eft == allow))
28+
29+
[matchers]
30+
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
31+
`);
32+
33+
const testdata = [
34+
'p, admin, /, GET',
35+
'# test comment 1',
36+
' # test comment 2',
37+
`p, "admin", /, POST`,
38+
`"p", "admin", "/", "PUT"`,
39+
`"p","admin",/, "DELETE"`,
40+
`p, " admin","/ ", "PATCH"`,
41+
];
42+
43+
const expectedPolicy = [
44+
['admin', '/', 'GET'],
45+
['admin', '/', 'POST'],
46+
['admin', '/', 'PUT'],
47+
['admin', '/', 'DELETE'],
48+
[' admin', '/ ', 'PATCH'],
49+
];
50+
51+
testdata.forEach((n) => {
52+
Helper.loadPolicyLine(n, m);
53+
});
54+
55+
const ast = m.model.get('p')?.get('p');
56+
expect(ast !== null && ast !== undefined).toBe(true);
57+
expect(ast?.policy?.length === expectedPolicy.length).toBe(true);
58+
expect(ast?.policy).toEqual(expectedPolicy);
59+
});

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -2022,6 +2022,11 @@ cssstyle@^1.0.0:
20222022
dependencies:
20232023
cssom "0.3.x"
20242024

2025+
csv-parse@^4.15.3:
2026+
version "4.15.3"
2027+
resolved "https://registry.npm.taobao.org/csv-parse/download/csv-parse-4.15.3.tgz#8a62759617a920c328cb31c351b05053b8f92b10"
2028+
integrity sha1-imJ1lhepIMMoyzHDUbBQU7j5KxA=
2029+
20252030
cyclist@^1.0.1:
20262031
version "1.0.1"
20272032
resolved "https://registry.npm.taobao.org/cyclist/download/cyclist-1.0.1.tgz#596e9698fd0c80e12038c2b82d6eb1b35b6224d9"

0 commit comments

Comments
 (0)