Skip to content

Commit 2ab0b6f

Browse files
committed
add test with auth
1 parent 8b65a51 commit 2ab0b6f

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

packages/schema/src/plugins/enhancer/policy/utils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,15 @@ export function generateSelectForRules(rules: Expression[], forAuthContext = fal
199199
}
200200
} else if (isCollectionPredicate(expr)) {
201201
const path = visit(expr.left);
202+
// recurse into RHS
203+
const rhs = collectReferencePaths(expr.right);
202204
if (path) {
203-
// recurse into RHS
204-
const rhs = collectReferencePaths(expr.right);
205205
// combine path of LHS and RHS
206206
return rhs.map((r) => [...path, ...r]);
207207
} else {
208-
return [];
208+
// LHS is not rooted from the current model,
209+
// only keep RHS items that contains '$this'
210+
return rhs.filter((r) => r.includes('$this'));
209211
}
210212
} else if (isInvocationExpr(expr)) {
211213
// recurse into function arguments

tests/integration/tests/enhancements/with-policy/cross-model-field-comparison.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,4 +768,56 @@ describe('Cross-model field comparison', () => {
768768
await prisma.user.update({ where: { id: 1 }, data: { age: 21 } });
769769
await expect(db.user.update({ where: { id: 1 }, data: { age: 25 } })).toResolveTruthy();
770770
});
771+
772+
it('with auth', async () => {
773+
const { prisma, enhance } = await loadSchema(
774+
`
775+
model User {
776+
id Int @id @default(autoincrement())
777+
permissions Permission[]
778+
@@allow('all', true)
779+
}
780+
781+
model Permission {
782+
id Int @id @default(autoincrement())
783+
user User @relation(fields: [userId], references: [id])
784+
userId Int
785+
model String
786+
level Int
787+
@@allow('all', true)
788+
}
789+
790+
model Post {
791+
id Int @id @default(autoincrement())
792+
title String
793+
permission PostPermission?
794+
795+
@@allow('read', true)
796+
@@allow("create", auth().permissions?[model == 'Post' && level == this.permission.level])
797+
}
798+
799+
model PostPermission {
800+
id Int @id @default(autoincrement())
801+
post Post @relation(fields: [postId], references: [id])
802+
postId Int @unique
803+
level Int
804+
@@allow('all', true)
805+
}
806+
`,
807+
{ preserveTsFiles: true }
808+
);
809+
810+
await expect(enhance().post.create({ data: { title: 'P1' } })).toBeRejectedByPolicy();
811+
await expect(
812+
enhance({ id: 1, permissions: [{ model: 'Foo', level: 1 }] }).post.create({ data: { title: 'P1' } })
813+
).toBeRejectedByPolicy();
814+
await expect(
815+
enhance({ id: 1, permissions: [{ model: 'Post', level: 1 }] }).post.create({ data: { title: 'P1' } })
816+
).toBeRejectedByPolicy();
817+
await expect(
818+
enhance({ id: 1, permissions: [{ model: 'Post', level: 1 }] }).post.create({
819+
data: { title: 'P1', permission: { create: { level: 1 } } },
820+
})
821+
).toResolveTruthy();
822+
});
771823
});

0 commit comments

Comments
 (0)