Skip to content

Commit eec498b

Browse files
authored
feat: Add authorization rules to user email object type (#863)
1 parent 5f7f3f1 commit eec498b

21 files changed

Lines changed: 301 additions & 123 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@baseplate-dev/plugin-auth': patch
3+
---
4+
5+
Add authorization rules to the User model's email field in both Better Auth and Placeholder Auth plugins, restricting email visibility to admins and the user themselves (via a new "self" instance role)

examples/blog-with-auth/apps/backend/baseplate/generated/src/modules/accounts/users/authorizers/user.authorizer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ export const userAuthorizer = createModelAuthorizer({
55
model: 'user',
66
idField: 'id',
77
getModelById: (id) => prisma.user.findUnique({ where: { id } }),
8-
roles: { owner: (ctx, model) => model.id === ctx.auth.userId },
8+
roles: { self: (ctx, model) => model.id === ctx.auth.userId },
99
});

examples/blog-with-auth/apps/backend/baseplate/generated/src/modules/accounts/users/authorizers/user.query-filter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { createModelQueryFilter } from '@src/utils/query-filters.js';
33
export const userQueryFilter = createModelQueryFilter({
44
model: 'user',
55
roles: {
6-
owner: (ctx) => (ctx.auth.userId != null ? { id: ctx.auth.userId } : false),
6+
self: (ctx) => (ctx.auth.userId != null ? { id: ctx.auth.userId } : false),
77
},
88
});

examples/blog-with-auth/apps/backend/baseplate/generated/src/modules/accounts/users/schema/user.object-type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const userObjectType = builder.prismaObject('User', {
66
fields: (t) => ({
77
id: t.exposeID('id'),
88
email: t.exposeString('email', {
9-
authorize: ['admin', userAuthorizer.roles.owner],
9+
authorize: ['admin', userAuthorizer.roles.self],
1010
nullable: true,
1111
}),
1212
name: t.exposeString('name', { nullable: true }),

examples/blog-with-auth/apps/backend/src/modules/accounts/users/authorizers/user.authorizer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ export const userAuthorizer = createModelAuthorizer({
55
model: 'user',
66
idField: 'id',
77
getModelById: (id) => prisma.user.findUnique({ where: { id } }),
8-
roles: { owner: (ctx, model) => model.id === ctx.auth.userId },
8+
roles: { self: (ctx, model) => model.id === ctx.auth.userId },
99
});

examples/blog-with-auth/apps/backend/src/modules/accounts/users/authorizers/user.query-filter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { createModelQueryFilter } from '@src/utils/query-filters.js';
33
export const userQueryFilter = createModelQueryFilter({
44
model: 'user',
55
roles: {
6-
owner: (ctx) => (ctx.auth.userId != null ? { id: ctx.auth.userId } : false),
6+
self: (ctx) => (ctx.auth.userId != null ? { id: ctx.auth.userId } : false),
77
},
88
});

examples/blog-with-auth/apps/backend/src/modules/accounts/users/schema/user.object-type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const userObjectType = builder.prismaObject('User', {
66
fields: (t) => ({
77
id: t.exposeID('id'),
88
email: t.exposeString('email', {
9-
authorize: ['admin', userAuthorizer.roles.owner],
9+
authorize: ['admin', userAuthorizer.roles.self],
1010
nullable: true,
1111
}),
1212
name: t.exposeString('name', { nullable: true }),

examples/blog-with-auth/baseplate/project-definition.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,8 @@
614614
"roles": [
615615
{
616616
"expression": "model.id === userId",
617-
"id": "model-authorizer-role:blog-user-owner",
618-
"name": "owner"
617+
"id": "model-authorizer-role:BjC0i357yrgp",
618+
"name": "self"
619619
}
620620
]
621621
},
@@ -632,7 +632,7 @@
632632
{ "ref": "id" },
633633
{
634634
"globalRoles": ["admin"],
635-
"instanceRoles": ["owner"],
635+
"instanceRoles": ["self"],
636636
"ref": "email"
637637
},
638638
{ "ref": "name" },

examples/blog-with-auth/pnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/todo-with-better-auth/apps/backend/baseplate/generated/src/modules/accounts/users/authorizers/user.authorizer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ export const userAuthorizer = createModelAuthorizer({
55
model: 'user',
66
idField: 'id',
77
getModelById: (id) => prisma.user.findUnique({ where: { id } }),
8-
roles: { owner: (ctx, model) => model.id === ctx.auth.userId },
8+
roles: { self: (ctx, model) => model.id === ctx.auth.userId },
99
});

0 commit comments

Comments
 (0)