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 missing folder access check

Kiran Kcb9fa81cd89a
diff --git a/apps/web/app/api/cron/domains/transfer/route.ts b/apps/web/app/api/cron/domains/transfer/route.ts
index a54a5f8af7..25d0fa3aae 100644
--- a/apps/web/app/api/cron/domains/transfer/route.ts
+++ b/apps/web/app/api/cron/domains/transfer/route.ts
@@ -53,9 +53,14 @@ export async function POST(req: Request) {
where: {
domain,
projectId: currentWorkspaceId,
- id: { in: linkIds },
+ id: {
+ in: linkIds,
+ },
+ },
+ data: {
+ projectId: newWorkspaceId,
+ folderId: null,
},
- data: { projectId: newWorkspaceId },
}),
prisma.linkTag.deleteMany({
@@ -73,7 +78,11 @@ export async function POST(req: Request) {
}),
recordLink(
- links.map((link) => ({ ...link, projectId: newWorkspaceId })),
+ links.map((link) => ({
+ ...link,
+ projectId: newWorkspaceId,
+ folderId: null,
+ })),
),
]);
diff --git a/apps/web/app/api/dashboards/route.ts b/apps/web/app/api/dashboards/route.ts
index af14cd10a1..8aeab0109f 100644
--- a/apps/web/app/api/dashboards/route.ts
+++ b/apps/web/app/api/dashboards/route.ts
@@ -1,6 +1,7 @@
import { getLinkOrThrow } from "@/lib/api/links/get-link-or-throw";
import { createId } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
+import { verifyFolderAccess } from "@/lib/folder/permissions";
import { dashboardSchema } from "@/lib/zod/schemas/dashboard";
import { domainKeySchema } from "@/lib/zod/schemas/links";
import { prisma } from "@dub/prisma";
@@ -24,7 +25,7 @@ export const GET = withWorkspace(
// POST /api/dashboards – create a new dashboard
export const POST = withWorkspace(
- async ({ searchParams, workspace }) => {
+ async ({ searchParams, workspace, session }) => {
const { domain, key } = domainKeySchema.parse(searchParams);
const link = await getLinkOrThrow({
@@ -33,6 +34,15 @@ export const POST = withWorkspace(
key,
});
+ if (link.folderId) {
+ await verifyFolderAccess({
+ workspaceId: workspace.id,
+ userId: session.user.id,
+ folderId: link.folderId,
+ requiredPermission: "folders.links.write",
+ });
+ }
+
const dashboard = await prisma.dashboard.create({
data: {
id: createId({ prefix: "dash_" }),
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index 27caa97c08..11a3244091 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -11,6 +11,7 @@ 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 { storage } from "@/lib/storage";
import { NewLinkProps, ProcessedLinkProps } from "@/lib/types";
import {
@@ -199,7 +200,7 @@ export const POST = withWorkspace(
// PATCH /api/links/bulk – bulk update up to 100 links with the same data
export const PATCH = withWorkspace(
- async ({ req, workspace, headers }) => {
+ async ({ req, workspace, headers, session }) => {
const { linkIds, externalIds, data } = bulkUpdateLinksBodySchema.parse(
await parseRequestBody(req),
);
@@ -273,6 +274,30 @@ export const PATCH = withWorkspace(
}
}
+ await Promise.all([
+ // ...(link.folderId
+ // ? [
+ // verifyFolderAccess({
+ // workspaceId: workspace.id,
+ // userId: session.user.id,
+ // folderId: link.folderId,
+ // requiredPermission: "folders.links.write",
+ // }),
+ // ]
+ // : []),
+
+ ...(data.folderId
+ ? [
+ verifyFolderAccess({
+ workspaceId: workspace.id,
+ userId: session.user.id,
+ folderId: data.folderId,
+ requiredPermission: "folders.links.write",
+ }),
+ ]
+ : []),
+ ]);
+
const processedLinks = await Promise.all(
links.map(async (link) =>
processLink({
diff --git a/apps/web/app/api/links/export/route.ts b/apps/web/app/api/links/export/route.ts
index 70185ba58e..03ecc44e65 100644
--- a/apps/web/app/api/links/export/route.ts
+++ b/apps/web/app/api/links/export/route.ts
@@ -91,7 +91,7 @@ export const GET = withWorkspace(
tags: { some: { tagId: { in: tagIds } } },
}),
...(userId && { userId }),
- folderId: folderId || null,
+ folderId: folderId || "",
},
// TODO: orderBy is not currently supported
diff --git a/apps/web/lib/folder/constants.ts b/apps/web/lib/folder/constants.ts
index a17cadcc81..adf275a832 100644
--- a/apps/web/lib/folder/constants.ts
+++ b/apps/web/lib/folder/constants.ts
@@ -15,8 +15,8 @@ export const FOLDER_USER_ROLE: Record<FolderUserRole, string> = {
export const FOLDER_PERMISSIONS = [
"folders.read",
"folders.write",
- "folders.links.write", // Move links to a folder
- "folders.users.write", // Add or remove users to a folder
+ "folders.links.write", // Add links to a folder
+ "folders.users.write", // Manage folder users
] as const;
export const FOLDER_WORKSPACE_ACCESS_TO_USER_ROLE: Record<
diff --git a/apps/web/lib/swr/use-folder-permissions.ts b/apps/web/lib/swr/use-folder-permissions.ts
index dfea3dc6b9..12d0fa4487 100644
--- a/apps/web/lib/swr/use-folder-permissions.ts
+++ b/apps/web/lib/swr/use-folder-permissions.ts
@@ -26,7 +26,11 @@ export function useCheckFolderPermission(
folderId: string | null,
action: FolderPermission,
) {
- const { folders } = useFolderPermissions();
+ const { folders, isLoading } = useFolderPermissions();
+
+ // if (isLoading) {
+ // return false;
+ // }
if (!folderId) {
return true;
diff --git a/apps/web/ui/links/link-controls.tsx b/apps/web/ui/links/link-controls.tsx
index 5c1927bbfc..897af184bf 100644
--- a/apps/web/ui/links/link-controls.tsx
+++ b/apps/web/ui/links/link-controls.tsx
@@ -1,5 +1,6 @@
import { mutatePrefix } from "@/lib/swr/mutate";
import useFolders from "@/lib/swr/use-folders";
+import useWorkspace from "@/lib/swr/use-workspace";
import { useArchiveLinkModal } from "@/ui/modals/archive-link-modal";
import { useDeleteLinkModal } from "@/ui/modals/delete-link-modal";
import {
@@ -26,6 +27,7 @@ import { ThreeDots } from "../shared/icons";
import { LinksListContext, ResponseLink } from "./links-container";
export function LinkControls({ link }: { link: ResponseLink }) {
+ const { flags } = useWorkspace();
const { slug } = useParams() as { slug?: string };
const { folders } = useFolders();
const { hovered } = useContext(CardList.Card.Context);
@@ -207,7 +209,7 @@ export function LinkControls({ link }: { link: ResponseLink }) {
</div>
<div className="border-t border-gray-200" />
<div className="grid gap-px p-2">
- {folders && folders.length > 0 && (
+ {flags?.linkFolders && folders && folders.length > 0 && (
<Button
text="Move"
variant="outline"