Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/schema/src/plugins/enhancer/policy/expression-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -815,8 +815,15 @@ export class ExpressionWriter {
}

this.block(() => {
const targetGuardFunc = getQueryGuardFunctionName(targetModel, undefined, false, operation);
this.writer.write(`${fieldRef.target.$refText}: ${targetGuardFunc}(context, db)`);
if (operation === 'postUpdate') {
// 'postUpdate' policies are not delegated to relations, just use constant `false` here
// e.g.:
// @@allow('all', check(author)) should not delegate "postUpdate" to author
this.writer.write(`${fieldRef.target.$refText}: ${FALSE}`);
} else {
const targetGuardFunc = getQueryGuardFunctionName(targetModel, undefined, false, operation);
this.writer.write(`${fieldRef.target.$refText}: ${targetGuardFunc}(context, db)`);
}
});
}
}
40 changes: 40 additions & 0 deletions tests/regression/tests/issue-1642.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { loadSchema } from '@zenstackhq/testtools';
describe('issue 1642', () => {
it('regression', async () => {
const { prisma, enhance } = await loadSchema(
`
model User {
id Int @id
name String
posts Post[]

@@allow('read', true)
@@allow('all', auth().id == 1)
}

model Post {
id Int @id
title String
description String
author User @relation(fields: [authorId], references: [id])
authorId Int

// delegate all access policies to the author:
@@allow('all', check(author))

@@allow('update', future().title == 'hello')
}
`
);

await prisma.user.create({ data: { id: 1, name: 'User1' } });
await prisma.post.create({ data: { id: 1, title: 'hello', description: 'desc1', authorId: 1 } });

const db = enhance({ id: 2 });
await expect(
db.post.update({ where: { id: 1 }, data: { title: 'world', description: 'desc2' } })
).toBeRejectedByPolicy();

await expect(db.post.update({ where: { id: 1 }, data: { description: 'desc2' } })).toResolveTruthy();
});
});