@@ -473,10 +473,11 @@ export class PolicyUtil extends QueryUtils {
473
473
474
474
let mergedGuard = guard ;
475
475
if ( args . where ) {
476
- // inject into relation fields:
476
+ // inject into fields:
477
477
// to-many: some/none/every
478
478
// to-one: direct-conditions/is/isNot
479
- mergedGuard = this . injectReadGuardForRelationFields ( db , model , args . where , guard ) ;
479
+ // regular fields
480
+ mergedGuard = this . buildReadGuardForFields ( db , model , args . where , guard ) ;
480
481
}
481
482
482
483
args . where = this . and ( args . where , mergedGuard ) ;
@@ -485,7 +486,7 @@ export class PolicyUtil extends QueryUtils {
485
486
486
487
// Injects guard for relation fields nested in `payload`. The `modelGuard` parameter represents the model-level guard for `model`.
487
488
// The function returns a modified copy of `modelGuard` with field-level policies combined.
488
- private injectReadGuardForRelationFields ( db : CrudContract , model : string , payload : any , modelGuard : any ) {
489
+ private buildReadGuardForFields ( db : CrudContract , model : string , payload : any , modelGuard : any ) {
489
490
if ( ! payload || typeof payload !== 'object' || Object . keys ( payload ) . length === 0 ) {
490
491
return modelGuard ;
491
492
}
@@ -530,12 +531,12 @@ export class PolicyUtil extends QueryUtils {
530
531
) {
531
532
const guard = this . getAuthGuard ( db , fieldInfo . type , 'read' ) ;
532
533
if ( payload . some ) {
533
- const mergedGuard = this . injectReadGuardForRelationFields ( db , fieldInfo . type , payload . some , guard ) ;
534
+ const mergedGuard = this . buildReadGuardForFields ( db , fieldInfo . type , payload . some , guard ) ;
534
535
// turn "some" into: { some: { AND: [guard, payload.some] } }
535
536
payload . some = this . and ( payload . some , mergedGuard ) ;
536
537
}
537
538
if ( payload . none ) {
538
- const mergedGuard = this . injectReadGuardForRelationFields ( db , fieldInfo . type , payload . none , guard ) ;
539
+ const mergedGuard = this . buildReadGuardForFields ( db , fieldInfo . type , payload . none , guard ) ;
539
540
// turn none into: { none: { AND: [guard, payload.none] } }
540
541
payload . none = this . and ( payload . none , mergedGuard ) ;
541
542
}
@@ -545,7 +546,7 @@ export class PolicyUtil extends QueryUtils {
545
546
// ignore empty every clause
546
547
Object . keys ( payload . every ) . length > 0
547
548
) {
548
- const mergedGuard = this . injectReadGuardForRelationFields ( db , fieldInfo . type , payload . every , guard ) ;
549
+ const mergedGuard = this . buildReadGuardForFields ( db , fieldInfo . type , payload . every , guard ) ;
549
550
550
551
// turn "every" into: { none: { AND: [guard, { NOT: payload.every }] } }
551
552
if ( ! payload . none ) {
@@ -569,18 +570,18 @@ export class PolicyUtil extends QueryUtils {
569
570
570
571
if ( payload . is !== undefined || payload . isNot !== undefined ) {
571
572
if ( payload . is ) {
572
- const mergedGuard = this . injectReadGuardForRelationFields ( db , fieldInfo . type , payload . is , guard ) ;
573
+ const mergedGuard = this . buildReadGuardForFields ( db , fieldInfo . type , payload . is , guard ) ;
573
574
// merge guard with existing "is": { is: { AND: [originalIs, guard] } }
574
575
payload . is = this . and ( payload . is , mergedGuard ) ;
575
576
}
576
577
577
578
if ( payload . isNot ) {
578
- const mergedGuard = this . injectReadGuardForRelationFields ( db , fieldInfo . type , payload . isNot , guard ) ;
579
+ const mergedGuard = this . buildReadGuardForFields ( db , fieldInfo . type , payload . isNot , guard ) ;
579
580
// merge guard with existing "isNot": { isNot: { AND: [originalIsNot, guard] } }
580
581
payload . isNot = this . and ( payload . isNot , mergedGuard ) ;
581
582
}
582
583
} else {
583
- const mergedGuard = this . injectReadGuardForRelationFields ( db , fieldInfo . type , payload , guard ) ;
584
+ const mergedGuard = this . buildReadGuardForFields ( db , fieldInfo . type , payload , guard ) ;
584
585
// turn direct conditions into: { is: { AND: [ originalConditions, guard ] } }
585
586
const combined = this . and ( clone ( payload ) , mergedGuard ) ;
586
587
Object . keys ( payload ) . forEach ( ( key ) => delete payload [ key ] ) ;
@@ -600,18 +601,22 @@ export class PolicyUtil extends QueryUtils {
600
601
}
601
602
602
603
if ( args . where ) {
603
- // inject into relation fields:
604
+ // inject into fields:
604
605
// to-many: some/none/every
605
606
// to-one: direct-conditions/is/isNot
606
- this . injectReadGuardForRelationFields ( db , model , args . where , { } ) ;
607
+ // regular fields
608
+ const mergedGuard = this . buildReadGuardForFields ( db , model , args . where , { } ) ;
609
+ this . mergeWhereClause ( args . where , mergedGuard ) ;
607
610
}
608
611
609
- if ( injected . where && Object . keys ( injected . where ) . length > 0 && ! this . isTrue ( injected . where ) ) {
610
- if ( ! args . where ) {
611
- args . where = injected . where ;
612
- } else {
612
+ if ( args . where ) {
613
+ if ( injected . where && Object . keys ( injected . where ) . length > 0 ) {
614
+ // merge injected guard with the user-provided where clause
613
615
this . mergeWhereClause ( args . where , injected . where ) ;
614
616
}
617
+ } else if ( injected . where ) {
618
+ // no user-provided where clause, use the injected one
619
+ args . where = injected . where ;
615
620
}
616
621
617
622
// recursively inject read guard conditions into nested select, include, and _count
0 commit comments