Skip to content

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

fix

Marc Seitzde2bc14de607
diff --git a/components/links/link-sheet/index.tsx b/components/links/link-sheet/index.tsx
index 49c0f1f9..2a7c571a 100644
--- a/components/links/link-sheet/index.tsx
+++ b/components/links/link-sheet/index.tsx
@@ -89,7 +89,7 @@ export const DEFAULT_LINK_PROPS = (
uploadFolderId: null,
uploadFolderName: "Home",
enableIndexFile: false,
- permissions: () => ({}), // lazy factory
+ permissions: {},
permissionGroupId: null,
});
@@ -131,7 +131,7 @@ export type DEFAULT_LINK_TYPE = {
uploadFolderId: string | null;
uploadFolderName: string;
enableIndexFile: boolean;
- permissions?: any; // For dataroom links file permissions
+ permissions?: Record<string, unknown> | null; // For dataroom links file permissions
permissionGroupId?: string | null;
};
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/permission-groups/[permissionGroupId].ts b/pages/api/teams/[teamId]/datarooms/[id]/permission-groups/[permissionGroupId].ts
index 57cf0959..1e0fb387 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/permission-groups/[permissionGroupId].ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/permission-groups/[permissionGroupId].ts
@@ -1,12 +1,23 @@
import { NextApiRequest, NextApiResponse } from "next";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
+import { ItemType } from "@prisma/client";
import { getServerSession } from "next-auth/next";
+import { z } from "zod";
import { errorhandler } from "@/lib/errorHandler";
import prisma from "@/lib/prisma";
import { CustomUser } from "@/lib/types";
+// Zod schema for validating permissions
+const itemPermissionSchema = z.object({
+ view: z.boolean(),
+ download: z.boolean(),
+ itemType: z.nativeEnum(ItemType),
+});
+
+const permissionsSchema = z.record(z.string(), itemPermissionSchema);
+
export default async function handle(
req: NextApiRequest,
res: NextApiResponse,
@@ -135,132 +146,143 @@ export default async function handle(
return res.status(404).json({ error: "Permission group not found" });
}
- // Update permission group access controls using a transaction
- const updatedPermissionGroup = await prisma.$transaction(async (tx) => {
- if (permissions && typeof permissions === "object") {
- // Get existing access controls to determine what needs to be updated vs created
- const existingControls =
- await tx.permissionGroupAccessControls.findMany({
- where: {
- groupId: permissionGroupId,
- },
- select: {
- itemId: true,
- itemType: true,
- canView: true,
- canDownload: true,
- canDownloadOriginal: true,
- },
- });
+ // Validate permissions payload using Zod
+ if (!permissions) {
+ return res.status(400).json({ error: "Permissions are required" });
+ }
- const existingMap = new Map(
- existingControls.map((control) => [
- `${control.itemId}-${control.itemType}`,
- control,
- ]),
- );
+ // Validate schema structure
+ const validationResult = permissionsSchema.safeParse(permissions);
+ if (!validationResult.success) {
+ return res.status(400).json({
+ error: "Invalid permissions format",
+ details: validationResult.error.issues,
+ });
+ }
- const toUpdate: Array<{
- itemId: string;
- itemType: any;
- canView: boolean;
- canDownload: boolean;
- canDownloadOriginal: boolean;
- }> = [];
-
- const toCreate: Array<{
- groupId: string;
- itemId: string;
- itemType: any;
- canView: boolean;
- canDownload: boolean;
- canDownloadOriginal: boolean;
- }> = [];
-
- // Categorize permissions into updates vs creates
- Object.entries(permissions).forEach(
- ([itemId, permission]: [string, any]) => {
- const key = `${itemId}-${permission.itemType}`;
- const existing = existingMap.get(key);
-
- const permissionData = {
- itemId,
- itemType: permission.itemType,
- canView: permission.view,
- canDownload: permission.download,
- canDownloadOriginal: false,
- };
-
- if (existing) {
- // Check if anything actually changed
- if (
- existing.canView !== permission.view ||
- existing.canDownload !== permission.download
- ) {
- toUpdate.push(permissionData);
- }
- } else {
- toCreate.push({
- groupId: permissionGroupId,
- ...permissionData,
- });
- }
- },
- );
+ const validatedPermissions = validationResult.data;
- // Perform updates
- if (toUpdate.length > 0) {
- await Promise.all(
- toUpdate.map((item) =>
- tx.permissionGroupAccessControls.updateMany({
- where: {
- groupId: permissionGroupId,
- itemId: item.itemId,
- itemType: item.itemType,
- },
- data: {
- canView: item.canView,
- canDownload: item.canDownload,
- canDownloadOriginal: item.canDownloadOriginal,
- },
- }),
- ),
- );
- }
-
- // Perform creates
- if (toCreate.length > 0) {
- await tx.permissionGroupAccessControls.createMany({
- data: toCreate,
+ // Update permission group access controls using a transaction
+ const updatedPermissionGroup = await prisma.$transaction(async (tx) => {
+ // Get existing access controls to determine what needs to be updated vs created
+ const existingControls =
+ await tx.permissionGroupAccessControls.findMany({
+ where: {
+ groupId: permissionGroupId,
+ },
+ select: {
+ itemId: true,
+ itemType: true,
+ canView: true,
+ canDownload: true,
+ canDownloadOriginal: true,
+ },
+ });
+
+ const existingMap = new Map(
+ existingControls.map((control) => [
+ `${control.itemId}-${control.itemType}`,
+ control,
+ ]),
+ );
+
+ const toUpdate: Array<{
+ itemId: string;
+ itemType: any;
+ canView: boolean;
+ canDownload: boolean;
+ canDownloadOriginal: boolean;
+ }> = [];
+
+ const toCreate: Array<{
+ groupId: string;
+ itemId: string;
+ itemType: any;
+ canView: boolean;
+ canDownload: boolean;
+ canDownloadOriginal: boolean;
+ }> = [];
+
+ // Categorize permissions into updates vs creates
+ Object.entries(validatedPermissions).forEach(([itemId, permission]) => {
+ const key = `${itemId}-${permission.itemType}`;
+ const existing = existingMap.get(key);
+
+ const permissionData = {
+ itemId,
+ itemType: permission.itemType,
+ canView: permission.view,
+ canDownload: permission.download,
+ canDownloadOriginal: false,
+ };
+
+ if (existing) {
+ // Check if anything actually changed
+ if (
+ existing.canView !== permission.view ||
+ existing.canDownload !== permission.download
+ ) {
+ toUpdate.push(permissionData);
+ }
+ } else {
+ toCreate.push({
+ groupId: permissionGroupId,
+ ...permissionData,
});
}
+ });
- // Remove permissions that are no longer in the new set
- const newItemKeys = new Set(
- Object.entries(permissions).map(
- ([itemId, permission]: [string, any]) =>
- `${itemId}-${permission.itemType}`,
+ // Perform updates
+ if (toUpdate.length > 0) {
+ await Promise.all(
+ toUpdate.map((item) =>
+ tx.permissionGroupAccessControls.updateMany({
+ where: {
+ groupId: permissionGroupId,
+ itemId: item.itemId,
+ itemType: item.itemType,
+ },
+ data: {
+ canView: item.canView,
+ canDownload: item.canDownload,
+ canDownloadOriginal: item.canDownloadOriginal,
+ },
+ }),
),
);
+ }
- const toDelete = existingControls.filter(
- (control) =>
- !newItemKeys.has(`${control.itemId}-${control.itemType}`),
- );
+ // Perform creates
+ if (toCreate.length > 0) {
+ await tx.permissionGroupAccessControls.createMany({
+ data: toCreate,
+ });
+ }
- if (toDelete.length > 0) {
- await Promise.all(
- toDelete.map((item) =>
- tx.permissionGroupAccessControls.deleteMany({
- where: {
- groupId: permissionGroupId,
- itemId: item.itemId,
- itemType: item.itemType,
- },
- }),
- ),
- );
- }
+ // Remove permissions that are no longer in the new set
+ const newItemKeys = new Set(
+ Object.entries(validatedPermissions).map(
+ ([itemId, permission]) => `${itemId}-${permission.itemType}`,
+ ),
+ );
+
+ const toDelete = existingControls.filter(
+ (control) =>
+ !newItemKeys.has(`${control.itemId}-${control.itemType}`),
+ );
+
+ if (toDelete.length > 0) {
+ await Promise.all(
+ toDelete.map((item) =>
+ tx.permissionGroupAccessControls.deleteMany({
+ where: {
+ groupId: permissionGroupId,
+ itemId: item.itemId,
+ itemType: item.itemType,
+ },
+ }),
+ ),
+ );
}
// Return the updated permission group with access controls
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/permission-groups/index.ts b/pages/api/teams/[teamId]/datarooms/[id]/permission-groups/index.ts
index 9ba34815..8491630c 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/permission-groups/index.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/permission-groups/index.ts
@@ -67,26 +67,36 @@ export default async function handle(
},
});
- // Create access controls for each permission
- const accessControls = [];
- for (const [itemId, permission] of Object.entries(permissions)) {
- const perm = permission as {
- view: boolean;
- download: boolean;
- itemType: ItemType;
- };
- const accessControl = await tx.permissionGroupAccessControls.create({
- data: {
+ // Prepare access control data for batch insert
+ const accessControlData = Object.entries(permissions).map(
+ ([itemId, permission]) => {
+ const perm = permission as {
+ view: boolean;
+ download: boolean;
+ itemType: ItemType;
+ };
+ return {
groupId: permissionGroup.id,
itemId: itemId,
itemType: perm.itemType,
canView: perm.view,
canDownload: perm.download,
canDownloadOriginal: false,
- },
- });
- accessControls.push(accessControl);
- }
+ };
+ },
+ );
+
+ // Create all access controls in a single batch operation
+ await tx.permissionGroupAccessControls.createMany({
+ data: accessControlData,
+ });
+
+ // Fetch the created access controls for return data
+ const accessControls = await tx.permissionGroupAccessControls.findMany({
+ where: {
+ groupId: permissionGroup.id,
+ },
+ });
// Update the link with the permission group
if (linkId) {