Skip to content

Commit 73f2cec

Browse files
authored
fix: auth() cannot be resolved if the auth model is marked @@ignore (#844)
1 parent e99ad2c commit 73f2cec

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

packages/schema/src/cli/cli-util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function validationAfterMerge(model: Model) {
103103
}
104104

105105
// at most one `@@auth` model
106-
const dataModels = getDataModels(model);
106+
const dataModels = getDataModels(model, true);
107107
const authModels = dataModels.filter((d) => hasAttribute(d, '@@auth'));
108108
if (authModels.length > 1) {
109109
console.error(colors.red('Validation error: Multiple `@@auth` models are not allowed'));

packages/schema/src/plugins/access-policy/policy-guard-generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ export default class PolicyGenerator {
817817
);
818818

819819
if (hasAuthRef) {
820-
const authModel = getAuthModel(getDataModels(model.$container));
820+
const authModel = getAuthModel(getDataModels(model.$container, true));
821821
if (!authModel) {
822822
throw new PluginError(name, 'Auth model not found');
823823
}

packages/sdk/src/utils.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@ import { PluginError, PluginOptions } from './types';
3434
/**
3535
* Gets data models that are not ignored
3636
*/
37-
export function getDataModels(model: Model) {
38-
return model.declarations.filter((d): d is DataModel => isDataModel(d) && !hasAttribute(d, '@@ignore'));
37+
export function getDataModels(model: Model, includeIgnored = false) {
38+
const r = model.declarations.filter((d): d is DataModel => isDataModel(d));
39+
if (includeIgnored) {
40+
return r;
41+
} else {
42+
return r.filter((model) => !hasAttribute(model, '@@ignore'));
43+
}
3944
}
4045

4146
export function resolved<T extends AstNode>(ref: Reference<T>): T {

tests/integration/tests/enhancements/with-policy/auth.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,61 @@ describe('With Policy: auth() test', () => {
213213
await expect(adminDb.post.create({ data: { title: 'abc' } })).toResolveTruthy();
214214
});
215215

216+
it('User model ignored', async () => {
217+
const { withPolicy } = await loadSchema(
218+
`
219+
model User {
220+
id String @id @default(uuid())
221+
role String
222+
223+
@@ignore
224+
}
225+
226+
model Post {
227+
id String @id @default(uuid())
228+
title String
229+
230+
@@allow('read', true)
231+
@@allow('create', auth().role == 'ADMIN')
232+
}
233+
`
234+
);
235+
236+
const userDb = withPolicy({ id: 'user1', role: 'USER' });
237+
await expect(userDb.post.create({ data: { title: 'abc' } })).toBeRejectedByPolicy();
238+
239+
const adminDb = withPolicy({ id: 'user1', role: 'ADMIN' });
240+
await expect(adminDb.post.create({ data: { title: 'abc' } })).toResolveTruthy();
241+
});
242+
243+
it('Auth model ignored', async () => {
244+
const { withPolicy } = await loadSchema(
245+
`
246+
model Foo {
247+
id String @id @default(uuid())
248+
role String
249+
250+
@@auth()
251+
@@ignore
252+
}
253+
254+
model Post {
255+
id String @id @default(uuid())
256+
title String
257+
258+
@@allow('read', true)
259+
@@allow('create', auth().role == 'ADMIN')
260+
}
261+
`
262+
);
263+
264+
const userDb = withPolicy({ id: 'user1', role: 'USER' });
265+
await expect(userDb.post.create({ data: { title: 'abc' } })).toBeRejectedByPolicy();
266+
267+
const adminDb = withPolicy({ id: 'user1', role: 'ADMIN' });
268+
await expect(adminDb.post.create({ data: { title: 'abc' } })).toResolveTruthy();
269+
});
270+
216271
it('collection predicate', async () => {
217272
const { enhance, prisma } = await loadSchema(
218273
`

0 commit comments

Comments
 (0)