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.

check for folder permissions and filter

Kiran Kfd58701d61ae
diff --git a/apps/web/app/api/folders/route.ts b/apps/web/app/api/folders/route.ts
index 1e88d61c44..c4a956e403 100644
--- a/apps/web/app/api/folders/route.ts
+++ b/apps/web/app/api/folders/route.ts
@@ -1,6 +1,7 @@
import { DubApiError, exceededLimitError } from "@/lib/api/errors";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
+import { getFolders } from "@/lib/link-folder/get-folders";
import { prisma } from "@/lib/prisma";
import { createFolderSchema, folderSchema } from "@/lib/zod/schemas/folders";
import { FolderAccessLevel } from "@prisma/client";
@@ -9,52 +10,13 @@ import { NextResponse } from "next/server";
// GET /api/folders - get all folders for a workspace
export const GET = withWorkspace(
async ({ workspace, headers, session }) => {
- const folders = await prisma.folder.findMany({
- where: {
- projectId: workspace.id,
- OR: [
- { accessLevel: { not: null } },
- {
- users: {
- some: {
- userId: session.user.id,
- role: { not: null },
- },
- },
- },
- ],
- NOT: {
- users: {
- some: {
- userId: session.user.id,
- role: null,
- },
- },
- },
- },
- orderBy: {
- createdAt: "desc",
- },
- select: {
- id: true,
- name: true,
- accessLevel: true,
- createdAt: true,
- updatedAt: true,
- _count: {
- select: {
- links: true,
- },
- },
- },
+ const folders = await getFolders({
+ workspaceId: workspace.id,
+ userId: session.user.id,
+ includeLinkCount: true,
});
- const formattedFolders = folders.map((folder) => ({
- ...folder,
- linkCount: folder._count.links,
- }));
-
- return NextResponse.json(folderSchema.array().parse(formattedFolders), {
+ return NextResponse.json(folderSchema.array().parse(folders), {
headers,
});
},
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index ebd6723ae3..71d363df43 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -36,7 +36,7 @@ export const GET = withWorkspace(
throwIfNotAllowed({
folder,
folderUser,
- requiredPermission: "folders.links.read",
+ requiredPermission: "folders.read",
});
}
diff --git a/apps/web/app/api/links/count/route.ts b/apps/web/app/api/links/count/route.ts
index 5a386699dd..4d5c5aa982 100644
--- a/apps/web/app/api/links/count/route.ts
+++ b/apps/web/app/api/links/count/route.ts
@@ -1,22 +1,45 @@
import { getDomainOrThrow } from "@/lib/api/domains/get-domain-or-throw";
import { getLinksCount } from "@/lib/api/links";
import { withWorkspace } from "@/lib/auth";
+import { getFolderWithUserOrThrow } from "@/lib/link-folder/get-folder";
+import { getFolders } from "@/lib/link-folder/get-folders";
+import { throwIfNotAllowed } from "@/lib/link-folder/permissions";
import { getLinksCountQuerySchema } from "@/lib/zod/schemas/links";
import { NextResponse } from "next/server";
// GET /api/links/count – get the number of links for a workspace
export const GET = withWorkspace(
- async ({ headers, searchParams, workspace }) => {
+ async ({ headers, searchParams, workspace, session }) => {
const params = getLinksCountQuerySchema.parse(searchParams);
- const { domain } = params;
+ const { domain, folderId } = params;
if (domain) {
await getDomainOrThrow({ domain, workspace: workspace });
}
+ if (folderId) {
+ const { folder, folderUser } = await getFolderWithUserOrThrow({
+ folderId,
+ workspaceId: workspace.id,
+ userId: session.user.id,
+ });
+
+ throwIfNotAllowed({
+ folder,
+ folderUser,
+ requiredPermission: "folders.read",
+ });
+ }
+
+ const folders = await getFolders({
+ workspaceId: workspace.id,
+ userId: session.user.id,
+ });
+
const count = await getLinksCount({
searchParams: params,
workspaceId: workspace.id,
+ folderIds: folders.map((folder) => folder.id),
});
return NextResponse.json(count, {
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index 0b921ba776..f8db536641 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 { getFolders } from "@/lib/link-folder/get-folders";
import { throwIfNotAllowed } from "@/lib/link-folder/permissions";
import { ratelimit } from "@/lib/upstash";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
@@ -51,12 +52,14 @@ export const GET = withWorkspace(
throwIfNotAllowed({
folder,
folderUser,
- requiredPermission: "folders.links.read",
+ requiredPermission: "folders.read",
});
}
- // TODO: @LinkFolder
- // Filter out the links that the user does not have access to based on the folders
+ const folders = await getFolders({
+ workspaceId: workspace.id,
+ userId: session.user.id,
+ });
const response = await getLinksForWorkspace({
workspaceId: workspace.id,
@@ -72,6 +75,7 @@ export const GET = withWorkspace(
showArchived,
withTags,
includeUser,
+ folderIds: folders.map((folder) => folder.id),
});
return NextResponse.json(response, {
diff --git a/apps/web/lib/api/links/get-links-count.ts b/apps/web/lib/api/links/get-links-count.ts
index 7cb3401ff6..81e4d38ee4 100644
--- a/apps/web/lib/api/links/get-links-count.ts
+++ b/apps/web/lib/api/links/get-links-count.ts
@@ -6,10 +6,11 @@ import { combineTagIds } from "./utils";
export async function getLinksCount({
searchParams,
workspaceId,
+ folderIds,
}: {
searchParams: z.infer<typeof getLinksCountQuerySchema>;
workspaceId: string;
- userId?: string | null;
+ folderIds?: string[];
}) {
const {
groupBy,
@@ -53,6 +54,8 @@ export async function getLinksCount({
groupBy !== "folderId" && {
folderId,
}),
+
+ OR: [{ folderId: { in: folderIds } }, { folderId: null }],
};
if (groupBy === "tagId") {
diff --git a/apps/web/lib/api/links/get-links-for-workspace.ts b/apps/web/lib/api/links/get-links-for-workspace.ts
index 3f931e5c14..7a078556e3 100644
--- a/apps/web/lib/api/links/get-links-for-workspace.ts
+++ b/apps/web/lib/api/links/get-links-for-workspace.ts
@@ -9,7 +9,6 @@ export async function getLinksForWorkspace({
tagId,
tagIds,
tagNames,
- folderId,
search,
sort = "createdAt",
page,
@@ -18,8 +17,11 @@ export async function getLinksForWorkspace({
showArchived,
withTags,
includeUser,
+ folderId,
+ folderIds,
}: z.infer<typeof getLinksQuerySchemaExtended> & {
workspaceId: string;
+ folderIds?: string[];
}) {
const combinedTagIds = combineTagIds({ tagId, tagIds });
@@ -61,13 +63,8 @@ export async function getLinksForWorkspace({
}
: {}),
...(userId && { userId }),
-
- // Filter by folderId
- ...(folderId
- ? {
- folderId,
- }
- : {}),
+ ...(folderId && { folderId }),
+ OR: [{ folderId: { in: folderIds } }, { folderId: null }],
},
include: {
user: includeUser,
diff --git a/apps/web/lib/link-folder/constants.ts b/apps/web/lib/link-folder/constants.ts
index 875726abe1..004225bba3 100644
--- a/apps/web/lib/link-folder/constants.ts
+++ b/apps/web/lib/link-folder/constants.ts
@@ -15,7 +15,6 @@ export const FOLDER_USER_ROLE = {
export const FOLDER_PERMISSIONS = [
"folders.read",
"folders.write",
- "folders.links.read",
"folders.links.write", // Move links to a folder
"folders.users.write", // Add or remove users to a folder
] as const;
@@ -35,10 +34,9 @@ export const FOLDER_USER_ROLE_TO_PERMISSIONS: Record<
owner: [
"folders.read",
"folders.write",
- "folders.links.read",
"folders.links.write",
"folders.users.write",
],
- editor: ["folders.read", "folders.links.read", "folders.links.write"],
- viewer: ["folders.read", "folders.links.read"],
+ editor: ["folders.read", "folders.links.write"],
+ viewer: ["folders.read"],
} as const;
diff --git a/apps/web/lib/link-folder/get-folders.ts b/apps/web/lib/link-folder/get-folders.ts
new file mode 100644
index 0000000000..62708f141a
--- /dev/null
+++ b/apps/web/lib/link-folder/get-folders.ts
@@ -0,0 +1,61 @@
+import { prisma } from "@/lib/prisma";
+
+export const getFolders = async ({
+ workspaceId,
+ userId,
+ includeLinkCount = false,
+}: {
+ workspaceId: string;
+ userId: string;
+ includeLinkCount?: boolean;
+}) => {
+ const folders = await prisma.folder.findMany({
+ where: {
+ projectId: workspaceId,
+ OR: [
+ { accessLevel: { not: null } },
+ {
+ users: {
+ some: {
+ userId,
+ role: { not: null },
+ },
+ },
+ },
+ ],
+ NOT: {
+ users: {
+ some: {
+ userId,
+ role: null,
+ },
+ },
+ },
+ },
+ orderBy: {
+ createdAt: "desc",
+ },
+ select: {
+ id: true,
+ name: true,
+ accessLevel: true,
+ createdAt: true,
+ updatedAt: true,
+
+ ...(includeLinkCount && {
+ _count: {
+ select: {
+ links: true,
+ },
+ },
+ }),
+ },
+ });
+
+ return folders.map((folder) => ({
+ ...folder,
+ ...(includeLinkCount && {
+ linkCount: folder._count.links,
+ }),
+ }));
+};
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index 66680c2902..2e6bcbe5db 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -54,6 +54,11 @@ const LinksQuerySchema = z.object({
.string()
.optional()
.describe("The folder ID to filter the links by."),
+ // folderIds: z
+ // .union([z.string(), z.array(z.string())])
+ // .transform((v) => (Array.isArray(v) ? v : v.split(",")))
+ // .optional()
+ // .describe("The folder IDs to filter the links by."),
search: z
.string()
.optional()