fixed inconsitencies with NotFoundError

This commit is contained in:
Laurin
2023-01-02 18:56:12 +01:00
committed by La_Felx
parent c190e319a0
commit cf20f11ec1
8 changed files with 42 additions and 61 deletions
+5 -18
View File
@@ -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 })) {
+9 -21
View File
@@ -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
+4
View File
@@ -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: {
+1 -1
View File
@@ -96,7 +96,7 @@ export const getGroup = async (req: Request<GetGroupQueryParams>, 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",
+4 -12
View File
@@ -91,12 +91,7 @@ export const getOrganisation = async (req: Request<GetOrganisationQueryParams>,
});
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<DeleteOrganisationQueryPar
res.status(204).end();
} catch (e) {
if (e instanceof PrismaClientKnownRequestError) {
if ((e.code = "P2025")) {
return res.status(404).json(generateError(`The organisation with the ID ${pid} could not be found`));
}
} else if (e instanceof PrismaClientUnknownRequestError) {
return res.status(400).send(generateError("Unkonwn error occured. This could be due to malformed IDs"));
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
throw new NotFoundError("organisation", pid);
}
throw e;
}
return res.status(500).json(genericError);
+12 -5
View File
@@ -137,8 +137,17 @@ export const createParticipant = async (req: Request<{ teamPid: string }>, 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;
}
+1 -1
View File
@@ -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();
+6 -3
View File
@@ -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);
}
};