-
Notifications
You must be signed in to change notification settings - Fork 567
/
adapter.ts
208 lines (188 loc) 路 5.48 KB
/
adapter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { FilteredAdapter, Helper, Model } from 'casbin';
interface Line {
ptype: string;
v0: string;
v1: string;
v2: string;
v3: string;
v4: string;
v5: string;
}
export abstract class BaseAdapter<AdapterLine extends Line>
implements FilteredAdapter
{
private filtered = false;
private policies: AdapterLine[];
public isFiltered(): boolean {
return this.filtered;
}
public async loadPolicy(model: Model): Promise<void> {
const policies = await this.loadPolicyByAdapter();
this.policies = policies;
for (const line of policies) {
this.loadPolicyLine(line, model);
}
}
public async savePolicy(model: Model): Promise<boolean> {
const policyRuleAST = model.model.get('p')!;
const groupingPolicyAST = model.model.get('g')!;
const policies: AdapterLine[] = [];
for (const astMap of [policyRuleAST, groupingPolicyAST]) {
for (const [ptype, ast] of astMap) {
for (const rule of ast.policy) {
const line = this.savePolicyLine(ptype, rule);
policies.push(line);
}
}
}
await this.savePolicyByAdapter(policies);
return true;
}
public async addPolicy(
sec: string,
ptype: string,
rule: string[]
): Promise<void> {
const line = this.savePolicyLine(ptype, rule);
this.policies.push(line);
await this.savePolicyByAdapter(this.policies);
}
public async removePolicy(
sec: string,
ptype: string,
rule: string[]
): Promise<void> {
const filteredPolicies = this.policies.filter(policy => {
let flag = true;
flag &&= ptype === policy.ptype;
if (rule.length > 0) {
flag &&= rule[0] === policy.v0;
}
if (rule.length > 1) {
flag &&= rule[1] === policy.v1;
}
if (rule.length > 2) {
flag &&= rule[2] === policy.v2;
}
if (rule.length > 3) {
flag &&= rule[3] === policy.v3;
}
if (rule.length > 4) {
flag &&= rule[4] === policy.v4;
}
if (rule.length > 5) {
flag &&= rule[5] === policy.v5;
}
return !flag;
});
this.policies = filteredPolicies;
const line = this.savePolicyLine(ptype, rule);
await this.removePolicyByAdapter(line, filteredPolicies);
}
public async loadFilteredPolicy(model: Model, filter: any): Promise<void> {
const filteredPolicies = await this.loadPolicyWithFilterByAdapter(filter);
this.policies = filteredPolicies;
filteredPolicies.forEach((policy: any) => {
this.loadPolicyLine(policy, model);
});
this.filtered = true;
}
public async removeFilteredPolicy(
sec: string,
ptype: string,
fieldIndex: number,
...fieldValues: string[]
): Promise<void> {
const rule = new Array<string>(fieldIndex).fill('');
rule.push(...fieldValues);
const filteredPolicies = this.policies.filter(policy => {
let flag = true;
flag &&= ptype === policy.ptype;
if (rule.length > 0 && rule[0]) {
flag &&= rule[0] === policy.v0;
}
if (rule.length > 1 && rule[1]) {
flag &&= rule[1] === policy.v1;
}
if (rule.length > 2 && rule[2]) {
flag &&= rule[2] === policy.v2;
}
if (rule.length > 3 && rule[3]) {
flag &&= rule[3] === policy.v3;
}
if (rule.length > 4 && rule[4]) {
flag &&= rule[4] === policy.v4;
}
if (rule.length > 5 && rule[5]) {
flag &&= rule[5] === policy.v5;
}
return !flag;
});
this.policies = filteredPolicies;
const line = new (this.getAdapterLine())();
line.ptype = ptype;
if (fieldIndex <= 0 && 0 < fieldIndex + fieldValues.length) {
line.v0 = fieldValues[0 - fieldIndex];
}
if (fieldIndex <= 1 && 1 < fieldIndex + fieldValues.length) {
line.v1 = fieldValues[1 - fieldIndex];
}
if (fieldIndex <= 2 && 2 < fieldIndex + fieldValues.length) {
line.v2 = fieldValues[2 - fieldIndex];
}
if (fieldIndex <= 3 && 3 < fieldIndex + fieldValues.length) {
line.v3 = fieldValues[3 - fieldIndex];
}
if (fieldIndex <= 4 && 4 < fieldIndex + fieldValues.length) {
line.v4 = fieldValues[4 - fieldIndex];
}
if (fieldIndex <= 5 && 5 < fieldIndex + fieldValues.length) {
line.v5 = fieldValues[5 - fieldIndex];
}
return this.removePolicyByAdapter(line, filteredPolicies);
}
private loadPolicyLine(line: any, model: any) {
const lineText =
line.ptype +
', ' +
[line.v0, line.v1, line.v2, line.v3, line.v4, line.v5]
.filter(n => n)
.join(', ');
Helper.loadPolicyLine(lineText, model);
}
private savePolicyLine(ptype: string, rule: string[]): AdapterLine {
const line = new (this.getAdapterLine())();
line.ptype = ptype;
if (rule.length > 0) {
line.v0 = rule[0];
}
if (rule.length > 1) {
line.v1 = rule[1];
}
if (rule.length > 2) {
line.v2 = rule[2];
}
if (rule.length > 3) {
line.v3 = rule[3];
}
if (rule.length > 4) {
line.v4 = rule[4];
}
if (rule.length > 5) {
line.v5 = rule[5];
}
return line;
}
protected abstract getAdapterLine(): new () => AdapterLine;
protected abstract loadPolicyByAdapter(): Promise<AdapterLine[]>;
protected abstract loadPolicyWithFilterByAdapter(
filter
): Promise<AdapterLine[]>;
protected abstract savePolicyByAdapter(
policies: AdapterLine[]
): Promise<void>;
protected abstract removePolicyByAdapter(
removePolicy: AdapterLine,
newestPolicies?: AdapterLine[]
): Promise<void>;
}