Skip to content

Commit 83aca6e

Browse files
committed
add owner to Space, fix "create" policy for SpaceUser
1 parent 8cd77cf commit 83aca6e

File tree

11 files changed

+158
-169
lines changed

11 files changed

+158
-169
lines changed

lib/hooks/__model_meta.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ const metadata = {
2222
name: "updatedAt",
2323
type: "DateTime",
2424
attributes: [{ "name": "@updatedAt", "args": [] }],
25+
}, owner: {
26+
name: "owner",
27+
type: "User",
28+
isDataModel: true,
29+
backLink: 'ownedSpaces',
30+
isRelationOwner: true,
31+
foreignKeyMapping: { "id": "ownerId" },
32+
}, ownerId: {
33+
name: "ownerId",
34+
type: "String",
35+
attributes: [{ "name": "@default", "args": [] }],
36+
defaultValueProvider: $default$Space$ownerId,
37+
isForeignKey: true,
38+
relationField: 'owner',
2539
}, name: {
2640
name: "name",
2741
type: "String",
@@ -85,7 +99,7 @@ const metadata = {
8599
name: "user",
86100
type: "User",
87101
isDataModel: true,
88-
backLink: 'spaces',
102+
backLink: 'memberships',
89103
isRelationOwner: true,
90104
foreignKeyMapping: { "id": "userId" },
91105
}, userId: {
@@ -140,8 +154,14 @@ const metadata = {
140154
name: "name",
141155
type: "String",
142156
isOptional: true,
143-
}, spaces: {
144-
name: "spaces",
157+
}, ownedSpaces: {
158+
name: "ownedSpaces",
159+
type: "Space",
160+
isDataModel: true,
161+
isArray: true,
162+
backLink: 'owner',
163+
}, memberships: {
164+
name: "memberships",
145165
type: "SpaceUser",
146166
isDataModel: true,
147167
isArray: true,
@@ -384,12 +404,16 @@ const metadata = {
384404
,
385405
deleteCascade: {
386406
space: ['SpaceUser', 'List'],
387-
user: ['SpaceUser', 'List', 'Todo', 'Account'],
407+
user: ['Space', 'SpaceUser', 'List', 'Todo', 'Account'],
388408
list: ['Todo'],
389409
}
390410
,
391411
authModel: 'User'
392412
};
413+
function $default$Space$ownerId(user: any): unknown {
414+
return user?.id;
415+
}
416+
393417
function $default$List$ownerId(user: any): unknown {
394418
return user?.id;
395419
}

lib/hooks/space.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,6 @@ export function useCountSpace<T extends Prisma.SpaceCountArgs>(args?: Prisma.Sub
159159
return request.useModelQuery('Space', 'count', args, options);
160160
}
161161

162-
export function useCheckSpace(args: { operation: PolicyCrudKind; where?: { id?: string; name?: string; slug?: string }; }, options?: QueryOptions<boolean>) {
162+
export function useCheckSpace(args: { operation: PolicyCrudKind; where?: { id?: string; ownerId?: string; name?: string; slug?: string }; }, options?: QueryOptions<boolean>) {
163163
return request.useModelQuery('Space', 'check', args, options);
164164
}

next.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ const nextConfig = {
33
reactStrictMode: true,
44
swcMinify: true,
55
images: {
6-
domains: ['picsum.photos', 'lh3.googleusercontent.com', 'avatars.githubusercontent.com'],
6+
remotePatterns: [
7+
{ hostname: 'picsum.photos' },
8+
{ hostname: 'lh3.googleusercontent.com' },
9+
{ hostname: 'avatars.githubusercontent.com' },
10+
],
711
},
812
};
913

package-lock.json

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"dependencies": {
2222
"@heroicons/react": "^2.0.12",
2323
"@next-auth/prisma-adapter": "^1.0.6",
24-
"@prisma/client": "^6.0.1",
24+
"@prisma/client": "^6.1.0",
2525
"@vercel/analytics": "^1.0.1",
2626
"@zenstackhq/runtime": "2.10.2",
2727
"@zenstackhq/server": "2.10.2",
@@ -51,7 +51,7 @@
5151
"eslint": "^7.19.0",
5252
"eslint-config-next": "12.3.1",
5353
"postcss": "^8.4.16",
54-
"prisma": "^6.0.1",
54+
"prisma": "^6.1.0",
5555
"tailwindcss": "^3.1.8",
5656
"typescript": "^5.1.6",
5757
"zenstack": "2.10.2"

pages/api/auth/[...nextauth].ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export const authOptions: NextAuthOptions = {
6868
data: {
6969
name: `${user.name || user.email}'s space`,
7070
slug: nanoid(8),
71+
owner: { connect: { id: user.id } },
7172
members: {
7273
create: [
7374
{
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
Warnings:
3+
4+
- Added the required column `ownerId` to the `Space` table without a default value. This is not possible if the table is not empty.
5+
6+
*/
7+
-- AlterTable
8+
ALTER TABLE "Space" ADD COLUMN "ownerId" TEXT NOT NULL;
9+
10+
-- AddForeignKey
11+
ALTER TABLE "Space" ADD CONSTRAINT "Space_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

prisma/migrations/migration_lock.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Please do not edit this file manually
2-
# It should be added in your version-control system (i.e. Git)
2+
# It should be added in your version-control system (e.g., Git)
33
provider = "postgresql"

prisma/schema.prisma

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ model Space {
2121
id String @id() @default(uuid())
2222
createdAt DateTime @default(now())
2323
updatedAt DateTime @updatedAt()
24+
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
25+
ownerId String
2426
name String
2527
slug String @unique()
2628
members SpaceUser[]
@@ -48,7 +50,8 @@ model User {
4850
emailVerified DateTime?
4951
password String?
5052
name String?
51-
spaces SpaceUser[]
53+
ownedSpaces Space[]
54+
memberships SpaceUser[]
5255
image String?
5356
lists List[]
5457
todos Todo[]

schema.zmodel

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ model Space {
3636
id String @id @default(uuid())
3737
createdAt DateTime @default(now())
3838
updatedAt DateTime @updatedAt
39+
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
40+
ownerId String @default(auth().id)
3941
name String @length(4, 50)
4042
slug String @unique @regex('^[0-9a-zA-Z]{4,16}$')
4143
members SpaceUser[]
@@ -71,8 +73,14 @@ model SpaceUser {
7173
// require login
7274
@@deny('all', auth() == null)
7375

74-
// space admin can create/update/delete
75-
@@allow('create,update,delete', space.members?[user == auth() && role == ADMIN])
76+
// space owner can add any one
77+
@@allow('create', space.owner == auth())
78+
79+
// space admin can add anyone but not himself
80+
@@allow('create', auth() != this.user && space.members?[user == auth() && role == ADMIN])
81+
82+
// space admin can update/delete
83+
@@allow('update,delete', space.members?[user == auth() && role == ADMIN])
7684

7785
// user can read entries for spaces which he's a member of
7886
@@allow('read', space.members?[user == auth()])
@@ -89,7 +97,8 @@ model User {
8997
emailVerified DateTime?
9098
password String? @password @omit
9199
name String?
92-
spaces SpaceUser[]
100+
ownedSpaces Space[]
101+
memberships SpaceUser[]
93102
image String? @url
94103
lists List[]
95104
todos Todo[]
@@ -101,7 +110,7 @@ model User {
101110
@@allow('create', true)
102111

103112
// can be read by users sharing any space
104-
@@allow('read', spaces?[space.members?[user == auth()]])
113+
@@allow('read', memberships?[space.members?[user == auth()]])
105114

106115
// full access by oneself
107116
@@allow('all', auth() == this)

tailwind.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = {
44
theme: {
55
extend: {},
66
},
7-
plugins: [require('daisyui'), require('@tailwindcss/line-clamp')],
7+
plugins: [require('daisyui')],
88
daisyui: {
99
themes: ['light'],
1010
},

0 commit comments

Comments
 (0)