Skip to content

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

feat(api): v1 dataroom group management, group permissions, and link permissions

Marc Seitz4dfd1308869e
diff --git a/app/api/views-dataroom/route.ts b/app/api/views-dataroom/route.ts
index 271ec9bd..54ba16dc 100644
--- a/app/api/views-dataroom/route.ts
+++ b/app/api/views-dataroom/route.ts
@@ -39,7 +39,11 @@ import {
import { recordLinkView } from "@/lib/tracking/record-link-view";
import { CustomUser, WatermarkConfigSchema } from "@/lib/types";
import { checkPassword, decryptEncrpytedPassword, log } from "@/lib/utils";
-import { extractEmailDomain, isEmailMatched } from "@/lib/utils/email-domain";
+import {
+ extractEmailDomain,
+ isEmailMatched,
+ normalizeGroupDomain,
+} from "@/lib/utils/email-domain";
import { generateOTP } from "@/lib/utils/generate-otp";
import { LOCALHOST_IP } from "@/lib/utils/geo";
import { checkGlobalBlockList } from "@/lib/utils/global-block-list";
@@ -521,11 +525,14 @@ export async function POST(request: NextRequest) {
(member) => member.viewer.email === effectiveEmail,
);
- // Extract domain from email
+ // Extract domain from email (canonical "@acme.com" form)
const emailDomain = extractEmailDomain(effectiveEmail ?? "");
- // Check domain access
+ // Check domain access. Normalize each stored domain so bare-domain
+ // rows (e.g. created before domain normalization) still match.
const hasDomainAccess = emailDomain
- ? group.domains.some((domain) => domain === emailDomain)
+ ? group.domains.some(
+ (domain) => normalizeGroupDomain(domain) === emailDomain,
+ )
: false;
if (!isMember && !hasDomainAccess) {
diff --git a/lib/dataroom/apply-permissions.ts b/lib/dataroom/apply-permissions.ts
new file mode 100644
index 00000000..ee1d2314
--- /dev/null
+++ b/lib/dataroom/apply-permissions.ts
@@ -0,0 +1,269 @@
+import { ItemType, Prisma } from "@prisma/client";
+
+import cuid from "cuid";
+
+import {
+ type AccessControlTable,
+ type AncestorUpsertRow,
+ type PermissionUpsertRow,
+ buildBulkUpsertPermissionsSql,
+ buildDeletePermissionsNotInPayloadSql,
+ buildFindAncestorFolderIdsSql,
+ buildUpsertAncestorVisibilitySql,
+} from "@/lib/dataroom/permissions-sql";
+import prisma from "@/lib/prisma";
+
+/**
+ * Canonical writer for dataroom access-control rows, shared by every consumer
+ * that saves per-item permissions (viewer groups and per-link permission
+ * groups). It owns the sensitive parts of the save — dedupe, the recursive
+ * ancestor-visibility expansion, transaction timing, and which bulk SQL runs
+ * in which order — so the delta and full-replace semantics can't drift apart
+ * across call sites. Routes keep only validation, target resolution, and
+ * serialization.
+ *
+ * The two write modes:
+ * - `delta`: entries in the payload are upserted; omitted items keep their
+ * current state. Nothing is deleted. (Viewer-group semantics.)
+ * - `replace`: the payload (plus its ancestor expansion) is the complete
+ * desired state; any row not in it is deleted. An empty payload clears
+ * every row for the group. (Per-link permission-group semantics.)
+ *
+ * In both modes, ancestor folders of any item made visible are forced to
+ * `canView: true` so the folder tree stays navigable.
+ */
+
+export type PermissionWriteMode = "delta" | "replace";
+
+/** A single normalized permission entry (internal shape, not the public API). */
+export type PermissionEntryInput = {
+ itemId: string;
+ itemType: ItemType;
+ canView: boolean;
+ canDownload: boolean;
+};
+
+export type PermissionWriteTarget = {
+ table: AccessControlTable;
+ /**
+ * Resolve the group id to write to. Runs inside the write transaction so
+ * callers that must create/claim a group atomically (per-link permission
+ * groups) can do so without a second transaction; callers with a known
+ * group id just return it.
+ */
+ resolveGroupId: (tx: Prisma.TransactionClient) => Promise<string>;
+};
+
+/**
+ * One effective access-control row, normalized across both tables.
+ * `ViewerGroupAccessControls` has no `canDownloadOriginal` column, so it is
+ * surfaced as `false` there — giving both serializers a single row shape.
+ */
+export type EffectivePermissionRow = {
+ id: string;
+ itemId: string;
+ itemType: ItemType;
+ canView: boolean;
+ canDownload: boolean;
+ canDownloadOriginal: boolean;
+ updatedAt: Date;
+};
+
+export type ApplyDataroomPermissionsResult = {
+ groupId: string;
+ /** Payload items plus auto-upserted ancestor folders. */
+ touchedItemIds: string[];
+ /** Effective rows for every touched item, ordered by id. */
+ rows: EffectivePermissionRow[];
+};
+
+export async function applyDataroomPermissions({
+ target,
+ dataroomId,
+ entries,
+ mode,
+}: {
+ target: PermissionWriteTarget;
+ dataroomId: string;
+ entries: PermissionEntryInput[];
+ mode: PermissionWriteMode;
+}): Promise<ApplyDataroomPermissionsResult> {
+ const { table, resolveGroupId } = target;
+
+ // Last entry wins when the same item appears twice.
+ const dedupedEntries = Array.from(
+ new Map(entries.map((e) => [e.itemId, e])).values(),
+ );
+
+ const upsertRows: PermissionUpsertRow[] = dedupedEntries.map((e) => ({
+ id: cuid(),
+ itemId: e.itemId,
+ itemType: e.itemType,
+ canView: e.canView,
+ canDownload: e.canDownload,
+ }));
+
+ const visibleDocumentIds: string[] = [];
+ const visibleFolderIds: string[] = [];
+ for (const e of dedupedEntries) {
+ if (!e.canView) continue;
+ switch (e.itemType) {
+ case ItemType.DATAROOM_DOCUMENT:
+ visibleDocumentIds.push(e.itemId);
+ break;
+ case ItemType.DATAROOM_FOLDER:
+ visibleFolderIds.push(e.itemId);
+ break;
+ default: {
+ const _exhaustive: never = e.itemType;
+ throw new Error(`Unhandled item type: ${_exhaustive}`);
+ }
+ }
+ }
+
+ // The ancestor CTE only reads DataroomFolder/DataroomDocument (immutable
+ // from this caller's perspective) and doesn't depend on the group id, so we
+ // build it once here. The group-scoped upsert/delete SQL is built inside the
+ // transaction, after the group id is resolved.
+ const findAncestorsSql = buildFindAncestorFolderIdsSql(
+ dataroomId,
+ visibleDocumentIds,
+ visibleFolderIds,
+ );
+
+ const { groupId, ancestorIds } = await prisma.$transaction(
+ async (tx) => {
+ const resolvedGroupId = await resolveGroupId(tx);
+
+ const bulkUpsertSql = buildBulkUpsertPermissionsSql(
+ table,
+ resolvedGroupId,
+ upsertRows,
+ );
+ if (bulkUpsertSql) {
+ await tx.$executeRaw(bulkUpsertSql);
+ }
+
+ const ancestors = new Set<string>();
+ if (findAncestorsSql) {
+ const ancestorRows =
+ await tx.$queryRaw<{ folder_id: string }[]>(findAncestorsSql);
+ for (const r of ancestorRows) ancestors.add(r.folder_id);
+
+ if (ancestorRows.length > 0) {
+ const ancestorRowsToUpsert: AncestorUpsertRow[] = ancestorRows.map(
+ (r) => ({ id: cuid(), folderId: r.folder_id }),
+ );
+ const ancestorUpsertSql = buildUpsertAncestorVisibilitySql(
+ table,
+ resolvedGroupId,
+ ancestorRowsToUpsert,
+ );
+ if (ancestorUpsertSql) {
+ await tx.$executeRaw(ancestorUpsertSql);
+ }
+ }
+ }
+
+ if (mode === "replace") {
+ const keepItemIds = [
+ ...dedupedEntries.map((e) => e.itemId),
+ ...ancestors,
+ ];
+ if (keepItemIds.length > 0) {
+ const deleteSql = buildDeletePermissionsNotInPayloadSql(
+ table,
+ resolvedGroupId,
+ keepItemIds,
+ );
+ if (deleteSql) {
+ await tx.$executeRaw(deleteSql);
+ }
+ } else {
+ // Empty desired state → caller is clearing every override.
+ await deleteAllPermissionsForGroup(tx, table, resolvedGroupId);
+ }
+ }
+
+ return { groupId: resolvedGroupId, ancestorIds: Array.from(ancestors) };
+ },
+ // Prisma's default interactive-transaction timeout is 5s, which a
+ // 1000-entry payload can exceed under load — align with the routes'
+ // maxDuration budget instead of failing with P2028.
+ { maxWait: 10_000, timeout: 120_000 },
+ );
+
+ const touchedItemIds = Array.from(
+ new Set([...dedupedEntries.map((e) => e.itemId), ...ancestorIds]),
+ );
+
+ const rows = await fetchEffectivePermissionRows(
+ table,
+ groupId,
+ touchedItemIds,
+ );
+
+ return { groupId, touchedItemIds, rows };
+}
+
+async function deleteAllPermissionsForGroup(
+ tx: Prisma.TransactionClient,
+ table: AccessControlTable,
+ groupId: string,
+): Promise<void> {
+ switch (table) {
+ case "ViewerGroupAccessControls":
+ await tx.viewerGroupAccessControls.deleteMany({ where: { groupId } });
+ return;
+ case "PermissionGroupAccessControls":
+ await tx.permissionGroupAccessControls.deleteMany({ where: { groupId } });
+ return;
+ default: {
+ const _exhaustive: never = table;
+ throw new Error(`Unhandled access-control table: ${_exhaustive}`);
+ }
+ }
+}
+
+async function fetchEffectivePermissionRows(
+ table: AccessControlTable,
+ groupId: string,
+ touchedItemIds: string[],
+): Promise<EffectivePermissionRow[]> {
+ switch (table) {
+ case "ViewerGroupAccessControls": {
+ const rows = await prisma.viewerGroupAccessControls.findMany({
+ where: { groupId, itemId: { in: touchedItemIds } },
+ orderBy: { id: "asc" },
+ });
+ return rows.map((r) => ({
+ id: r.id,
+ itemId: r.itemId,
+ itemType: r.itemType,
+ canView: r.canView,
+ canDownload: r.canDownload,
+ canDownloadOriginal: false,
+ updatedAt: r.updatedAt,
+ }));
+ }
+ case "PermissionGroupAccessControls": {
+ const rows = await prisma.permissionGroupAccessControls.findMany({
+ where: { groupId, itemId: { in: touchedItemIds } },
+ orderBy: { id: "asc" },
+ });
+ return rows.map((r) => ({
+ id: r.id,
+ itemId: r.itemId,
+ itemType: r.itemType,
+ canView: r.canView,
+ canDownload: r.canDownload,
+ canDownloadOriginal: r.canDownloadOriginal,
+ updatedAt: r.updatedAt,
+ }));
+ }
+ default: {
+ const _exhaustive: never = table;
+ throw new Error(`Unhandled access-control table: ${_exhaustive}`);
+ }
+ }
+}
diff --git a/lib/utils/email-domain.ts b/lib/utils/email-domain.ts
index e6e81cd3..14e067c9 100644
--- a/lib/utils/email-domain.ts
+++ b/lib/utils/email-domain.ts
@@ -71,6 +71,31 @@ export function extractEmailDomain(email: string): string | null {
return domain;
}
+const GROUP_DOMAIN_REGEX =
+ /^([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$/;
+
+/**
+ * Normalize a viewer-group domain audience entry to the canonical stored and
+ * runtime format: a lowercased, "@"-prefixed domain such as "@acme.com".
+ *
+ * Membership at view time compares stored domains against extractEmailDomain(),
+ * which yields "@acme.com", so every stored/accepted domain must carry the "@"
+ * prefix. Accepts input with or without the prefix (the public API documents
+ * bare domains like "acme.com"; the dashboard uses "@acme.com"). Returns null
+ * when the value is not a valid domain.
+ */
+export function normalizeGroupDomain(input: string): string | null {
+ if (!input || typeof input !== "string") {
+ return null;
+ }
+ const trimmed = input.trim().toLowerCase();
+ const bare = trimmed.startsWith("@") ? trimmed.slice(1) : trimmed;
+ if (!GROUP_DOMAIN_REGEX.test(bare)) {
+ return null;
+ }
+ return `@${bare}`;
+}
+
export function normalizeListEntry(entry: string): string {
if (!entry || typeof entry !== "string") {
return "";