-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathsyncedEnforcer.ts
520 lines (473 loc) · 19.8 KB
/
syncedEnforcer.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// Copyright 2020 The Casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { Enforcer, newEnforcerWithClass } from './enforcer';
import AwaitLock from 'await-lock';
import { Watcher } from './persist';
import { DefaultSyncedRoleManager, MatchingFunc, RoleManager } from './rbac';
import { newModel } from './model';
// SyncedEnforcer wraps Enforcer and provides synchronized access
export class SyncedEnforcer extends Enforcer {
lock = new AwaitLock();
/**
* setWatcher sets the current watcher.
*
* @param watcher the watcher.
*/
public setWatcher(watcher: Watcher): void {
this.watcher = watcher;
this.watcher.setUpdateCallback(() => this.loadPolicy());
}
/**
* loadPolicy reloads the policy from adapter.
*/
public async loadPolicy(): Promise<void> {
await this.lock.acquireAsync();
return super.loadPolicy().finally(() => this.lock.release());
}
/**
* clearPolicy clears all policy.
*/
public clearPolicy(): void {
this.lock
.acquireAsync()
.then(() => super.clearPolicy())
.finally(() => this.lock.release());
}
/**
* savePolicy saves the current policy (usually after changed with Casbin API) back to adapter.
*/
public async savePolicy(): Promise<boolean> {
await this.lock.acquireAsync();
return super.savePolicy().finally(() => this.lock.release());
}
/**
* buildRoleLinks manually rebuild the role inheritance relations.
*/
public async buildRoleLinks(): Promise<void> {
await this.lock.acquireAsync();
return super.buildRoleLinks().finally(() => this.lock.release());
}
/**
* If the matchers does not contain an asynchronous method, call it faster.
*
* enforceWithSyncCompile decides whether a "subject" can access a "object" with
* the operation "action", input parameters are usually: (sub, obj, act).
*
* @param rvals the request needs to be mediated, usually an array
* of strings, can be class instances if ABAC is used.
* @return whether to allow the request.
*/
public enforceWithSyncCompile(...rvals: any[]): boolean {
return super.enforceWithSyncCompile(...rvals);
}
/**
* enforce decides whether a "subject" can access a "object" with
* the operation "action", input parameters are usually: (sub, obj, act).
*
* @param rvals the request needs to be mediated, usually an array
* of strings, can be class instances if ABAC is used.
* @return whether to allow the request.
*/
public async enforce(...rvals: any[]): Promise<boolean> {
await this.lock.acquireAsync();
return super.enforce(...rvals).finally(() => this.lock.release());
}
/**
* getAllSubjects gets the list of subjects that show up in the current policy.
*
* @return all the subjects in "p" policy rules. It actually collects the
* 0-index elements of "p" policy rules. So make sure your subject
* is the 0-index element, like (sub, obj, act). Duplicates are removed.
*/
public async getAllSubjects(): Promise<string[]> {
return this.getAllNamedSubjects('p');
}
/**
* getAllNamedSubjects gets the list of subjects that show up in the currentnamed policy.
*
* @param ptype the policy type, can be "p", "p2", "p3", ..
* @return all the subjects in policy rules of the ptype type. It actually
* collects the 0-index elements of the policy rules. So make sure
* your subject is the 0-index element, like (sub, obj, act).
* Duplicates are removed.
*/
public async getAllNamedSubjects(ptype: string): Promise<string[]> {
await this.lock.acquireAsync();
return super.getAllNamedSubjects(ptype).finally(() => this.lock.release());
}
/**
* getAllObjects gets the list of objects that show up in the current policy.
*
* @return all the objects in "p" policy rules. It actually collects the
* 1-index elements of "p" policy rules. So make sure your object
* is the 1-index element, like (sub, obj, act).
* Duplicates are removed.
*/
public async getAllObjects(): Promise<string[]> {
return this.getAllNamedObjects('p');
}
/**
* getAllNamedObjects gets the list of objects that show up in the current named policy.
*
* @param ptype the policy type, can be "p", "p2", "p3", ..
* @return all the objects in policy rules of the ptype type. It actually
* collects the 1-index elements of the policy rules. So make sure
* your object is the 1-index element, like (sub, obj, act).
* Duplicates are removed.
*/
public async getAllNamedObjects(ptype: string): Promise<string[]> {
await this.lock.acquireAsync();
return super.getAllNamedObjects(ptype).finally(() => this.lock.release());
}
/**
* getAllActions gets the list of actions that show up in the current policy.
*
* @return all the actions in "p" policy rules. It actually collects
* the 2-index elements of "p" policy rules. So make sure your action
* is the 2-index element, like (sub, obj, act).
* Duplicates are removed.
*/
public async getAllActions(): Promise<string[]> {
return this.getAllNamedActions('p');
}
/**
* GetAllNamedActions gets the list of actions that show up in the current named policy.
*
* @param ptype the policy type, can be "p", "p2", "p3", ..
* @return all the actions in policy rules of the ptype type. It actually
* collects the 2-index elements of the policy rules. So make sure
* your action is the 2-index element, like (sub, obj, act).
* Duplicates are removed.
*/
public async getAllNamedActions(ptype: string): Promise<string[]> {
await this.lock.acquireAsync();
return super.getAllNamedActions(ptype).finally(() => this.lock.release());
}
/**
* getAllRoles gets the list of roles that show up in the current policy.
*
* @return all the roles in "g" policy rules. It actually collects
* the 1-index elements of "g" policy rules. So make sure your
* role is the 1-index element, like (sub, role).
* Duplicates are removed.
*/
public async getAllRoles(): Promise<string[]> {
return this.getAllNamedRoles('g');
}
/**
* getAllNamedRoles gets the list of roles that show up in the current named policy.
*
* @param ptype the policy type, can be "g", "g2", "g3", ..
* @return all the subjects in policy rules of the ptype type. It actually
* collects the 0-index elements of the policy rules. So make
* sure your subject is the 0-index element, like (sub, obj, act).
* Duplicates are removed.
*/
public async getAllNamedRoles(ptype: string): Promise<string[]> {
await this.lock.acquireAsync();
return super.getAllNamedRoles(ptype).finally(() => this.lock.release());
}
/**
* getPolicy gets all the authorization rules in the policy.
*
* @return all the "p" policy rules.
*/
public async getPolicy(): Promise<string[][]> {
return this.getNamedPolicy('p');
}
/**
* getFilteredPolicy gets all the authorization rules in the policy, field filters can be specified.
*
* @param fieldIndex the policy rule's start index to be matched.
* @param fieldValues the field values to be matched, value ""
* means not to match this field.
* @return the filtered "p" policy rules.
*/
public async getFilteredPolicy(fieldIndex: number, ...fieldValues: string[]): Promise<string[][]> {
return this.getFilteredNamedPolicy('p', fieldIndex, ...fieldValues);
}
/**
* getNamedPolicy gets all the authorization rules in the named policy.
*
* @param ptype the policy type, can be "p", "p2", "p3", ..
* @return the "p" policy rules of the specified ptype.
*/
public async getNamedPolicy(ptype: string): Promise<string[][]> {
await this.lock.acquireAsync();
return super.getNamedPolicy(ptype).finally(() => this.lock.release());
}
/**
* getFilteredNamedPolicy gets all the authorization rules in the named policy, field filters can be specified.
*
* @param ptype the policy type, can be "p", "p2", "p3", ..
* @param fieldIndex the policy rule's start index to be matched.
* @param fieldValues the field values to be matched, value ""
* means not to match this field.
* @return the filtered "p" policy rules of the specified ptype.
*/
public async getFilteredNamedPolicy(ptype: string, fieldIndex: number, ...fieldValues: string[]): Promise<string[][]> {
await this.lock.acquireAsync();
return super.getFilteredNamedPolicy(ptype, fieldIndex, ...fieldValues).finally(() => this.lock.release());
}
/**
* getGroupingPolicy gets all the role inheritance rules in the policy.
*
* @return all the "g" policy rules.
*/
public async getGroupingPolicy(): Promise<string[][]> {
return this.getNamedGroupingPolicy('g');
}
/**
* getFilteredGroupingPolicy gets all the role inheritance rules in the policy, field filters can be specified.
*
* @param fieldIndex the policy rule's start index to be matched.
* @param fieldValues the field values to be matched, value "" means not to match this field.
* @return the filtered "g" policy rules.
*/
public async getFilteredGroupingPolicy(fieldIndex: number, ...fieldValues: string[]): Promise<string[][]> {
return this.getFilteredNamedGroupingPolicy('g', fieldIndex, ...fieldValues);
}
/**
* getNamedGroupingPolicy gets all the role inheritance rules in the policy.
*
* @param ptype the policy type, can be "g", "g2", "g3", ..
* @return the "g" policy rules of the specified ptype.
*/
public async getNamedGroupingPolicy(ptype: string): Promise<string[][]> {
await this.lock.acquireAsync();
return super.getNamedGroupingPolicy(ptype).finally(() => this.lock.release());
}
/**
* getFilteredNamedGroupingPolicy gets all the role inheritance rules in the policy, field filters can be specified.
*
* @param ptype the policy type, can be "g", "g2", "g3", ..
* @param fieldIndex the policy rule's start index to be matched.
* @param fieldValues the field values to be matched, value ""
* means not to match this field.
* @return the filtered "g" policy rules of the specified ptype.
*/
public async getFilteredNamedGroupingPolicy(ptype: string, fieldIndex: number, ...fieldValues: string[]): Promise<string[][]> {
await this.lock.acquireAsync();
return super.getFilteredNamedGroupingPolicy(ptype, fieldIndex, ...fieldValues).finally(() => this.lock.release());
}
/**
* hasPolicy determines whether an authorization rule exists.
*
* @param params the "p" policy rule, ptype "p" is implicitly used.
* @return whether the rule exists.
*/
public async hasPolicy(...params: string[]): Promise<boolean> {
return this.hasNamedPolicy('p', ...params);
}
/**
* hasNamedPolicy determines whether a named authorization rule exists.
*
* @param ptype the policy type, can be "p", "p2", "p3", ..
* @param params the "p" policy rule.
* @return whether the rule exists.
*/
public async hasNamedPolicy(ptype: string, ...params: string[]): Promise<boolean> {
await this.lock.acquireAsync();
return super.hasNamedPolicy(ptype, ...params).finally(() => this.lock.release());
}
/**
* addPolicy adds an authorization rule to the current policy.
* If the rule already exists, the function returns false and the rule will not be added.
* Otherwise the function returns true by adding the new rule.
*
* @param params the "p" policy rule, ptype "p" is implicitly used.
* @return succeeds or not.
*/
public async addPolicy(...params: string[]): Promise<boolean> {
return this.addNamedPolicy('p', ...params);
}
/**
* addNamedPolicy adds an authorization rule to the current named policy.
* If the rule already exists, the function returns false and the rule will not be added.
* Otherwise the function returns true by adding the new rule.
*
* @param ptype the policy type, can be "p", "p2", "p3", ..
* @param params the "p" policy rule.
* @return succeeds or not.
*/
public async addNamedPolicy(ptype: string, ...params: string[]): Promise<boolean> {
await this.lock.acquireAsync();
return super.addNamedPolicy(ptype, ...params).finally(() => this.lock.release());
}
/**
* removePolicy removes an authorization rule from the current policy.
*
* @param params the "p" policy rule, ptype "p" is implicitly used.
* @return succeeds or not.
*/
public async removePolicy(...params: string[]): Promise<boolean> {
return this.removeNamedPolicy('p', ...params);
}
/**
* removeFilteredPolicy removes an authorization rule from the current policy, field filters can be specified.
*
* @param fieldIndex the policy rule's start index to be matched.
* @param fieldValues the field values to be matched, value ""
* means not to match this field.
* @return succeeds or not.
*/
public async removeFilteredPolicy(fieldIndex: number, ...fieldValues: string[]): Promise<boolean> {
return this.removeFilteredNamedPolicy('p', fieldIndex, ...fieldValues);
}
/**
* removeNamedPolicy removes an authorization rule from the current named policy.
*
* @param ptype the policy type, can be "p", "p2", "p3", ..
* @param params the "p" policy rule.
* @return succeeds or not.
*/
public async removeNamedPolicy(ptype: string, ...params: string[]): Promise<boolean> {
await this.lock.acquireAsync();
return this.removePolicyInternal('p', ptype, params).finally(() => this.lock.release());
}
/**
* removeFilteredNamedPolicy removes an authorization rule from the current named policy, field filters can be specified.
*
* @param ptype the policy type, can be "p", "p2", "p3", ..
* @param fieldIndex the policy rule's start index to be matched.
* @param fieldValues the field values to be matched, value ""
* means not to match this field.
* @return succeeds or not.
*/
public async removeFilteredNamedPolicy(ptype: string, fieldIndex: number, ...fieldValues: string[]): Promise<boolean> {
await this.lock.acquireAsync();
return super.removeFilteredNamedPolicy(ptype, fieldIndex, ...fieldValues).finally(() => this.lock.release());
}
/**
* hasGroupingPolicy determines whether a role inheritance rule exists.
*
* @param params the "g" policy rule, ptype "g" is implicitly used.
* @return whether the rule exists.
*/
public async hasGroupingPolicy(...params: string[]): Promise<boolean> {
return this.hasNamedGroupingPolicy('g', ...params);
}
/**
* hasNamedGroupingPolicy determines whether a named role inheritance rule exists.
*
* @param ptype the policy type, can be "g", "g2", "g3", ..
* @param params the "g" policy rule.
* @return whether the rule exists.
*/
public async hasNamedGroupingPolicy(ptype: string, ...params: string[]): Promise<boolean> {
await this.lock.acquireAsync();
return super.hasNamedGroupingPolicy(ptype, ...params).finally(() => this.lock.release());
}
/**
* addGroupingPolicy adds a role inheritance rule to the current policy.
* If the rule already exists, the function returns false and the rule will not be added.
* Otherwise the function returns true by adding the new rule.
*
* @param params the "g" policy rule, ptype "g" is implicitly used.
* @return succeeds or not.
*/
public async addGroupingPolicy(...params: string[]): Promise<boolean> {
return this.addNamedGroupingPolicy('g', ...params);
}
/**
* addNamedGroupingPolicy adds a named role inheritance rule to the current policy.
* If the rule already exists, the function returns false and the rule will not be added.
* Otherwise the function returns true by adding the new rule.
*
* @param ptype the policy type, can be "g", "g2", "g3", ..
* @param params the "g" policy rule.
* @return succeeds or not.
*/
public async addNamedGroupingPolicy(ptype: string, ...params: string[]): Promise<boolean> {
await this.lock.acquireAsync();
return super.addNamedGroupingPolicy(ptype, ...params).finally(() => this.lock.release());
}
/**
* removeGroupingPolicy removes a role inheritance rule from the current policy.
*
* @param params the "g" policy rule, ptype "g" is implicitly used.
* @return succeeds or not.
*/
public async removeGroupingPolicy(...params: string[]): Promise<boolean> {
return this.removeNamedGroupingPolicy('g', ...params);
}
/**
* removeFilteredGroupingPolicy removes a role inheritance rule from the current policy, field filters can be specified.
*
* @param fieldIndex the policy rule's start index to be matched.
* @param fieldValues the field values to be matched, value ""
* means not to match this field.
* @return succeeds or not.
*/
public async removeFilteredGroupingPolicy(fieldIndex: number, ...fieldValues: string[]): Promise<boolean> {
return this.removeFilteredNamedGroupingPolicy('g', fieldIndex, ...fieldValues);
}
/**
* removeNamedGroupingPolicy removes a role inheritance rule from the current named policy.
*
* @param ptype the policy type, can be "g", "g2", "g3", ..
* @param params the "g" policy rule.
* @return succeeds or not.
*/
public async removeNamedGroupingPolicy(ptype: string, ...params: string[]): Promise<boolean> {
await this.lock.acquireAsync();
return super.removeNamedGroupingPolicy(ptype, ...params).finally(() => this.lock.release());
}
/**
* removeFilteredNamedGroupingPolicy removes a role inheritance rule from the current named policy, field filters can be specified.
*
* @param ptype the policy type, can be "g", "g2", "g3", ..
* @param fieldIndex the policy rule's start index to be matched.
* @param fieldValues the field values to be matched, value ""
* means not to match this field.
* @return succeeds or not.
*/
public async removeFilteredNamedGroupingPolicy(ptype: string, fieldIndex: number, ...fieldValues: string[]): Promise<boolean> {
await this.lock.acquireAsync();
return super.removeFilteredNamedGroupingPolicy(ptype, fieldIndex, ...fieldValues).finally(() => this.lock.release());
}
/**
* add matching function to RoleManager by ptype
* @param ptype g
* @param fn the function will be added
*/
public async addNamedMatchingFunc(ptype: string, fn: MatchingFunc): Promise<void> {
await this.lock.acquireAsync();
return super.addNamedMatchingFunc(ptype, fn).finally(() => this.lock.release());
}
/**
* add domain matching function to RoleManager by ptype
* @param ptype g
* @param fn the function will be added
*/
public async addNamedDomainMatchingFunc(ptype: string, fn: MatchingFunc): Promise<void> {
await this.lock.acquireAsync();
return super.addNamedDomainMatchingFunc(ptype, fn).finally(() => {
this.lock.release();
});
}
public newRoleManager(): RoleManager {
return new DefaultSyncedRoleManager(10);
}
public loadModel(): void {
this.model = newModel();
this.model.synced = true;
this.model.loadModel(this.modelPath);
this.model.printModel();
}
}
// newSyncedEnforcer creates a synchronized enforcer via adapter.
export async function newSyncedEnforcer(...params: any[]): Promise<SyncedEnforcer> {
return newEnforcerWithClass(SyncedEnforcer, ...params);
}