From 88d73703f8a0f4d6e24ac9cdd316d211f350c63c Mon Sep 17 00:00:00 2001 From: daniel31x13 Date: Wed, 14 Feb 2024 08:10:45 -0500 Subject: [PATCH] final touch --- .../InputSelect/CollectionSelection.tsx | 42 +++++++++++++------ .../ModalContent/BulkEditLinksModal.tsx | 4 +- components/ModalContent/EditLinkModal.tsx | 1 + .../links/linkId/updateLinkById.ts | 26 ++++++++++++ lib/api/getPermission.ts | 7 +++- store/links.ts | 5 +++ 6 files changed, 70 insertions(+), 15 deletions(-) diff --git a/components/InputSelect/CollectionSelection.tsx b/components/InputSelect/CollectionSelection.tsx index dcbbe0f..ec586ea 100644 --- a/components/InputSelect/CollectionSelection.tsx +++ b/components/InputSelect/CollectionSelection.tsx @@ -4,6 +4,7 @@ import { useEffect, useState } from "react"; import { styles } from "./styles"; import { Options } from "./types"; import CreatableSelect from "react-select/creatable"; +import Select from "react-select"; type Props = { onChange: any; @@ -14,12 +15,14 @@ type Props = { value?: number; } | undefined; + creatable?: boolean; }; export default function CollectionSelection({ onChange, defaultValue, showDefaultValue = true, + creatable = true, }: Props) { const { collections } = useCollectionStore(); const router = useRouter(); @@ -47,16 +50,31 @@ export default function CollectionSelection({ setOptions(formatedCollections); }, [collections]); - return ( - - ); + if (creatable) { + return ( + + ); + } else { + return ( + ) : null} diff --git a/lib/api/controllers/links/linkId/updateLinkById.ts b/lib/api/controllers/links/linkId/updateLinkById.ts index 6944999..62b2945 100644 --- a/lib/api/controllers/links/linkId/updateLinkById.ts +++ b/lib/api/controllers/links/linkId/updateLinkById.ts @@ -16,6 +16,10 @@ export default async function updateLinkById( }; const collectionIsAccessible = await getPermission({ userId, linkId }); + const targetCollectionIsAccessible = await getPermission({ + userId, + collectionId: data.collection.id, + }); const memberHasAccess = collectionIsAccessible?.members.some( (e: UsersAndCollections) => e.userId === userId && e.canUpdate @@ -25,6 +29,28 @@ export default async function updateLinkById( collectionIsAccessible?.ownerId === data.collection.ownerId && data.collection.ownerId === userId; + const targetCollectionsAccessible = + targetCollectionIsAccessible?.ownerId === userId; + + const targetCollectionMatchesData = data.collection.id + ? data.collection.id === targetCollectionIsAccessible?.id + : true && data.collection.name + ? data.collection.name === targetCollectionIsAccessible?.name + : true && data.collection.ownerId + ? data.collection.ownerId === targetCollectionIsAccessible?.ownerId + : true; + + if (!targetCollectionsAccessible) + return { + response: "Target collection is not accessible.", + status: 401, + }; + else if (!targetCollectionMatchesData) + return { + response: "Target collection does not match the data.", + status: 401, + }; + const unauthorizedSwitchCollection = !isCollectionOwner && collectionIsAccessible?.id !== data.collection.id; diff --git a/lib/api/getPermission.ts b/lib/api/getPermission.ts index 61dc5c5..93dd04c 100644 --- a/lib/api/getPermission.ts +++ b/lib/api/getPermission.ts @@ -3,12 +3,14 @@ import { prisma } from "@/lib/api/db"; type Props = { userId: number; collectionId?: number; + collectionName?: string; linkId?: number; }; export default async function getPermission({ userId, collectionId, + collectionName, linkId, }: Props) { if (linkId) { @@ -24,10 +26,11 @@ export default async function getPermission({ }); return check; - } else if (collectionId) { + } else if (collectionId || collectionName) { const check = await prisma.collection.findFirst({ where: { - id: collectionId, + id: collectionId || undefined, + name: collectionName || undefined, OR: [{ ownerId: userId }, { members: { some: { userId } } }], }, include: { members: true }, diff --git a/store/links.ts b/store/links.ts index 01935e4..408a3ee 100644 --- a/store/links.ts +++ b/store/links.ts @@ -152,6 +152,11 @@ const useLinkStore = create()((set) => ({ links.some((link) => link.id === e.id) ? { ...e, + collectionId: newData.collectionId ?? e.collectionId, + collection: { + ...e.collection, + id: newData.collectionId ?? e.collection.id, + }, tags: removePreviousTags ? [...(newData.tags ?? [])] : [...e.tags, ...(newData.tags ?? [])],