@@ -253,7 +253,7 @@ export class PolicyUtil {
253
253
/**
254
254
* Injects model auth guard as where clause.
255
255
*/
256
- async injectAuthGuard ( db : Record < string , DbOperations > , args : any , model : string , operation : PolicyOperationKind ) {
256
+ injectAuthGuard ( db : Record < string , DbOperations > , args : any , model : string , operation : PolicyOperationKind ) {
257
257
let guard = this . getAuthGuard ( db , model , operation ) ;
258
258
if ( this . isFalse ( guard ) ) {
259
259
args . where = this . makeFalse ( ) ;
@@ -277,14 +277,14 @@ export class PolicyUtil {
277
277
// inject into relation fields:
278
278
// to-many: some/none/every
279
279
// to-one: direct-conditions/is/isNot
280
- await this . injectGuardForRelationFields ( db , model , args . where , operation ) ;
280
+ this . injectGuardForRelationFields ( db , model , args . where , operation ) ;
281
281
}
282
282
283
283
args . where = this . and ( args . where , guard ) ;
284
284
return true ;
285
285
}
286
286
287
- private async injectGuardForRelationFields (
287
+ private injectGuardForRelationFields (
288
288
db : Record < string , DbOperations > ,
289
289
model : string ,
290
290
payload : any ,
@@ -295,33 +295,33 @@ export class PolicyUtil {
295
295
continue ;
296
296
}
297
297
298
- const fieldInfo = await resolveField ( this . modelMeta , model , field ) ;
298
+ const fieldInfo = resolveField ( this . modelMeta , model , field ) ;
299
299
if ( ! fieldInfo || ! fieldInfo . isDataModel ) {
300
300
continue ;
301
301
}
302
302
303
303
if ( fieldInfo . isArray ) {
304
- await this . injectGuardForToManyField ( db , fieldInfo , subPayload , operation ) ;
304
+ this . injectGuardForToManyField ( db , fieldInfo , subPayload , operation ) ;
305
305
} else {
306
- await this . injectGuardForToOneField ( db , fieldInfo , subPayload , operation ) ;
306
+ this . injectGuardForToOneField ( db , fieldInfo , subPayload , operation ) ;
307
307
}
308
308
}
309
309
}
310
310
311
- private async injectGuardForToManyField (
311
+ private injectGuardForToManyField (
312
312
db : Record < string , DbOperations > ,
313
313
fieldInfo : FieldInfo ,
314
314
payload : { some ?: any ; every ?: any ; none ?: any } ,
315
315
operation : PolicyOperationKind
316
316
) {
317
317
const guard = this . getAuthGuard ( db , fieldInfo . type , operation ) ;
318
318
if ( payload . some ) {
319
- await this . injectGuardForRelationFields ( db , fieldInfo . type , payload . some , operation ) ;
319
+ this . injectGuardForRelationFields ( db , fieldInfo . type , payload . some , operation ) ;
320
320
// turn "some" into: { some: { AND: [guard, payload.some] } }
321
321
payload . some = this . and ( payload . some , guard ) ;
322
322
}
323
323
if ( payload . none ) {
324
- await this . injectGuardForRelationFields ( db , fieldInfo . type , payload . none , operation ) ;
324
+ this . injectGuardForRelationFields ( db , fieldInfo . type , payload . none , operation ) ;
325
325
// turn none into: { none: { AND: [guard, payload.none] } }
326
326
payload . none = this . and ( payload . none , guard ) ;
327
327
}
@@ -331,7 +331,7 @@ export class PolicyUtil {
331
331
// ignore empty every clause
332
332
Object . keys ( payload . every ) . length > 0
333
333
) {
334
- await this . injectGuardForRelationFields ( db , fieldInfo . type , payload . every , operation ) ;
334
+ this . injectGuardForRelationFields ( db , fieldInfo . type , payload . every , operation ) ;
335
335
336
336
// turn "every" into: { none: { AND: [guard, { NOT: payload.every }] } }
337
337
if ( ! payload . none ) {
@@ -342,7 +342,7 @@ export class PolicyUtil {
342
342
}
343
343
}
344
344
345
- private async injectGuardForToOneField (
345
+ private injectGuardForToOneField (
346
346
db : Record < string , DbOperations > ,
347
347
fieldInfo : FieldInfo ,
348
348
payload : { is ?: any ; isNot ?: any } & Record < string , any > ,
@@ -351,18 +351,18 @@ export class PolicyUtil {
351
351
const guard = this . getAuthGuard ( db , fieldInfo . type , operation ) ;
352
352
if ( payload . is || payload . isNot ) {
353
353
if ( payload . is ) {
354
- await this . injectGuardForRelationFields ( db , fieldInfo . type , payload . is , operation ) ;
354
+ this . injectGuardForRelationFields ( db , fieldInfo . type , payload . is , operation ) ;
355
355
// turn "is" into: { is: { AND: [ originalIs, guard ] }
356
356
payload . is = this . and ( payload . is , guard ) ;
357
357
}
358
358
if ( payload . isNot ) {
359
- await this . injectGuardForRelationFields ( db , fieldInfo . type , payload . isNot , operation ) ;
359
+ this . injectGuardForRelationFields ( db , fieldInfo . type , payload . isNot , operation ) ;
360
360
// turn "isNot" into: { isNot: { AND: [ originalIsNot, { NOT: guard } ] } }
361
361
payload . isNot = this . and ( payload . isNot , this . not ( guard ) ) ;
362
362
delete payload . isNot ;
363
363
}
364
364
} else {
365
- await this . injectGuardForRelationFields ( db , fieldInfo . type , payload , operation ) ;
365
+ this . injectGuardForRelationFields ( db , fieldInfo . type , payload , operation ) ;
366
366
// turn direct conditions into: { is: { AND: [ originalConditions, guard ] } }
367
367
const combined = this . and ( deepcopy ( payload ) , guard ) ;
368
368
Object . keys ( payload ) . forEach ( ( key ) => delete payload [ key ] ) ;
@@ -373,17 +373,17 @@ export class PolicyUtil {
373
373
/**
374
374
* Injects auth guard for read operations.
375
375
*/
376
- async injectForRead ( db : Record < string , DbOperations > , model : string , args : any ) {
376
+ injectForRead ( db : Record < string , DbOperations > , model : string , args : any ) {
377
377
const injected : any = { } ;
378
- if ( ! ( await this . injectAuthGuard ( db , injected , model , 'read' ) ) ) {
378
+ if ( ! this . injectAuthGuard ( db , injected , model , 'read' ) ) {
379
379
return false ;
380
380
}
381
381
382
382
if ( args . where ) {
383
383
// inject into relation fields:
384
384
// to-many: some/none/every
385
385
// to-one: direct-conditions/is/isNot
386
- await this . injectGuardForRelationFields ( db , model , args . where , 'read' ) ;
386
+ this . injectGuardForRelationFields ( db , model , args . where , 'read' ) ;
387
387
}
388
388
389
389
if ( injected . where && Object . keys ( injected . where ) . length > 0 && ! this . isTrue ( injected . where ) ) {
@@ -395,7 +395,7 @@ export class PolicyUtil {
395
395
}
396
396
397
397
// recursively inject read guard conditions into nested select, include, and _count
398
- const hoistedConditions = await this . injectNestedReadConditions ( db , model , args ) ;
398
+ const hoistedConditions = this . injectNestedReadConditions ( db , model , args ) ;
399
399
400
400
// the injection process may generate conditions that need to be hoisted to the toplevel,
401
401
// if so, merge it with the existing where
@@ -441,7 +441,7 @@ export class PolicyUtil {
441
441
/**
442
442
* Builds a reversed query for the given nested path.
443
443
*/
444
- async buildReversedQuery ( context : NestedWriteVisitorContext ) {
444
+ buildReversedQuery ( context : NestedWriteVisitorContext ) {
445
445
let result , currQuery : any ;
446
446
let currField : FieldInfo | undefined ;
447
447
@@ -489,11 +489,7 @@ export class PolicyUtil {
489
489
return result ;
490
490
}
491
491
492
- private async injectNestedReadConditions (
493
- db : Record < string , DbOperations > ,
494
- model : string ,
495
- args : any
496
- ) : Promise < any [ ] > {
492
+ private injectNestedReadConditions ( db : Record < string , DbOperations > , model : string , args : any ) : any [ ] {
497
493
const injectTarget = args . select ?? args . include ;
498
494
if ( ! injectTarget ) {
499
495
return [ ] ;
@@ -526,7 +522,7 @@ export class PolicyUtil {
526
522
continue ;
527
523
}
528
524
// inject into the "where" clause inside select
529
- await this . injectAuthGuard ( db , injectTarget . _count . select [ field ] , fieldInfo . type , 'read' ) ;
525
+ this . injectAuthGuard ( db , injectTarget . _count . select [ field ] , fieldInfo . type , 'read' ) ;
530
526
}
531
527
}
532
528
@@ -552,10 +548,10 @@ export class PolicyUtil {
552
548
injectTarget [ field ] = { } ;
553
549
}
554
550
// inject extra condition for to-many or nullable to-one relation
555
- await this . injectAuthGuard ( db , injectTarget [ field ] , fieldInfo . type , 'read' ) ;
551
+ this . injectAuthGuard ( db , injectTarget [ field ] , fieldInfo . type , 'read' ) ;
556
552
557
553
// recurse
558
- const subHoisted = await this . injectNestedReadConditions ( db , fieldInfo . type , injectTarget [ field ] ) ;
554
+ const subHoisted = this . injectNestedReadConditions ( db , fieldInfo . type , injectTarget [ field ] ) ;
559
555
if ( subHoisted . length > 0 ) {
560
556
// we can convert it to a where at this level
561
557
injectTarget [ field ] . where = this . and ( injectTarget [ field ] . where , ...subHoisted ) ;
@@ -564,7 +560,7 @@ export class PolicyUtil {
564
560
// hoist non-nullable to-one filter to the parent level
565
561
hoisted = this . getAuthGuard ( db , fieldInfo . type , 'read' ) ;
566
562
// recurse
567
- const subHoisted = await this . injectNestedReadConditions ( db , fieldInfo . type , injectTarget [ field ] ) ;
563
+ const subHoisted = this . injectNestedReadConditions ( db , fieldInfo . type , injectTarget [ field ] ) ;
568
564
if ( subHoisted . length > 0 ) {
569
565
hoisted = this . and ( hoisted , ...subHoisted ) ;
570
566
}
@@ -732,7 +728,7 @@ export class PolicyUtil {
732
728
CrudFailureReason . RESULT_NOT_READABLE
733
729
) ;
734
730
735
- const injectResult = await this . injectForRead ( db , model , readArgs ) ;
731
+ const injectResult = this . injectForRead ( db , model , readArgs ) ;
736
732
if ( ! injectResult ) {
737
733
return { error, result : undefined } ;
738
734
}
@@ -1011,6 +1007,14 @@ export class PolicyUtil {
1011
1007
}
1012
1008
}
1013
1009
1010
+ /**
1011
+ * Gets information for all fields of a model.
1012
+ */
1013
+ getModelFields ( model : string ) {
1014
+ model = lowerCaseFirst ( model ) ;
1015
+ return this . modelMeta . fields [ model ] ;
1016
+ }
1017
+
1014
1018
/**
1015
1019
* Gets information for a specific model field.
1016
1020
*/
0 commit comments