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 permission checks for bulk link deletion

Kiran K07be88aad927
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index 11a3244091..734023610d 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -11,7 +11,10 @@ import { throwIfLinksUsageExceeded } from "@/lib/api/links/usage-checks";
import { combineTagIds } from "@/lib/api/tags/combine-tag-ids";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
-import { verifyFolderAccess } from "@/lib/folder/permissions";
+import {
+ hasFolderPermission,
+ verifyFolderAccess,
+} from "@/lib/folder/permissions";
import { storage } from "@/lib/storage";
import { NewLinkProps, ProcessedLinkProps } from "@/lib/types";
import {
@@ -374,7 +377,7 @@ export const PATCH = withWorkspace(
// DELETE /api/links/bulk – bulk delete up to 100 links
export const DELETE = withWorkspace(
- async ({ workspace, headers, searchParams }) => {
+ async ({ workspace, headers, searchParams, session }) => {
const searchParamsLinkIds = searchParams["linkIds"]
? searchParams["linkIds"].split(",")
: [];
@@ -408,7 +411,7 @@ export const DELETE = withWorkspace(
});
}
- const links = await prisma.link.findMany({
+ let links = await prisma.link.findMany({
where: {
projectId: workspace.id,
programId: null,
@@ -428,6 +431,37 @@ export const DELETE = withWorkspace(
},
});
+ const uniqueFolderIds = [
+ ...new Set(
+ links
+ .map((link) => link.folderId)
+ .filter((folderId): folderId is string => folderId !== null),
+ ),
+ ];
+
+ let folderAccess: Record<string, boolean> = {};
+
+ if (uniqueFolderIds.length > 0) {
+ folderAccess = Object.fromEntries(
+ await Promise.all(
+ uniqueFolderIds.map(async (folderId) => [
+ folderId,
+ await hasFolderPermission({
+ workspaceId: workspace.id,
+ userId: session.user.id,
+ folderId,
+ requiredPermission: "folders.links.write",
+ }),
+ ]),
+ ),
+ );
+ }
+
+ // filter links that the user has access to
+ links = links.filter((link) => {
+ return link.folderId ? folderAccess[link.folderId] : true;
+ });
+
const { count: deletedCount } = await prisma.link.deleteMany({
where: {
id: { in: links.map((link) => link.id) },
diff --git a/apps/web/lib/folder/permissions.ts b/apps/web/lib/folder/permissions.ts
index f8ba40489d..6cfce7f9de 100644
--- a/apps/web/lib/folder/permissions.ts
+++ b/apps/web/lib/folder/permissions.ts
@@ -79,3 +79,29 @@ export const findUserFolderRole = ({
return FOLDER_WORKSPACE_ACCESS_TO_FOLDER_USER_ROLE[folder.accessLevel];
};
+
+// A version of verifyFolderAccess that does not throw an error if the user does not have access
+export const hasFolderPermission = async ({
+ workspaceId,
+ userId,
+ folderId,
+ requiredPermission,
+}: {
+ workspaceId: string;
+ userId: string;
+ folderId: string;
+ requiredPermission: FolderPermission;
+}) => {
+ try {
+ await verifyFolderAccess({
+ workspaceId,
+ userId,
+ folderId,
+ requiredPermission,
+ });
+
+ return true;
+ } catch (error) {
+ return false;
+ }
+};