From cf20f11ec18edd5838f65a72580cb574f500ceec Mon Sep 17 00:00:00 2001 From: Laurin <60652077+Flexla54@users.noreply.github.com> Date: Fri, 16 Dec 2022 16:59:28 +0100 Subject: [PATCH] fixed inconsitencies with NotFoundError --- src/Controllers/admin.controller.ts | 23 ++++------------- src/Controllers/common.ts | 30 +++++++--------------- src/Controllers/discipline.controller.ts | 4 +++ src/Controllers/group.controllers.ts | 2 +- src/Controllers/organisation.controller.ts | 16 +++--------- src/Controllers/participant.controller.ts | 17 ++++++++---- src/Controllers/user_auth.controller.ts | 2 +- src/Middleware/auth/defaultAdmin.ts | 9 ++++--- 8 files changed, 42 insertions(+), 61 deletions(-) diff --git a/src/Controllers/admin.controller.ts b/src/Controllers/admin.controller.ts index cca123f..c6270e6 100644 --- a/src/Controllers/admin.controller.ts +++ b/src/Controllers/admin.controller.ts @@ -4,6 +4,8 @@ import { AdminLevel } from "@prisma/client"; import argon2 from "argon2"; import { AUTH_ERROR, createInsufficientPermissionsError, DataType, generateInvalidBodyError } from "./common"; import { authClient } from "../lib/redis"; +import NotFoundError from "../Middleware/error/NotFoundError"; +import { notFoundHandler } from "../Middleware/error/defaultRoutes"; require("express-async-errors"); @@ -83,12 +85,7 @@ export const createAdmin = async (req: Request<{}, {}, CreateAdminBody>, res: Re // Check if all gropus exist for (const groupId of groups || []) { if (!(await prisma.group.findUnique({ where: { pid: groupId } }))) { - return res.status(404).json({ - type: "error", - payload: { - message: `The group with ID '${groupId}' could not be found!`, - }, - }); + throw new NotFoundError("group", groupId); } } @@ -143,12 +140,7 @@ export const updateForeignPassword = async ( if (!user_to_upate) { // REVIEW: This allows potential attackers (which are authorized with some account) // to test account names - return res.status(404).json({ - type: "error", - payload: { - message: "The requested user was not found", - }, - }); + throw new NotFoundError("user", req.params.pid); } if (req.auth.permission_level == "ELEVATED" && user_to_upate.permission_level == "STANDARD") { @@ -190,12 +182,7 @@ export const updateOwnPassword = async (req: Request<{}, {}, UpdatePasswordBody> if (!user_to_upate) { // REVIEW: This allows potential attackers (which are authorized with some account) // to test account names - return res.status(404).json({ - type: "error", - payload: { - message: "The requested user was not found", - }, - }); + throw new NotFoundError("user", pid); } if (await argon2.verify(user_to_upate.password, req.body.password, { type: argon2.argon2id })) { diff --git a/src/Controllers/common.ts b/src/Controllers/common.ts index 0f76edc..eca2b91 100644 --- a/src/Controllers/common.ts +++ b/src/Controllers/common.ts @@ -147,28 +147,16 @@ export async function handleCreateByName( // Check if link object exsits - try { - // @ts-ignore - const linked = await prisma[link.type].findUnique({ where: { pid: link.id }, select: { id: true } }); + // @ts-ignore + const linked = await prisma[link.type].findUnique({ where: { pid: link.id }, select: { id: true } }); - if (!linked) { - return res.status(404).json({ - type: "error", - payload: { - message: `Could not link to ${link.type} with ID '${link.id}'`, - }, - }); - } - } catch (e) { - // REVIEW: Check for valid UUID - if (e instanceof PrismaClientUnknownRequestError) { - return res.status(400).json({ - type: "error", - payload: { - message: "Unknown error occured. This could be due to malformed IDs", - }, - }); - } + if (!linked) { + return res.status(404).json({ + type: "error", + payload: { + message: `Could not link to ${link.type} with ID '${link.id}'`, + }, + }); } // @ts-ignore diff --git a/src/Controllers/discipline.controller.ts b/src/Controllers/discipline.controller.ts index 7caafe0..e9aba9d 100644 --- a/src/Controllers/discipline.controller.ts +++ b/src/Controllers/discipline.controller.ts @@ -64,6 +64,10 @@ export const _getAllDisciplines = async ( select: !authenticated ? basicDiscipline : authenticatedDiscipline, }); + if (!disciplines) { + throw new NotFoundError("event", eventId); + } + return res.status(200).json({ type: "success", payload: { diff --git a/src/Controllers/group.controllers.ts b/src/Controllers/group.controllers.ts index 4437054..226b04c 100644 --- a/src/Controllers/group.controllers.ts +++ b/src/Controllers/group.controllers.ts @@ -96,7 +96,7 @@ export const getGroup = async (req: Request, res: Response) : basicGroup, }); if (!group) { - return res.status(404).json(generateError(`The group with ID '${pid}' could not be found`)); + throw new NotFoundError("group", pid); } return res.status(200).json({ type: "success", diff --git a/src/Controllers/organisation.controller.ts b/src/Controllers/organisation.controller.ts index e79d7a3..6d8abb6 100644 --- a/src/Controllers/organisation.controller.ts +++ b/src/Controllers/organisation.controller.ts @@ -91,12 +91,7 @@ export const getOrganisation = async (req: Request, }); if (!organisation) { - return res.status(404).json({ - type: "error", - payload: { - message: `The organisation with ID '${pid} could not be found'`, - }, - }); + throw new NotFoundError("organisation", pid); } return res.status(200).json({ @@ -219,13 +214,10 @@ export const deleteOrganisation = async (req: Request, res: const maxteamsize = discipline?.discipline.maxTeamSize; - const userCount = await prisma.participant.count({ - where: { team: { pid: teamPid } }, + const roles = await prisma.team.findUnique({ + where: { pid: teamPid }, + select: { roles: true }, + }); + + let userCount: number = 0; + + roles?.roles.forEach((role) => { + if (role.participantId !== null) { + userCount++; + } }); if (maxteamsize == userCount) { @@ -159,9 +168,7 @@ export const createParticipant = async (req: Request<{ teamPid: string }>, res: return res.status(201).json({ type: "success", payload: { participant } }); } catch (e) { if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") { - return res - .status(404) - .json(generateError(`Could not link to team with ID '${teamPid}, or group with ID ${body.groupPid}'`)); + throw new NotFoundError("team", teamPid); } throw e; } diff --git a/src/Controllers/user_auth.controller.ts b/src/Controllers/user_auth.controller.ts index 268e1cc..b4e7e8c 100644 --- a/src/Controllers/user_auth.controller.ts +++ b/src/Controllers/user_auth.controller.ts @@ -117,7 +117,7 @@ export const requestToken = async (req: Request, res: Response) => { }); if (!team) { - return res.status(404).json(generateError("Team does not exist!")); + throw new NotFoundError("team", teamId); } const usid = nanoid(); diff --git a/src/Middleware/auth/defaultAdmin.ts b/src/Middleware/auth/defaultAdmin.ts index 4fc3e39..778ac3c 100644 --- a/src/Middleware/auth/defaultAdmin.ts +++ b/src/Middleware/auth/defaultAdmin.ts @@ -10,7 +10,7 @@ export const generateDefaultCredentials = async () => { const pw: string = crypto.randomBytes(8).toString("base64url"); - await prisma.admin.upsert({ + const defaultAdmin = await prisma.admin.upsert({ where: { id: 1 }, create: { name, @@ -18,8 +18,11 @@ export const generateDefaultCredentials = async () => { permission_level: "ELEVATED", }, update: {}, + select: { name: true }, }); - logger.notice("Default admin name is: " + name); - logger.notice("Confidential password: " + pw); + if (defaultAdmin.name === name) { + logger.notice("Default admin name is: " + name); + logger.notice("Confidential password: " + pw); + } };