Skip to content

Commit 00ab243

Browse files
committed
add owner to Space, fix "create" policy for SpaceUser
1 parent 02c9d5f commit 00ab243

File tree

10 files changed

+137
-197
lines changed

10 files changed

+137
-197
lines changed

package-lock.json

+64-173
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"postcss": "^8.4.23",
3535
"prettier": "^2.8.0",
3636
"prettier-plugin-svelte": "^2.8.1",
37-
"prisma": "^5.19.1",
37+
"prisma": "^6.1.0",
3838
"svelte": "^4.1.2",
3939
"svelte-check": "^3.4.6",
4040
"tailwindcss": "^3.3.2",
@@ -46,7 +46,7 @@
4646
},
4747
"type": "module",
4848
"dependencies": {
49-
"@prisma/client": "^5.19.1",
49+
"@prisma/client": "^6.1.0",
5050
"@steeze-ui/heroicons": "^2.2.3",
5151
"@steeze-ui/svelte-icon": "^1.5.0",
5252
"@tanstack/svelte-query": "^4.32.6",
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

+1-1
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

+10-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ model Space {
2222
id String @id() @default(uuid())
2323
createdAt DateTime @default(now())
2424
updatedAt DateTime @updatedAt()
25+
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
26+
ownerId String
2527
name String
2628
slug String @unique()
2729
members SpaceUser[]
@@ -42,13 +44,14 @@ model SpaceUser {
4244
}
4345

4446
model User {
45-
id String @id() @default(cuid())
46-
email String @unique()
47-
password String
48-
name String?
49-
spaces SpaceUser[]
50-
todos Todo[]
51-
lists List[]
47+
id String @id() @default(cuid())
48+
email String @unique()
49+
password String
50+
name String?
51+
ownedSpaces Space[]
52+
memberships SpaceUser[]
53+
todos Todo[]
54+
lists List[]
5255
}
5356

5457
model List {

schema.zmodel

+18-9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ model Space {
2929
id String @id @default(uuid())
3030
createdAt DateTime @default(now())
3131
updatedAt DateTime @updatedAt
32+
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
33+
ownerId String @default(auth().id)
3234
name String @length(4, 50)
3335
slug String @unique @regex('^[0-9a-zA-Z\-_]{4,16}$')
3436
members SpaceUser[]
@@ -64,8 +66,14 @@ model SpaceUser {
6466
// require login
6567
@@deny('all', auth() == null)
6668

67-
// space admin can create/update/delete
68-
@@allow('create,update,delete', space.members?[user == auth() && role == ADMIN])
69+
// space owner can add any one
70+
@@allow('create', space.owner == auth())
71+
72+
// space admin can add anyone but not himself
73+
@@allow('create', auth() != user && space.members?[user == auth() && role == ADMIN])
74+
75+
// space admin can update/delete
76+
@@allow('update,delete', space.members?[user == auth() && role == ADMIN])
6977

7078
// user can read entries for spaces which he's a member of
7179
@@allow('read', space.members?[user == auth()])
@@ -75,13 +83,14 @@ model SpaceUser {
7583
* User model
7684
*/
7785
model User {
78-
id String @id @default(cuid())
79-
email String @unique @email
80-
password String @password @omit @length(6, 32)
81-
name String?
82-
spaces SpaceUser[]
83-
todos Todo[]
84-
lists List[]
86+
id String @id @default(cuid())
87+
email String @unique @email
88+
password String @password @omit @length(6, 32)
89+
name String?
90+
ownedSpaces Space[]
91+
memberships SpaceUser[]
92+
todos Todo[]
93+
lists List[]
8594
@@allow('create,read', true)
8695
@@allow('all', auth() == this)
8796
}

src/lib/hooks/__model_meta.ts

+28-4
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: {
@@ -127,8 +141,14 @@ const metadata = {
127141
name: "name",
128142
type: "String",
129143
isOptional: true,
130-
}, spaces: {
131-
name: "spaces",
144+
}, ownedSpaces: {
145+
name: "ownedSpaces",
146+
type: "Space",
147+
isDataModel: true,
148+
isArray: true,
149+
backLink: 'owner',
150+
}, memberships: {
151+
name: "memberships",
132152
type: "SpaceUser",
133153
isDataModel: true,
134154
isArray: true,
@@ -287,12 +307,16 @@ const metadata = {
287307
,
288308
deleteCascade: {
289309
space: ['SpaceUser', 'List'],
290-
user: ['SpaceUser', 'List', 'Todo'],
310+
user: ['Space', 'SpaceUser', 'List', 'Todo'],
291311
list: ['Todo'],
292312
}
293313
,
294314
authModel: 'User'
295315
};
316+
function $default$Space$ownerId(user: any): unknown {
317+
return user?.id;
318+
}
319+
296320
function $default$List$ownerId(user: any): unknown {
297321
return user?.id;
298322
}

src/lib/hooks/space.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ export function useCountSpace<TArgs extends Prisma.SpaceCountArgs, TQueryFnData
242242
return useModelQuery<TQueryFnData, TData, TError>('Space', `${endpoint}/space/count`, args, options, fetch);
243243
}
244244

245-
export function useCheckSpace<TError = DefaultError>(args: { operation: PolicyCrudKind; where?: { id?: string; name?: string; slug?: string }; }, options?: (StoreOrVal<Omit<CreateQueryOptions<boolean, TError, boolean>, 'queryKey'>> & ExtraQueryOptions)) {
245+
export function useCheckSpace<TError = DefaultError>(args: { operation: PolicyCrudKind; where?: { id?: string; ownerId?: string; name?: string; slug?: string }; }, options?: (StoreOrVal<Omit<CreateQueryOptions<boolean, TError, boolean>, 'queryKey'>> & ExtraQueryOptions)) {
246246
const { endpoint, fetch } = getHooksContext();
247247
return useModelQuery<boolean, boolean, TError>('Space', `${endpoint}/space/check`, args, options, fetch);
248248
}

src/routes/(app)/create-space/+page.server.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const actions = {
2222
data: {
2323
name,
2424
slug,
25+
owner: { connect: { id: locals.user.id } },
2526
members: {
2627
create: {
2728
user: { connect: { id: locals.user.id } },

src/routes/(auth)/signup/+page.server.ts

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const actions = {
3535
data: {
3636
name: `My Space`,
3737
slug: nanoid(8),
38+
owner: { connect: { id: user.id } },
3839
members: {
3940
create: {
4041
user: { connect: { id: user.id } },

0 commit comments

Comments
 (0)