@@ -18,7 +18,7 @@ import { DefaultEffector, Effect, Effector } from './effect';
18
18
import { FunctionMap , Model , newModel , PolicyOp } from './model' ;
19
19
import { Adapter , FilteredAdapter , Watcher , BatchAdapter } from './persist' ;
20
20
import { DefaultRoleManager , RoleManager } from './rbac' ;
21
- import { escapeAssertion , generateGFunction , getEvalValue , hasEval , replaceEval } from './util' ;
21
+ import { escapeAssertion , generateGFunction , getEvalValue , hasEval , replaceEval , generatorRunSync , generatorRunAsync } from './util' ;
22
22
import { getLogger , logPrint } from './log' ;
23
23
24
24
type Matcher = ( ( context : any ) => Promise < any > ) | ( ( context : any ) => any ) ;
@@ -274,7 +274,7 @@ export class CoreEnforcer {
274
274
await this . model . buildRoleLinks ( this . rm ) ;
275
275
}
276
276
277
- private async privateEnforce ( asyncCompile = true , ...rvals : any [ ] ) : Promise < boolean > {
277
+ private * privateEnforce ( asyncCompile = true , ...rvals : any [ ] ) : Generator < boolean | Promise < boolean > > {
278
278
if ( ! this . enabled ) {
279
279
return true ;
280
280
}
@@ -347,7 +347,7 @@ export class CoreEnforcer {
347
347
}
348
348
349
349
const context = { ...parameters , ...functions } ;
350
- const result = asyncCompile ? await expression ( context ) : expression ( context ) ;
350
+ const result = asyncCompile ? yield expression ( context ) : expression ( context ) ;
351
351
352
352
let eftRes : Effect ;
353
353
switch ( typeof result ) {
@@ -395,7 +395,7 @@ export class CoreEnforcer {
395
395
396
396
expression = this . getExpression ( asyncCompile , expString ) ;
397
397
const context = { ...parameters , ...functions } ;
398
- const result = asyncCompile ? await expression ( context ) : expression ( context ) ;
398
+ const result = asyncCompile ? yield expression ( context ) : expression ( context ) ;
399
399
400
400
if ( result ) {
401
401
effectStream . pushEffect ( Effect . Allow ) ;
@@ -427,15 +427,22 @@ export class CoreEnforcer {
427
427
/**
428
428
* If the matchers does not contain an asynchronous method, call it faster.
429
429
*
430
- * enforceWithSyncCompile decides whether a "subject" can access a "object" with
430
+ * enforceSync decides whether a "subject" can access a "object" with
431
431
* the operation "action", input parameters are usually: (sub, obj, act).
432
432
*
433
433
* @param rvals the request needs to be mediated, usually an array
434
434
* of strings, can be class instances if ABAC is used.
435
435
* @return whether to allow the request.
436
436
*/
437
- public async enforceWithSyncCompile ( ...rvals : any [ ] ) : Promise < boolean > {
438
- return this . privateEnforce ( false , ...rvals ) ;
437
+ public enforceSync ( ...rvals : any [ ] ) : boolean {
438
+ return generatorRunSync ( this . privateEnforce ( false , ...rvals ) ) ;
439
+ }
440
+
441
+ /**
442
+ * Same as enforceSync. To be removed.
443
+ */
444
+ public enforceWithSyncCompile ( ...rvals : any [ ] ) : boolean {
445
+ return this . enforceSync ( ...rvals ) ;
439
446
}
440
447
441
448
/**
@@ -447,6 +454,6 @@ export class CoreEnforcer {
447
454
* @return whether to allow the request.
448
455
*/
449
456
public async enforce ( ...rvals : any [ ] ) : Promise < boolean > {
450
- return this . privateEnforce ( true , ...rvals ) ;
457
+ return generatorRunAsync ( this . privateEnforce ( true , ...rvals ) ) ;
451
458
}
452
459
}
0 commit comments