@@ -357,7 +357,7 @@ describe('Permission checker', () => {
357357 await expect ( db . model . check ( { operation : 'update' , where : { x : 1 , y : 1 } } ) ) . toResolveFalsy ( ) ;
358358 } ) ;
359359
360- it ( 'field condition unsolvable ' , async ( ) => {
360+ it ( 'field condition unsatisfiable ' , async ( ) => {
361361 const { enhance } = await load (
362362 `
363363 model Model {
@@ -649,4 +649,115 @@ describe('Permission checker', () => {
649649 await expect ( db . model . check ( { operation : 'read' , where : { value : 1 } } ) ) . toResolveTruthy ( ) ;
650650 await expect ( db . model . check ( { operation : 'read' , where : { value : 2 } } ) ) . toResolveTruthy ( ) ;
651651 } ) ;
652+
653+ it ( 'supports policy delegation simple' , async ( ) => {
654+ const { enhance } = await load (
655+ `
656+ model User {
657+ id Int @id @default(autoincrement())
658+ foo Foo[]
659+ }
660+
661+ model Foo {
662+ id Int @id @default(autoincrement())
663+ owner User @relation(fields: [ownerId], references: [id])
664+ ownerId Int
665+ model Model?
666+ @@allow('read', auth().id == ownerId)
667+ @@allow('create', auth().id != ownerId)
668+ @@allow('update', auth() == owner)
669+ }
670+
671+ model Model {
672+ id Int @id @default(autoincrement())
673+ foo Foo @relation(fields: [fooId], references: [id])
674+ fooId Int @unique
675+ @@allow('all', check(foo))
676+ }
677+ ` ,
678+ { preserveTsFiles : true }
679+ ) ;
680+
681+ await expect ( enhance ( ) . model . check ( { operation : 'read' } ) ) . toResolveFalsy ( ) ;
682+ await expect ( enhance ( { id : 1 } ) . model . check ( { operation : 'read' } ) ) . toResolveTruthy ( ) ;
683+
684+ await expect ( enhance ( ) . model . check ( { operation : 'create' } ) ) . toResolveFalsy ( ) ;
685+ await expect ( enhance ( { id : 1 } ) . model . check ( { operation : 'create' } ) ) . toResolveTruthy ( ) ;
686+
687+ await expect ( enhance ( ) . model . check ( { operation : 'update' } ) ) . toResolveFalsy ( ) ;
688+ await expect ( enhance ( { id : 1 } ) . model . check ( { operation : 'update' } ) ) . toResolveTruthy ( ) ;
689+
690+ await expect ( enhance ( ) . model . check ( { operation : 'delete' } ) ) . toResolveFalsy ( ) ;
691+ await expect ( enhance ( { id : 1 } ) . model . check ( { operation : 'delete' } ) ) . toResolveFalsy ( ) ;
692+ } ) ;
693+
694+ it ( 'supports policy delegation explicit' , async ( ) => {
695+ const { enhance } = await load (
696+ `
697+ model Foo {
698+ id Int @id @default(autoincrement())
699+ model Model?
700+ @@allow('all', true)
701+ @@deny('update', true)
702+ }
703+
704+ model Model {
705+ id Int @id @default(autoincrement())
706+ foo Foo @relation(fields: [fooId], references: [id])
707+ fooId Int @unique
708+ @@allow('read', check(foo, 'update'))
709+ }
710+ ` ,
711+ { preserveTsFiles : true }
712+ ) ;
713+
714+ await expect ( enhance ( ) . model . check ( { operation : 'read' } ) ) . toResolveFalsy ( ) ;
715+ } ) ;
716+
717+ it ( 'supports policy delegation combined' , async ( ) => {
718+ const { enhance } = await load (
719+ `
720+ model User {
721+ id Int @id @default(autoincrement())
722+ foo Foo[]
723+ }
724+
725+ model Foo {
726+ id Int @id @default(autoincrement())
727+ owner User @relation(fields: [ownerId], references: [id])
728+ ownerId Int
729+ model Model?
730+ @@allow('read', auth().id == ownerId)
731+ @@allow('create', auth().id != ownerId)
732+ @@allow('update', auth() == owner)
733+ }
734+
735+ model Model {
736+ id Int @id @default(autoincrement())
737+ foo Foo @relation(fields: [fooId], references: [id])
738+ fooId Int @unique
739+ value Int
740+ @@allow('all', check(foo) && value > 0)
741+ @@deny('update', check(foo) && value == 1)
742+ }
743+ ` ,
744+ { preserveTsFiles : true }
745+ ) ;
746+
747+ await expect ( enhance ( ) . model . check ( { operation : 'read' } ) ) . toResolveFalsy ( ) ;
748+ await expect ( enhance ( { id : 1 } ) . model . check ( { operation : 'read' } ) ) . toResolveTruthy ( ) ;
749+ await expect ( enhance ( { id : 1 } ) . model . check ( { operation : 'read' , where : { value : 1 } } ) ) . toResolveTruthy ( ) ;
750+ await expect ( enhance ( { id : 1 } ) . model . check ( { operation : 'read' , where : { value : 0 } } ) ) . toResolveFalsy ( ) ;
751+
752+ await expect ( enhance ( ) . model . check ( { operation : 'create' } ) ) . toResolveFalsy ( ) ;
753+ await expect ( enhance ( { id : 1 } ) . model . check ( { operation : 'create' } ) ) . toResolveTruthy ( ) ;
754+ await expect ( enhance ( { id : 1 } ) . model . check ( { operation : 'create' , where : { value : 1 } } ) ) . toResolveTruthy ( ) ;
755+ await expect ( enhance ( { id : 1 } ) . model . check ( { operation : 'create' , where : { value : 0 } } ) ) . toResolveFalsy ( ) ;
756+
757+ await expect ( enhance ( ) . model . check ( { operation : 'update' } ) ) . toResolveFalsy ( ) ;
758+ await expect ( enhance ( { id : 1 } ) . model . check ( { operation : 'update' } ) ) . toResolveTruthy ( ) ;
759+ await expect ( enhance ( { id : 1 } ) . model . check ( { operation : 'update' , where : { value : 2 } } ) ) . toResolveTruthy ( ) ;
760+ await expect ( enhance ( { id : 1 } ) . model . check ( { operation : 'update' , where : { value : 0 } } ) ) . toResolveFalsy ( ) ;
761+ await expect ( enhance ( { id : 1 } ) . model . check ( { operation : 'update' , where : { value : 1 } } ) ) . toResolveFalsy ( ) ;
762+ } ) ;
652763} ) ;
0 commit comments