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.

apply folder permissions

Kiran Kfc36812b8782
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index 22d5a80b17..ebd6723ae3 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -9,6 +9,7 @@ import { getLinkOrThrow } from "@/lib/api/links/get-link-or-throw";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { getFolderWithUserOrThrow } from "@/lib/link-folder/get-folder";
+import { throwIfNotAllowed } from "@/lib/link-folder/permissions";
import { prisma } from "@/lib/prisma";
import { NewLinkProps } from "@/lib/types";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
@@ -19,12 +20,26 @@ import { NextResponse } from "next/server";
// GET /api/links/[linkId] – get a link
export const GET = withWorkspace(
- async ({ headers, workspace, params }) => {
+ async ({ headers, workspace, params, session }) => {
const link = await getLinkOrThrow({
workspace,
linkId: params.linkId,
});
+ if (link.folderId) {
+ const { folder, folderUser } = await getFolderWithUserOrThrow({
+ folderId: link.folderId,
+ workspaceId: workspace.id,
+ userId: session.user.id,
+ });
+
+ throwIfNotAllowed({
+ folder,
+ folderUser,
+ requiredPermission: "folders.links.read",
+ });
+ }
+
const tags = await prisma.tag.findMany({
where: {
links: {
@@ -64,13 +79,32 @@ export const PATCH = withWorkspace(
const body = updateLinkBodySchema.parse(await parseRequestBody(req)) || {};
- // Check if the user has edit access to the folder
+ if (link.folderId) {
+ const { folder, folderUser } = await getFolderWithUserOrThrow({
+ folderId: link.folderId,
+ workspaceId: workspace.id,
+ userId: session.user.id,
+ });
+
+ throwIfNotAllowed({
+ folder,
+ folderUser,
+ requiredPermission: "folders.links.write",
+ });
+ }
+
if (body.folderId) {
- await getFolderWithUserOrThrow({
+ const { folder, folderUser } = await getFolderWithUserOrThrow({
folderId: body.folderId,
workspaceId: workspace.id,
userId: session.user.id,
});
+
+ throwIfNotAllowed({
+ folder,
+ folderUser,
+ requiredPermission: "folders.links.write",
+ });
}
// Add body onto existing link but maintain NewLinkProps form for processLink
@@ -176,12 +210,26 @@ export const PUT = PATCH;
// DELETE /api/links/[linkId] – delete a link
export const DELETE = withWorkspace(
- async ({ headers, params, workspace }) => {
+ async ({ headers, params, workspace, session }) => {
const link = await getLinkOrThrow({
workspace,
linkId: params.linkId,
});
+ if (link.folderId) {
+ const { folder, folderUser } = await getFolderWithUserOrThrow({
+ folderId: link.folderId,
+ workspaceId: workspace.id,
+ userId: session.user.id,
+ });
+
+ throwIfNotAllowed({
+ folder,
+ folderUser,
+ requiredPermission: "folders.links.write",
+ });
+ }
+
await deleteLink(link.id);
waitUntil(
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index 067f0c8eb4..0b921ba776 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -5,6 +5,7 @@ import { throwIfLinksUsageExceeded } from "@/lib/api/links/usage-checks";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { getFolderWithUserOrThrow } from "@/lib/link-folder/get-folder";
+import { throwIfNotAllowed } from "@/lib/link-folder/permissions";
import { ratelimit } from "@/lib/upstash";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import {
@@ -18,7 +19,7 @@ import { NextResponse } from "next/server";
// GET /api/links – get all links for a workspace
export const GET = withWorkspace(
- async ({ req, headers, workspace }) => {
+ async ({ req, headers, workspace, session }) => {
const searchParams = getSearchParamsWithArray(req.url);
const {
@@ -40,6 +41,23 @@ export const GET = withWorkspace(
await getDomainOrThrow({ workspace, domain });
}
+ if (folderId) {
+ const { folder, folderUser } = await getFolderWithUserOrThrow({
+ folderId,
+ workspaceId: workspace.id,
+ userId: session.user.id,
+ });
+
+ throwIfNotAllowed({
+ folder,
+ folderUser,
+ requiredPermission: "folders.links.read",
+ });
+ }
+
+ // TODO: @LinkFolder
+ // Filter out the links that the user does not have access to based on the folders
+
const response = await getLinksForWorkspace({
workspaceId: workspace.id,
domain,
@@ -89,11 +107,17 @@ export const POST = withWorkspace(
// Check if the user has edit access to the folder
if (body.folderId) {
- await getFolderWithUserOrThrow({
+ const { folder, folderUser } = await getFolderWithUserOrThrow({
folderId: body.folderId,
workspaceId: workspace.id,
userId: session.user.id,
});
+
+ throwIfNotAllowed({
+ folder,
+ folderUser,
+ requiredPermission: "folders.links.write",
+ });
}
const { link, error, code } = await processLink({