members with edit permission can now refresh preservation as well + bug fix

This commit is contained in:
daniel31x13 2024-09-12 15:30:15 -04:00
parent 8bb6e32bfa
commit aaf3590542
5 changed files with 58 additions and 36 deletions

View File

@ -440,7 +440,7 @@ export default function LinkDetails({
{mode === "view" ? ( {mode === "view" ? (
<div className="flex gap-2 flex-wrap rounded-md p-2 bg-base-200 border border-base-200 w-full text-xs"> <div className="flex gap-2 flex-wrap rounded-md p-2 bg-base-200 border border-base-200 w-full text-xs">
{link.tags[0] ? ( {link.tags && link.tags[0] ? (
link.tags.map((tag) => link.tags.map((tag) =>
isPublicRoute ? ( isPublicRoute ? (
<div <div

View File

@ -127,21 +127,22 @@ export default function LinkActions({ link, btnStyle }: Props) {
</div> </div>
</li> </li>
)} )}
{link.type === "url" && permissions === true && ( {link.type === "url" &&
<li> (permissions === true || permissions?.canUpdate) && (
<div <li>
role="button" <div
tabIndex={0} role="button"
onClick={() => { tabIndex={0}
(document?.activeElement as HTMLElement)?.blur(); onClick={() => {
updateArchive(); (document?.activeElement as HTMLElement)?.blur();
}} updateArchive();
className="whitespace-nowrap" }}
> className="whitespace-nowrap"
{t("refresh_preserved_formats")} >
</div> {t("refresh_preserved_formats")}
</li> </div>
)} </li>
)}
{(permissions === true || permissions?.canDelete) && ( {(permissions === true || permissions?.canDelete) && (
<li> <li>
<div <div

View File

@ -108,21 +108,22 @@ export default function LinkModal({
</div> </div>
</li> </li>
} }
{link.type === "url" && permissions === true && ( {link.type === "url" &&
<li> (permissions === true || permissions?.canUpdate) && (
<div <li>
role="button" <div
tabIndex={0} role="button"
onClick={() => { tabIndex={0}
(document?.activeElement as HTMLElement)?.blur(); onClick={() => {
onUpdateArchive(); (document?.activeElement as HTMLElement)?.blur();
}} onUpdateArchive();
className="whitespace-nowrap" }}
> className="whitespace-nowrap"
{t("refresh_preserved_formats")} >
</div> {t("refresh_preserved_formats")}
</li> </div>
)} </li>
)}
{(permissions === true || permissions?.canDelete) && ( {(permissions === true || permissions?.canDelete) && (
<li> <li>
<div <div

View File

@ -96,9 +96,18 @@ export default async function updateLinkById(
}, },
}); });
if (oldLink && oldLink?.url !== data.url) { if (
data.url &&
oldLink &&
oldLink?.url !== data.url &&
isValidUrl(data.url)
) {
await removeFiles(oldLink.id, oldLink.collectionId); await removeFiles(oldLink.id, oldLink.collectionId);
} } else
return {
response: "Invalid URL.",
status: 401,
};
const updatedLink = await prisma.link.update({ const updatedLink = await prisma.link.update({
where: { where: {

View File

@ -2,8 +2,10 @@ import type { NextApiRequest, NextApiResponse } from "next";
import { prisma } from "@/lib/api/db"; import { prisma } from "@/lib/api/db";
import verifyUser from "@/lib/api/verifyUser"; import verifyUser from "@/lib/api/verifyUser";
import isValidUrl from "@/lib/shared/isValidUrl"; import isValidUrl from "@/lib/shared/isValidUrl";
import { Collection, Link } from "@prisma/client"; import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
import { removeFiles } from "@/lib/api/manageLinkFiles"; import { UsersAndCollections } from "@prisma/client";
import getPermission from "@/lib/api/getPermission";
import { moveFiles, removeFiles } from "@/lib/api/manageLinkFiles";
const RE_ARCHIVE_LIMIT = Number(process.env.RE_ARCHIVE_LIMIT) || 5; const RE_ARCHIVE_LIMIT = Number(process.env.RE_ARCHIVE_LIMIT) || 5;
@ -23,7 +25,16 @@ export default async function links(req: NextApiRequest, res: NextApiResponse) {
response: "Link not found.", response: "Link not found.",
}); });
if (link.collection.ownerId !== user.id) const collectionIsAccessible = await getPermission({
userId: user.id,
collectionId: link.collectionId,
});
const memberHasAccess = collectionIsAccessible?.members.some(
(e: UsersAndCollections) => e.userId === user.id && e.canUpdate
);
if (!(collectionIsAccessible?.ownerId === user.id || memberHasAccess))
return res.status(401).json({ return res.status(401).json({
response: "Permission denied.", response: "Permission denied.",
}); });