Skip to content

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

feat: add permissions for admin/manager to delete docs

Marc Seitzb99ca4ba657d
diff --git a/components/datarooms/actions/remove-document-modal.tsx b/components/datarooms/actions/remove-document-modal.tsx
index 06a25977..404ec06f 100644
--- a/components/datarooms/actions/remove-document-modal.tsx
+++ b/components/datarooms/actions/remove-document-modal.tsx
@@ -60,9 +60,7 @@ function RemoveDataroomItemsModal({
).then(async (res) => {
if (!res.ok) {
const error = await res.json();
- throw new Error(
- `Failed to remove dataroom document ${documentId}: ${error.message}`,
- );
+ throw new Error(error.message || "Failed to remove dataroom document");
}
analytics.capture("Dataroom Document Removed", {
team: teamInfo?.currentTeam?.id,
@@ -78,9 +76,7 @@ function RemoveDataroomItemsModal({
).then(async (res) => {
if (!res.ok) {
const error = await res.json();
- throw new Error(
- `Failed to remove folderId folder ${folderId}: ${error.message}`,
- );
+ throw new Error(error.message || "Failed to remove dataroom folder");
}
analytics.capture("Dataroom folder Removed", {
team: teamInfo?.currentTeam?.id,
diff --git a/components/datarooms/dataroom-document-card.tsx b/components/datarooms/dataroom-document-card.tsx
index 0d07b782..cf7f7642 100644
--- a/components/datarooms/dataroom-document-card.tsx
+++ b/components/datarooms/dataroom-document-card.tsx
@@ -137,7 +137,11 @@ export default function DataroomDocumentCard({
{
method: "DELETE",
},
- ).then(() => {
+ ).then(async (res) => {
+ if (!res.ok) {
+ const error = await res.json();
+ throw new Error(error.message || "Failed to remove document");
+ }
mutate(
`/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}${endpoint}`,
null,
@@ -155,7 +159,7 @@ export default function DataroomDocumentCard({
{
loading: "Removing document...",
success: "Document removed successfully.",
- error: "Failed to remove document. Try again.",
+ error: (err) => err.message || "Failed to remove document. Try again.",
},
);
};
diff --git a/components/documents/actions/delete-documents-modal.tsx b/components/documents/actions/delete-documents-modal.tsx
index 4872aaa8..c99fbae6 100644
--- a/components/documents/actions/delete-documents-modal.tsx
+++ b/components/documents/actions/delete-documents-modal.tsx
@@ -12,13 +12,13 @@ import { useTeam } from "@/context/team-context";
import { toast } from "sonner";
import { mutate } from "swr";
+import { useAnalytics } from "@/lib/analytics";
+
import { Button } from "@/components/ui/button";
import { DialogDescription, DialogTitle } from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Modal } from "@/components/ui/modal";
-import { useAnalytics } from "@/lib/analytics";
-
function DeleteItemsModal({
showDeleteItemsModal,
setShowDeleteItemsModal,
@@ -75,9 +75,7 @@ function DeleteItemsModal({
).then(async (res) => {
if (!res.ok) {
const error = await res.json();
- throw new Error(
- `Failed to delete document ${documentId}: ${error.message}`,
- );
+ throw new Error(error.message || "Failed to delete document");
}
analytics.capture("Document Deleted", {
team: teamInfo?.currentTeam?.id,
@@ -96,9 +94,7 @@ function DeleteItemsModal({
).then(async (res) => {
if (!res.ok) {
const error = await res.json();
- throw new Error(
- `Failed to delete folder ${folderId}: ${error.message}`,
- );
+ throw new Error(error.message || "Failed to delete folder");
}
analytics.capture("Folder Deleted", {
team: teamInfo?.currentTeam?.id,
@@ -319,4 +315,4 @@ export function useDeleteDocumentsAndFoldersModal({
}),
[setShowDeleteItemsModal, DeleteItemsModalCallback],
);
-}
\ No newline at end of file
+}
diff --git a/components/documents/actions/delete-folder-modal.tsx b/components/documents/actions/delete-folder-modal.tsx
index 160b30fd..d6e12c1f 100644
--- a/components/documents/actions/delete-folder-modal.tsx
+++ b/components/documents/actions/delete-folder-modal.tsx
@@ -37,7 +37,13 @@ export function useDeleteFolderModal(
{
method: "DELETE",
},
- ),
+ ).then(async (res) => {
+ if (!res.ok) {
+ const error = await res.json();
+ throw new Error(error.message || "Failed to delete folder");
+ }
+ return res;
+ }),
{
loading: isDataroom ? "Removing folder..." : "Deleting folder...",
success: () => {
@@ -54,9 +60,11 @@ export function useDeleteFolderModal(
? "Folder removed successfully."
: `Folder deleted successfully with ${folderToDelete?._count.documents} documents and ${folderToDelete?._count.childFolders} folders`;
},
- error: isDataroom
- ? "Failed to remove folder."
- : "Failed to delete folder. Move documents first.",
+ error: (err) =>
+ err.message ||
+ (isDataroom
+ ? "Failed to remove folder."
+ : "Failed to delete folder. Move documents first."),
},
);
};
diff --git a/components/documents/document-card.tsx b/components/documents/document-card.tsx
index 91217828..39d69238 100644
--- a/components/documents/document-card.tsx
+++ b/components/documents/document-card.tsx
@@ -152,7 +152,11 @@ export default function DocumentsCard({
toast.promise(
fetch(`/api/teams/${teamInfo?.currentTeam?.id}/documents/${documentId}`, {
method: "DELETE",
- }).then(() => {
+ }).then(async (res) => {
+ if (!res.ok) {
+ const error = await res.json();
+ throw new Error(error.message || "Failed to delete document");
+ }
mutate(
`/api/teams/${teamInfo?.currentTeam?.id}${endpoint}`,
(currentData: any) => {
@@ -182,7 +186,7 @@ export default function DocumentsCard({
{
loading: "Deleting document...",
success: "Document deleted successfully.",
- error: "Failed to delete document. Try again.",
+ error: (err) => err.message || "Failed to delete document. Try again.",
},
);
};
diff --git a/components/documents/document-header.tsx b/components/documents/document-header.tsx
index 984e6860..b2df58fd 100644
--- a/components/documents/document-header.tsx
+++ b/components/documents/document-header.tsx
@@ -406,7 +406,11 @@ export default function DocumentHeader({
toast.promise(
fetch(`/api/teams/${teamId}/documents/${documentId}`, {
method: "DELETE",
- }).then(() => {
+ }).then(async (res) => {
+ if (!res.ok) {
+ const error = await res.json();
+ throw new Error(error.message || "Failed to delete document");
+ }
mutate(`/api/teams/${teamInfo?.currentTeam?.id}/documents`, null, {
populateCache: (_, docs) => {
return docs.filter(
@@ -423,7 +427,7 @@ export default function DocumentHeader({
{
loading: "Deleting document...",
success: "Document deleted successfully.",
- error: "Failed to delete document. Try again.",
+ error: (err) => err.message || "Failed to delete document. Try again.",
},
);
};
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/documents/[documentId]/index.ts b/pages/api/teams/[teamId]/datarooms/[id]/documents/[documentId]/index.ts
index 8432eaa0..c59e9337 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/documents/[documentId]/index.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/documents/[documentId]/index.ts
@@ -88,25 +88,32 @@ export default async function handle(
} = req.query as { teamId: string; id: string; documentId: string };
try {
- const team = await prisma.team.findUnique({
+ const teamAccess = await prisma.userTeam.findUnique({
where: {
- id: teamId,
- users: {
- some: {
- userId: userId,
- },
+ userId_teamId: {
+ userId: userId,
+ teamId: teamId,
},
},
+ select: {
+ role: true,
+ },
});
-
- if (!team) {
+ if (!teamAccess) {
return res.status(401).end("Unauthorized");
}
+ if (teamAccess.role !== "ADMIN" && teamAccess.role !== "MANAGER") {
+ return res.status(403).json({
+ message:
+ "You are not permitted to perform this action. Only admin and managers can delete dataroom documents.",
+ });
+ }
+
const dataroom = await prisma.dataroom.findUnique({
where: {
id: dataroomId,
- teamId: team.id,
+ teamId,
},
});
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/folders/manage/[folderId]/index.ts b/pages/api/teams/[teamId]/datarooms/[id]/folders/manage/[folderId]/index.ts
index 886f5970..79742496 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/folders/manage/[folderId]/index.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/folders/manage/[folderId]/index.ts
@@ -33,25 +33,33 @@ export default async function handle(
const userId = (session.user as CustomUser).id;
try {
- const team = await prisma.team.findUnique({
+ const teamAccess = await prisma.userTeam.findUnique({
where: {
- id: teamId,
- users: {
- some: {
- userId: userId,
- },
+ userId_teamId: {
+ userId: userId,
+ teamId: teamId,
},
},
+ select: {
+ role: true,
+ },
});
- if (!team) {
+ if (!teamAccess) {
return res.status(401).end("Unauthorized");
}
+ if (teamAccess.role !== "ADMIN" && teamAccess.role !== "MANAGER") {
+ return res.status(403).json({
+ message:
+ "You are not permitted to perform this action. Only admin and managers can delete dataroom folders.",
+ });
+ }
+
const dataroom = await prisma.dataroom.findUnique({
where: {
id: dataroomId,
- teamId: team.id,
+ teamId,
},
});
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/index.ts b/pages/api/teams/[teamId]/datarooms/[id]/index.ts
index 6eaff9ce..eab83bc1 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/index.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/index.ts
@@ -159,36 +159,30 @@ export default async function handle(
return res.status(401).end("Unauthorized");
}
+ const userId = (session.user as CustomUser).id;
const { teamId, id: dataroomId } = req.query as {
teamId: string;
id: string;
};
try {
- // Check if the user is part of the team
- const team = await prisma.team.findUnique({
+ const teamAccess = await prisma.userTeam.findUnique({
where: {
- id: teamId,
- datarooms: {
- some: {
- id: dataroomId,
- },
+ userId_teamId: {
+ userId: userId,
+ teamId: teamId,
},
},
- include: { users: true },
+ select: {
+ role: true,
+ },
});
- if (!team) {
+ if (!teamAccess) {
return res.status(401).end("Unauthorized");
}
- // check if current user is admin of the team
- const isUserAdmin = team.users.some(
- (user) =>
- (user.role === "ADMIN" || user.role === "MANAGER") &&
- user.userId === (session.user as CustomUser).id,
- );
- if (!isUserAdmin) {
+ if (teamAccess.role !== "ADMIN" && teamAccess.role !== "MANAGER") {
return res.status(403).json({
message:
"You are not permitted to perform this action. Only admin and managers can delete datarooms.",
diff --git a/pages/api/teams/[teamId]/documents/[id]/index.ts b/pages/api/teams/[teamId]/documents/[id]/index.ts
index 1315f17d..2799c178 100644
--- a/pages/api/teams/[teamId]/documents/[id]/index.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/index.ts
@@ -196,18 +196,32 @@ export default async function handle(
const userId = (session.user as CustomUser).id;
try {
+ const teamAccess = await prisma.userTeam.findUnique({
+ where: {
+ userId_teamId: {
+ userId: userId,
+ teamId: teamId,
+ },
+ },
+ select: {
+ role: true,
+ },
+ });
+ if (!teamAccess) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ if (teamAccess.role !== "ADMIN" && teamAccess.role !== "MANAGER") {
+ return res.status(403).json({
+ message:
+ "You are not permitted to perform this action. Only admin and managers can delete documents.",
+ });
+ }
+
const documentVersions = await prisma.document.findUnique({
where: {
id: docId,
teamId: teamId,
- team: {
- users: {
- some: {
- // role: { in: ["ADMIN", "MANAGER"] },
- userId: userId,
- },
- },
- },
},
include: {
versions: {
diff --git a/pages/api/teams/[teamId]/folders/manage/[folderId]/index.ts b/pages/api/teams/[teamId]/folders/manage/[folderId]/index.ts
index b0c1cec5..9a28cf07 100644
--- a/pages/api/teams/[teamId]/folders/manage/[folderId]/index.ts
+++ b/pages/api/teams/[teamId]/folders/manage/[folderId]/index.ts
@@ -28,21 +28,29 @@ export default async function handle(
const userId = (session.user as CustomUser).id;
try {
- const team = await prisma.team.findUnique({
+ const teamAccess = await prisma.userTeam.findUnique({
where: {
- id: teamId,
- users: {
- some: {
- userId: userId,
- },
+ userId_teamId: {
+ userId: userId,
+ teamId: teamId,
},
},
+ select: {
+ role: true,
+ },
});
- if (!team) {
+ if (!teamAccess) {
return res.status(401).end("Unauthorized");
}
+ if (teamAccess.role !== "ADMIN" && teamAccess.role !== "MANAGER") {
+ return res.status(403).json({
+ message:
+ "You are not permitted to perform this action. Only admin and managers can delete folders.",
+ });
+ }
+
const folder = await prisma.folder.findUnique({
where: {
id: folderId,