Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

add folder permissions

Kiran K06f6f06aa974
diff --git a/apps/web/app/api/folders/route.ts b/apps/web/app/api/folders/route.ts
index 11c22cd47a..19b4be09ee 100644
--- a/apps/web/app/api/folders/route.ts
+++ b/apps/web/app/api/folders/route.ts
@@ -55,7 +55,9 @@ export const POST = withWorkspace(
});
}
- const { name } = createFolderBodySchema.parse(await parseRequestBody(req));
+ const { name, users } = createFolderBodySchema.parse(
+ await parseRequestBody(req),
+ );
const existingFolder = await prisma.folder.findFirst({
where: {
@@ -71,10 +73,44 @@ export const POST = withWorkspace(
});
}
+ // Check the given users has access to the workspace
+ let usersWithAccess: { userId: string; accessLevel: string | null }[] = [];
+
+ if (users) {
+ const workspaceUsers = await prisma.projectUsers.findMany({
+ where: {
+ projectId: workspace.id,
+ AND: {
+ userId: {
+ in: users?.map((user) => user.userId) || [],
+ },
+ },
+ },
+ select: {
+ id: true,
+ userId: true,
+ },
+ });
+
+ usersWithAccess = workspaceUsers?.map(({ userId }) => {
+ const userAccess = users?.find((u) => u.userId === userId);
+
+ return {
+ userId,
+ accessLevel: userAccess?.accessLevel || null,
+ };
+ });
+ }
+
const folder = await prisma.folder.create({
data: {
projectId: workspace.id,
name,
+ ...(usersWithAccess.length > 0 && {
+ accessControls: {
+ create: usersWithAccess,
+ },
+ }),
},
});
diff --git a/apps/web/lib/zod/schemas/folders.ts b/apps/web/lib/zod/schemas/folders.ts
index c163a1e812..94d04002d3 100644
--- a/apps/web/lib/zod/schemas/folders.ts
+++ b/apps/web/lib/zod/schemas/folders.ts
@@ -1,12 +1,29 @@
import z from "@/lib/zod";
+const accessLevel = z
+ .enum(["READ", "WRITE"])
+ .describe("The access level of the folder access control.");
+
+const folderAccessControlSchema = z.object({
+ accessLevel: accessLevel.nullable(),
+ userId: z.string().describe("The unique ID of the user"), // TODO: Maybe we should support the email as well?
+});
+
export const folderSchema = z.object({
id: z.string().describe("The unique ID of the folder."),
name: z.string().describe("The name of the folder."),
+ // users: z
+ // .array(folderAccessControlSchema)
+ // .describe("The list of users and their access level to the folder.")
+ // .nullable(),
});
export const createFolderBodySchema = z.object({
name: z.string().describe("The name of the folder."),
+ users: z
+ .array(folderAccessControlSchema)
+ .describe("The list of users and their access level to the folder.")
+ .nullish(),
});
export const updateFolderBodySchema = createFolderBodySchema;
diff --git a/apps/web/prisma/schema/folder.prisma b/apps/web/prisma/schema/folder.prisma
index e790bb9abb..1071173609 100644
--- a/apps/web/prisma/schema/folder.prisma
+++ b/apps/web/prisma/schema/folder.prisma
@@ -5,9 +5,25 @@ model Folder {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
- project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
- links Link[]
+ project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
+ links Link[]
+ accessControls FolderAccessControl[]
@@unique([name, projectId])
@@index(projectId)
}
+
+model FolderAccessControl {
+ id String @id @default(cuid())
+ folderId String
+ userId String
+ accessLevel String? // READ, WRITE
+ createdAt DateTime @default(now())
+ updatedAt DateTime @updatedAt
+
+ folder Folder @relation(fields: [folderId], references: [id], onDelete: Cascade)
+ user User @relation(fields: [userId], references: [id], onDelete: Cascade)
+
+ @@unique([folderId, userId])
+ @@index([userId])
+}
diff --git a/apps/web/prisma/schema/schema.prisma b/apps/web/prisma/schema/schema.prisma
index f5c5a804a5..747e4bbad4 100644
--- a/apps/web/prisma/schema/schema.prisma
+++ b/apps/web/prisma/schema/schema.prisma
@@ -39,6 +39,7 @@ model User {
oAuthCodes OAuthCode[]
integrations Integration[] // Integrations user created in their workspace
installedIntegrations InstalledIntegration[] // Integrations user installed in their workspace
+ folderPermissions FolderAccessControl[]
@@index(source)
@@index(defaultWorkspace)
diff --git a/apps/web/prisma/schema/token.prisma b/apps/web/prisma/schema/token.prisma
index fd5721eb87..e7d5de5918 100644
--- a/apps/web/prisma/schema/token.prisma
+++ b/apps/web/prisma/schema/token.prisma
@@ -30,7 +30,6 @@ model RestrictedToken {
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
- // authorizedApp OAuthAuthorizedApp? @relation(fields: [installationId], references: [id], onDelete: Cascade)
refreshTokens OAuthRefreshToken[]
installedIntegration InstalledIntegration? @relation(fields: [installationId], references: [id], onDelete: Cascade)