@@ -18,7 +18,17 @@ import { DefaultEffector, Effect, Effector } from './effect';
18
18
import { FunctionMap , Model , newModel , PolicyOp } from './model' ;
19
19
import { Adapter , FilteredAdapter , Watcher } from './persist' ;
20
20
import { DefaultRoleManager , RoleManager } from './rbac' ;
21
- import { escapeAssertion , generateGFunction , getEvalValue , hasEval , replaceEval , generatorRunSync , generatorRunAsync } from './util' ;
21
+ import {
22
+ escapeAssertion ,
23
+ generateGFunction ,
24
+ getEvalValue ,
25
+ hasEval ,
26
+ replaceEval ,
27
+ generatorRunSync ,
28
+ generatorRunAsync ,
29
+ generateSyncGFunction ,
30
+ isRoleManagerSync ,
31
+ } from './util' ;
22
32
import { getLogger , logPrint } from './log' ;
23
33
import { MatchingFunc } from './rbac' ;
24
34
@@ -38,7 +48,7 @@ export class CoreEnforcer {
38
48
39
49
protected adapter : Adapter ;
40
50
protected watcher : Watcher | null = null ;
41
- protected rmMap : Map < string , RoleManager > = new Map < string , RoleManager > ( [ [ 'g' , new DefaultRoleManager ( 10 ) ] ] ) ;
51
+ protected rmMap : Map < string , RoleManager > ;
42
52
43
53
protected enabled = true ;
44
54
protected autoSave = true ;
@@ -69,10 +79,18 @@ export class CoreEnforcer {
69
79
*/
70
80
public loadModel ( ) : void {
71
81
this . model = newModel ( ) ;
82
+ this . model . synced = false ;
72
83
this . model . loadModel ( this . modelPath ) ;
73
84
this . model . printModel ( ) ;
74
85
}
75
86
87
+ /**
88
+ * get a new RoleManager based on the type of current Enforcer
89
+ */
90
+ public newRoleManager ( ) : RoleManager {
91
+ return new DefaultRoleManager ( 10 ) ;
92
+ }
93
+
76
94
/**
77
95
* getModel gets the current model.
78
96
*
@@ -128,11 +146,21 @@ export class CoreEnforcer {
128
146
this . rmMap . set ( 'g' , rm ) ;
129
147
}
130
148
149
+ /**
150
+ * setRoleManager sets the current role manager.
151
+ *
152
+ * @param name
153
+ * @param rm the role manager.
154
+ */
155
+ public setNamedRoleManager ( name : string , rm : RoleManager ) : void {
156
+ this . rmMap . set ( name , rm ) ;
157
+ }
158
+
131
159
/**
132
160
* getRoleManager gets the current role manager.
133
161
*/
134
- public getRoleManager ( ) : RoleManager {
135
- return < RoleManager > this . rmMap . get ( 'g' ) ;
162
+ public getRoleManager ( ) : RoleManager | undefined {
163
+ return this . rmMap . get ( 'g' ) ;
136
164
}
137
165
138
166
/**
@@ -163,7 +191,7 @@ export class CoreEnforcer {
163
191
const rm = this . model . model . get ( 'g' ) ;
164
192
if ( rm ) {
165
193
for ( const ptype of rm . keys ( ) ) {
166
- this . rmMap . set ( ptype , new DefaultRoleManager ( 10 ) ) ;
194
+ this . rmMap . set ( ptype , this . newRoleManager ( ) ) ;
167
195
}
168
196
}
169
197
}
@@ -352,7 +380,7 @@ export class CoreEnforcer {
352
380
public async buildIncrementalRoleLinks ( op : PolicyOp , ptype : string , rules : string [ ] [ ] ) : Promise < void > {
353
381
let rm = this . rmMap . get ( ptype ) ;
354
382
if ( ! rm ) {
355
- rm = new DefaultRoleManager ( 10 ) ;
383
+ rm = this . newRoleManager ( ) ;
356
384
this . rmMap . set ( ptype , rm ) ;
357
385
}
358
386
await this . model . buildIncrementalRoleLinks ( rm , op , 'g' , ptype , rules ) ;
@@ -381,7 +409,7 @@ export class CoreEnforcer {
381
409
382
410
astMap ?. forEach ( ( value , key ) => {
383
411
const rm = value . rm ;
384
- functions [ key ] = generateGFunction ( rm ) ;
412
+ functions [ key ] = isRoleManagerSync ( rm ) ? generateSyncGFunction ( rm ) : generateGFunction ( rm ) ;
385
413
} ) ;
386
414
387
415
const expString = this . model . model . get ( 'm' ) ?. get ( 'm' ) ?. value ;
0 commit comments