Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: add group links

Marc Seitz4676e39dfcb4
diff --git a/lib/api/links/link-data.ts b/lib/api/links/link-data.ts
index a9150234..402d8fa9 100644
--- a/lib/api/links/link-data.ts
+++ b/lib/api/links/link-data.ts
@@ -1,7 +1,37 @@
+import { ItemType, ViewerGroupAccessControls } from "@prisma/client";
+
import prisma from "@/lib/prisma";
import { sortItemsByIndexAndName } from "@/lib/utils/sort-items-by-index-name";
-export async function fetchDataroomLinkData({ linkId }: { linkId: string }) {
+export async function fetchDataroomLinkData({
+ linkId,
+ groupId,
+}: {
+ linkId: string;
+ groupId?: string;
+}) {
+ let groupPermissions: ViewerGroupAccessControls[] = [];
+ let documentIds: string[] = [];
+ let folderIds: string[] = [];
+
+ if (groupId) {
+ groupPermissions = await prisma.viewerGroupAccessControls.findMany({
+ where: {
+ groupId,
+ OR: [{ canView: true }, { canDownload: true }],
+ },
+ });
+
+ documentIds = groupPermissions
+ .filter(
+ (permission) => permission.itemType === ItemType.DATAROOM_DOCUMENT,
+ )
+ .map((permission) => permission.itemId);
+ folderIds = groupPermissions
+ .filter((permission) => permission.itemType === ItemType.DATAROOM_FOLDER)
+ .map((permission) => permission.itemId);
+ }
+
const linkData = await prisma.link.findUnique({
where: { id: linkId },
select: {
@@ -11,6 +41,10 @@ export async function fetchDataroomLinkData({ linkId }: { linkId: string }) {
name: true,
teamId: true,
documents: {
+ where:
+ groupPermissions.length > 0 || groupId
+ ? { id: { in: documentIds } }
+ : undefined,
select: {
id: true,
folderId: true,
@@ -38,19 +72,16 @@ export async function fetchDataroomLinkData({ linkId }: { linkId: string }) {
orderBy: [
{ orderIndex: "asc" },
{
- document: {
- name: "asc",
- },
+ document: { name: "asc" },
},
],
},
folders: {
- orderBy: [
- { orderIndex: "asc" },
- {
- name: "asc",
- },
- ],
+ where:
+ groupPermissions.length > 0 || groupId
+ ? { id: { in: folderIds } }
+ : undefined,
+ orderBy: [{ orderIndex: "asc" }, { name: "asc" }],
},
},
},
diff --git a/pages/api/links/[id]/index.ts b/pages/api/links/[id]/index.ts
index 398b1bcd..948db158 100644
--- a/pages/api/links/[id]/index.ts
+++ b/pages/api/links/[id]/index.ts
@@ -1,6 +1,6 @@
import { NextApiRequest, NextApiResponse } from "next";
-import { Brand, DataroomBrand } from "@prisma/client";
+import { Brand, DataroomBrand, LinkAudienceType } from "@prisma/client";
import { getServerSession } from "next-auth/next";
import {
@@ -54,6 +54,8 @@ export default async function handle(
showBanner: true,
enableWatermark: true,
watermarkConfig: true,
+ groupId: true,
+ audienceType: true,
},
});
@@ -77,7 +79,13 @@ export default async function handle(
linkData = data.linkData;
brand = data.brand;
} else if (linkType === "DATAROOM_LINK") {
- const data = await fetchDataroomLinkData({ linkId: id });
+ const data = await fetchDataroomLinkData({
+ linkId: id,
+ ...(link.audienceType === LinkAudienceType.GROUP &&
+ link.groupId && {
+ groupId: link.groupId,
+ }),
+ });
linkData = data.linkData;
brand = data.brand;
}
@@ -177,7 +185,10 @@ export default async function handle(
dataroomId: dataroomLink ? targetId : null,
password: hashedPassword,
name: linkData.name || null,
- emailProtected: linkData.emailProtected,
+ emailProtected:
+ linkData.audienceType === LinkAudienceType.GROUP
+ ? true
+ : linkData.emailProtected,
emailAuthenticated: linkData.emailAuthenticated,
allowDownload: linkData.allowDownload,
allowList: linkData.allowList,
@@ -215,6 +226,8 @@ export default async function handle(
showBanner: linkData.showBanner,
enableWatermark: linkData.enableWatermark || false,
watermarkConfig: linkData.watermarkConfig || null,
+ groupId: linkData.groupId || null,
+ audienceType: linkData.audienceType || LinkAudienceType.GENERAL,
},
include: {
views: {
diff --git a/pages/api/links/domains/[...domainSlug].ts b/pages/api/links/domains/[...domainSlug].ts
index b73b4364..24876dc9 100644
--- a/pages/api/links/domains/[...domainSlug].ts
+++ b/pages/api/links/domains/[...domainSlug].ts
@@ -1,6 +1,6 @@
import { NextApiRequest, NextApiResponse } from "next";
-import { Brand, DataroomBrand } from "@prisma/client";
+import { Brand, DataroomBrand, LinkAudienceType } from "@prisma/client";
import {
fetchDataroomLinkData,
@@ -64,6 +64,8 @@ export default async function handle(
showBanner: true,
enableWatermark: true,
watermarkConfig: true,
+ groupId: true,
+ audienceType: true,
document: {
select: {
team: {
@@ -131,7 +133,13 @@ export default async function handle(
linkData = data.linkData;
brand = data.brand;
} else if (linkType === "DATAROOM_LINK") {
- const data = await fetchDataroomLinkData({ linkId: link.id });
+ const data = await fetchDataroomLinkData({
+ linkId: link.id,
+ ...(link.audienceType === LinkAudienceType.GROUP &&
+ link.groupId && {
+ groupId: link.groupId,
+ }),
+ });
linkData = data.linkData;
brand = data.brand;
}
diff --git a/pages/api/links/index.ts b/pages/api/links/index.ts
index 8473055e..b17fd547 100644
--- a/pages/api/links/index.ts
+++ b/pages/api/links/index.ts
@@ -122,6 +122,15 @@ export default async function handler(
});
}
+ if (
+ linkData.audienceType === LinkAudienceType.GROUP &&
+ !linkData.groupId
+ ) {
+ return res.status(400).json({
+ error: "No group selected.",
+ });
+ }
+
// Fetch the link and its related document from the database
const link = await prisma.link.create({
data: {
@@ -130,7 +139,10 @@ export default async function handler(
linkType,
password: hashedPassword,
name: linkData.name || null,
- emailProtected: linkData.emailProtected,
+ emailProtected:
+ linkData.audienceType === LinkAudienceType.GROUP
+ ? true
+ : linkData.emailProtected,
emailAuthenticated: linkData.emailAuthenticated,
expiresAt: exat,
allowDownload: linkData.allowDownload,
diff --git a/pages/api/views-dataroom.ts b/pages/api/views-dataroom.ts
index f23ada27..73db0867 100644
--- a/pages/api/views-dataroom.ts
+++ b/pages/api/views-dataroom.ts
@@ -1,5 +1,6 @@
import { NextApiRequest, NextApiResponse } from "next";
+import { LinkAudienceType } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { getServerSession } from "next-auth/next";
import { parsePageId } from "notion-utils";
@@ -47,6 +48,7 @@ export default async function handle(
linkType,
dataroomViewId,
viewType,
+ groupId,
...data
} = req.body as {
linkId: string;
@@ -63,6 +65,7 @@ export default async function handle(
linkType: string;
dataroomViewId?: string;
viewType: "DATAROOM_VIEW" | "DOCUMENT_VIEW";
+ groupId?: string;
};
const { email, password, name, hasConfirmedAgreement } = data as {
@@ -101,6 +104,8 @@ export default async function handle(
agreementId: true,
enableWatermark: true,
watermarkConfig: true,
+ groupId: true,
+ audienceType: true,
},
});
@@ -189,6 +194,27 @@ export default async function handle(
}
}
+ // Check if group is allowed to visit the link
+ if (link.audienceType === LinkAudienceType.GROUP && link.groupId) {
+ const group = await prisma.viewerGroup.findUnique({
+ where: { id: link.groupId },
+ select: { members: { include: { viewer: { select: { email: true } } } } },
+ });
+
+ if (!group) {
+ res.status(404).json({ message: "Group not found." });
+ return;
+ }
+
+ const isMember = group.members.some(
+ (member) => member.viewer.email === email,
+ );
+ if (!isMember) {
+ res.status(403).json({ message: "Unauthorized access" });
+ return;
+ }
+ }
+
// Check if email verification is required for visiting the link
if (link.emailAuthenticated && !token && !dataroomVerified) {
const token = newId("email");
@@ -353,6 +379,10 @@ export default async function handle(
},
},
}),
+ ...(link.audienceType === LinkAudienceType.GROUP &&
+ link.groupId && {
+ groupId: link.groupId,
+ }),
},
select: { id: true },
});
@@ -409,6 +439,10 @@ export default async function handle(
},
},
}),
+ ...(link.audienceType === LinkAudienceType.GROUP &&
+ link.groupId && {
+ groupId: link.groupId,
+ }),
},
select: { id: true },
});