Skip to content

fix: auth() cannot be resolved if the auth model is marked @@ignore #844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2023
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
2 changes: 1 addition & 1 deletion packages/schema/src/cli/cli-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function validationAfterMerge(model: Model) {
}

// at most one `@@auth` model
const dataModels = getDataModels(model);
const dataModels = getDataModels(model, true);
const authModels = dataModels.filter((d) => hasAttribute(d, '@@auth'));
if (authModels.length > 1) {
console.error(colors.red('Validation error: Multiple `@@auth` models are not allowed'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ export default class PolicyGenerator {
);

if (hasAuthRef) {
const authModel = getAuthModel(getDataModels(model.$container));
const authModel = getAuthModel(getDataModels(model.$container, true));
if (!authModel) {
throw new PluginError(name, 'Auth model not found');
}
Expand Down
9 changes: 7 additions & 2 deletions packages/sdk/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ import { PluginError, PluginOptions } from './types';
/**
* Gets data models that are not ignored
*/
export function getDataModels(model: Model) {
return model.declarations.filter((d): d is DataModel => isDataModel(d) && !hasAttribute(d, '@@ignore'));
export function getDataModels(model: Model, includeIgnored = false) {
const r = model.declarations.filter((d): d is DataModel => isDataModel(d));
if (includeIgnored) {
return r;
} else {
return r.filter((model) => !hasAttribute(model, '@@ignore'));
}
}

export function resolved<T extends AstNode>(ref: Reference<T>): T {
Expand Down
55 changes: 55 additions & 0 deletions tests/integration/tests/enhancements/with-policy/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,61 @@ describe('With Policy: auth() test', () => {
await expect(adminDb.post.create({ data: { title: 'abc' } })).toResolveTruthy();
});

it('User model ignored', async () => {
const { withPolicy } = await loadSchema(
`
model User {
id String @id @default(uuid())
role String

@@ignore
}

model Post {
id String @id @default(uuid())
title String

@@allow('read', true)
@@allow('create', auth().role == 'ADMIN')
}
`
);

const userDb = withPolicy({ id: 'user1', role: 'USER' });
await expect(userDb.post.create({ data: { title: 'abc' } })).toBeRejectedByPolicy();

const adminDb = withPolicy({ id: 'user1', role: 'ADMIN' });
await expect(adminDb.post.create({ data: { title: 'abc' } })).toResolveTruthy();
});

it('Auth model ignored', async () => {
const { withPolicy } = await loadSchema(
`
model Foo {
id String @id @default(uuid())
role String

@@auth()
@@ignore
}

model Post {
id String @id @default(uuid())
title String

@@allow('read', true)
@@allow('create', auth().role == 'ADMIN')
}
`
);

const userDb = withPolicy({ id: 'user1', role: 'USER' });
await expect(userDb.post.create({ data: { title: 'abc' } })).toBeRejectedByPolicy();

const adminDb = withPolicy({ id: 'user1', role: 'ADMIN' });
await expect(adminDb.post.create({ data: { title: 'abc' } })).toResolveTruthy();
});

it('collection predicate', async () => {
const { enhance, prisma } = await loadSchema(
`
Expand Down