Skip to content

Commit 75bc4a1

Browse files
authored
fix(policy): wrong table alias used when injecting for field policies (#2413)
1 parent 851e744 commit 75bc4a1

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

packages/plugins/policy/src/policy-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ export class PolicyHandler<Schema extends SchemaDef> extends OperationNodeTransf
660660
selections = [SelectionNode.create(SelectAllNode.create())];
661661
}
662662

663-
const modelPolicyFilter = this.buildPolicyFilter(model, alias, operation);
663+
const modelPolicyFilter = this.buildPolicyFilter(model, model, operation);
664664
if (!isTrueNode(modelPolicyFilter)) {
665665
hasPolicies = true;
666666
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { createPolicyTestClient } from '@zenstackhq/testtools';
2+
import { describe, expect, it } from 'vitest';
3+
4+
describe('Regression for issue #2385', () => {
5+
it('should not generate "missing FROM-clause entry" when including a relation with a static-value @@allow policy', async () => {
6+
const db = await createPolicyTestClient(
7+
`
8+
model User {
9+
id String @id @default(uuid())
10+
name String
11+
associations Association[]
12+
createdAt DateTime @default(now())
13+
updatedAt DateTime @updatedAt
14+
15+
@@allow("read", auth().id == id)
16+
}
17+
18+
model Association {
19+
id String @id @default(uuid())
20+
name String
21+
userId String @db.VarChar @map("user_id")
22+
user User @relation(fields: [userId], references: [id])
23+
createdAt DateTime @default(now()) @allow("read", auth().id == userId)
24+
updatedAt DateTime @updatedAt
25+
26+
@@allow("read", auth().id == userId)
27+
}
28+
`,
29+
);
30+
31+
const rawDb = db.$unuseAll();
32+
const user = await rawDb.user.create({
33+
data: {
34+
name: 'John Doe',
35+
},
36+
});
37+
38+
await rawDb.association.create({
39+
data: {
40+
name: 'Association for John',
41+
userId: user.id,
42+
},
43+
});
44+
45+
const userDb = db.$setAuth(user);
46+
47+
await expect(
48+
userDb.user.findMany({
49+
include: { associations: true },
50+
}),
51+
).toResolveTruthy();
52+
});
53+
});

0 commit comments

Comments
 (0)