From 13797209f7427ebad5050f753b5e3eae927ef043 Mon Sep 17 00:00:00 2001 From: Florian Trayon <26360935+FlorianLeChat@users.noreply.github.com> Date: Wed, 10 Jan 2024 17:41:36 +0100 Subject: [PATCH] Added user quota check bypass for uploads by administrators --- app/[locale]/dashboard/actions.ts | 12 ++++++---- .../dashboard/components/data-table.tsx | 7 ++++-- .../dashboard/components/file-upload.tsx | 23 +++++++++++++------ schemas/file-upload.ts | 7 +----- 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/app/[locale]/dashboard/actions.ts b/app/[locale]/dashboard/actions.ts index af2fa93..e8e7cea 100644 --- a/app/[locale]/dashboard/actions.ts +++ b/app/[locale]/dashboard/actions.ts @@ -225,12 +225,16 @@ export async function uploadFiles( // On filtre la liste des fichiers à téléverser pour ne garder que // ceux qui ne dépassent pas le quota de l'utilisateur. - result.data.upload = result.data.upload.filter( ( file ) => + // Note : cela ne concerne pas les administrateurs. + if ( session.user.role !== "admin" ) { - currentQuota += file.size; + result.data.upload = result.data.upload.filter( ( file ) => + { + currentQuota += file.size; - return currentQuota <= maxQuota; - } ); + return currentQuota <= maxQuota; + } ); + } // On téléverse chaque fichier dans le système de fichiers. const data = result.data.upload.map( async ( file, index ) => diff --git a/app/[locale]/dashboard/components/data-table.tsx b/app/[locale]/dashboard/components/data-table.tsx index d2b3e0f..e031745 100644 --- a/app/[locale]/dashboard/components/data-table.tsx +++ b/app/[locale]/dashboard/components/data-table.tsx @@ -7,6 +7,7 @@ import { X } from "lucide-react"; import { useState } from "react"; +import { SessionProvider } from "next-auth/react"; import { flexRender, SortingState, useReactTable, @@ -72,7 +73,9 @@ export default function DataTable( { data }: { data: FileAttributes[] } ) // Affichage du rendu HTML du composant. return ( - <> + {/* Filtrage et tri des données */}
{/* Filtrage par nom */} @@ -164,6 +167,6 @@ export default function DataTable( { data }: { data: FileAttributes[] } ) - + ); } \ No newline at end of file diff --git a/app/[locale]/dashboard/components/file-upload.tsx b/app/[locale]/dashboard/components/file-upload.tsx index 407a5a9..d3e52df 100644 --- a/app/[locale]/dashboard/components/file-upload.tsx +++ b/app/[locale]/dashboard/components/file-upload.tsx @@ -7,6 +7,7 @@ import { merge } from "@/utilities/tailwind"; import { useForm } from "react-hook-form"; import serverAction from "@/utilities/recaptcha"; +import { useSession } from "next-auth/react"; import type { Table } from "@tanstack/react-table"; import { formatSize } from "@/utilities/react-table"; import { useFormState } from "react-dom"; @@ -42,6 +43,7 @@ export default function FileUpload( { { // Déclaration des constantes. const files = table.options.meta?.files ?? []; + const session = useSession(); const maxQuota = Number( process.env.NEXT_PUBLIC_MAX_QUOTA ?? 0 ); const setFiles = useMemo( () => table.options.meta?.setFiles ?? ( () => @@ -214,14 +216,21 @@ export default function FileUpload( { /> - + {session.data?.user?.role !== "admin" && ( + <> + - - {percent.toLocaleString()}% du quota - actuellement utilisés ( - {formatSize( quota )} /{" "} - {formatSize( maxQuota )}) - + + {percent.toLocaleString()}% du + quota actuellement utilisés ( + {formatSize( quota )} /{" "} + {formatSize( maxQuota )}) + + + )} diff --git a/schemas/file-upload.ts b/schemas/file-upload.ts index 9c19f11..24f96e4 100644 --- a/schemas/file-upload.ts +++ b/schemas/file-upload.ts @@ -3,9 +3,6 @@ // import { z } from "zod"; -// Taille maximale d'un fichier. -const MAX_FILE_SIZE = Number( process.env.NEXT_PUBLIC_MAX_QUOTA ?? "0" ); - // Types de fichiers acceptés. const ACCEPTED_FILE_TYPES = process.env.NEXT_PUBLIC_ACCEPTED_FILE_TYPES?.split( "," ) ?? []; @@ -20,9 +17,7 @@ const schema = z.object( { "wrong_file_object" ) .refine( - ( files ) => files.every( - ( file ) => file.size > 0 && file.size <= MAX_FILE_SIZE - ), + ( files ) => files.every( ( file ) => file.size > 0 ), "wrong_file_size" ) .refine(