Skip to content

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

feat: remove permissions group from link if setting to all

Marc Seitz5bb11448a154
diff --git a/components/links/links-table.tsx b/components/links/links-table.tsx
index 2902fd69..c6cc29f5 100644
--- a/components/links/links-table.tsx
+++ b/components/links/links-table.tsx
@@ -304,6 +304,45 @@ export default function LinksTable({
const handlePermissionsSave = async (permissions: any) => {
if (!editPermissionLink) return;
+ // Handle the case where user wants to share entire dataroom (permissions === null)
+ if (permissions === null && editPermissionLink.permissionGroupId) {
+ // Delete the permission group - database will set permissionGroupId to null automatically
+ try {
+ const deleteResponse = await fetch(
+ `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${targetId}/permission-groups/${editPermissionLink.permissionGroupId}`,
+ {
+ method: "DELETE",
+ },
+ );
+
+ if (!deleteResponse.ok) {
+ const { error } = await deleteResponse.json();
+ throw new Error(error ?? "Failed to delete permission group");
+ }
+
+ // Refresh the links cache
+ const endpointTargetType = `${targetType.toLowerCase()}s`;
+ mutate(
+ `/api/teams/${teamInfo?.currentTeam?.id}/${endpointTargetType}/${encodeURIComponent(
+ targetId,
+ )}/links`,
+ );
+
+ // Invalidate the permission group cache
+ mutate(
+ `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${targetId}/permission-groups/${editPermissionLink.permissionGroupId}`,
+ );
+
+ setShowPermissionsSheet(false);
+ setEditPermissionLink(null);
+ toast.success("File permissions updated successfully");
+ } catch (error) {
+ console.error("Error updating file permissions:", error);
+ toast.error("Failed to update file permissions");
+ }
+ return;
+ }
+
if (!editPermissionLink.permissionGroupId) {
setIsLoading(true);
try {
diff --git a/ee/features/permissions/components/dataroom-link-sheet.tsx b/ee/features/permissions/components/dataroom-link-sheet.tsx
index 6d475245..968e32c3 100644
--- a/ee/features/permissions/components/dataroom-link-sheet.tsx
+++ b/ee/features/permissions/components/dataroom-link-sheet.tsx
@@ -297,11 +297,34 @@ export function DataroomLinkSheet({
const returnedLink = await response.json();
// Handle permissions
- await handlePermissionGroupOperations(
- returnedLink,
- permissions,
- isUpdating,
- );
+ if (permissions === null && isUpdating && currentLink?.permissionGroupId) {
+ // Delete the permission group - database will set permissionGroupId to null automatically
+ await fetch(
+ `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${targetId}/permission-groups/${currentLink.permissionGroupId}`,
+ {
+ method: "DELETE",
+ },
+ );
+
+ // Refresh the links cache
+ mutate(
+ `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${encodeURIComponent(
+ targetId,
+ )}/links`,
+ );
+
+ // Invalidate the permission group cache
+ mutate(
+ `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${targetId}/permission-groups/${currentLink.permissionGroupId}`,
+ );
+ } else if (permissions !== null) {
+ // Only handle permission group operations if we're not deleting
+ await handlePermissionGroupOperations(
+ returnedLink,
+ permissions,
+ isUpdating,
+ );
+ }
// Handle UI updates and notifications
await handlePostSaveOperations(
@@ -318,70 +341,60 @@ export function DataroomLinkSheet({
const handlePermissionGroupOperations = async (
link: any,
- permissions: ItemPermission | null,
+ permissions: ItemPermission,
isUpdating: boolean,
) => {
- // If permissions are null, user wants full dataroom access - no permission group needed
- if (permissions === null) {
- // If updating and link had a permission group, we might want to remove it
- // For now, we'll leave existing permission groups as they are
- return;
- }
+ // Create/update permission group with the provided permissions
+ if (isUpdating && currentLink?.permissionGroupId) {
+ // Update existing permission group
+ const response = await fetch(
+ `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${targetId}/permission-groups/${currentLink.permissionGroupId}`,
+ {
+ method: "PUT",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ permissions: permissions,
+ }),
+ },
+ );
- // If permissions is an empty object, user wants no access - still create/update permission group
- // If permissions has items, create/update permission group with those permissions
- if (permissions && typeof permissions === "object") {
- if (isUpdating && currentLink?.permissionGroupId) {
- // Update existing permission group
- const response = await fetch(
+ if (response.ok) {
+ // Invalidate the permission group cache
+ mutate(
`/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${targetId}/permission-groups/${currentLink.permissionGroupId}`,
- {
- method: "PUT",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- permissions: permissions,
- }),
- },
);
-
- if (response.ok) {
- // Invalidate the permission group cache
- mutate(
- `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${targetId}/permission-groups/${currentLink.permissionGroupId}`,
- );
- }
- } else {
- // Create new permission group
- const response = await fetch(
- `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${targetId}/permission-groups`,
- {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- permissions: permissions,
- linkId: link.id,
- }),
+ }
+ } else {
+ // Create new permission group
+ const response = await fetch(
+ `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${targetId}/permission-groups`,
+ {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
},
- );
+ body: JSON.stringify({
+ permissions: permissions,
+ linkId: link.id,
+ }),
+ },
+ );
- if (response.ok) {
- const { permissionGroup: newPermissionGroup, _ } =
- await response.json();
- // Cache the new permission group data
- mutate(
- `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${targetId}/permission-groups/${newPermissionGroup.id}`,
- newPermissionGroup,
- false,
- );
+ if (response.ok) {
+ const { permissionGroup: newPermissionGroup, _ } =
+ await response.json();
+ // Cache the new permission group data
+ mutate(
+ `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${targetId}/permission-groups/${newPermissionGroup.id}`,
+ newPermissionGroup,
+ false,
+ );
- // Update the link with the new permission group ID
- if (newPermissionGroup.id) {
- link.permissionGroupId = newPermissionGroup.id;
- }
+ // Update the link with the new permission group ID
+ if (newPermissionGroup.id) {
+ link.permissionGroupId = newPermissionGroup.id;
}
}
}
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 1e0fb387..ff08eae5 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/permission-groups/[permissionGroupId].ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/permission-groups/[permissionGroupId].ts
@@ -307,9 +307,69 @@ export default async function handle(
} catch (error) {
errorhandler(error, res);
}
+ } else if (req.method === "DELETE") {
+ // DELETE /api/teams/:teamId/datarooms/:id/permission-groups/:permissionGroupId
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const {
+ teamId,
+ id: dataroomId,
+ permissionGroupId,
+ } = req.query as {
+ teamId: string;
+ id: string;
+ permissionGroupId: string;
+ };
+
+ const userId = (session.user as CustomUser).id;
+
+ try {
+ // Verify team membership
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ users: {
+ some: { userId },
+ },
+ },
+ });
+
+ if (!team) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ // Verify permission group exists and belongs to dataroom
+ const permissionGroup = await prisma.permissionGroup.findUnique({
+ where: {
+ id: permissionGroupId,
+ dataroomId: dataroomId,
+ teamId: teamId,
+ },
+ });
+
+ if (!permissionGroup) {
+ return res.status(404).json({ error: "Permission group not found" });
+ }
+
+ // Delete the permission group (this will cascade delete the access controls)
+ await prisma.permissionGroup.delete({
+ where: {
+ id: permissionGroupId,
+ },
+ });
+
+ return res
+ .status(200)
+ .json({ message: "Permission group deleted successfully" });
+ } catch (error) {
+ errorhandler(error, res);
+ }
}
- // We only allow GET requests
- res.setHeader("Allow", ["GET", "PUT"]);
+ // We only allow GET, PUT, and DELETE requests
+ res.setHeader("Allow", ["GET", "PUT", "DELETE"]);
return res.status(405).end(`Method ${req.method} Not Allowed`);
}