mirror of
https://github.com/detleph/server.git
synced 2026-09-07 07:56:18 +02:00
fixed inconsitencies with NotFoundError
This commit is contained in:
@@ -4,6 +4,8 @@ import { AdminLevel } from "@prisma/client";
|
|||||||
import argon2 from "argon2";
|
import argon2 from "argon2";
|
||||||
import { AUTH_ERROR, createInsufficientPermissionsError, DataType, generateInvalidBodyError } from "./common";
|
import { AUTH_ERROR, createInsufficientPermissionsError, DataType, generateInvalidBodyError } from "./common";
|
||||||
import { authClient } from "../lib/redis";
|
import { authClient } from "../lib/redis";
|
||||||
|
import NotFoundError from "../Middleware/error/NotFoundError";
|
||||||
|
import { notFoundHandler } from "../Middleware/error/defaultRoutes";
|
||||||
|
|
||||||
require("express-async-errors");
|
require("express-async-errors");
|
||||||
|
|
||||||
@@ -83,12 +85,7 @@ export const createAdmin = async (req: Request<{}, {}, CreateAdminBody>, res: Re
|
|||||||
// Check if all gropus exist
|
// Check if all gropus exist
|
||||||
for (const groupId of groups || []) {
|
for (const groupId of groups || []) {
|
||||||
if (!(await prisma.group.findUnique({ where: { pid: groupId } }))) {
|
if (!(await prisma.group.findUnique({ where: { pid: groupId } }))) {
|
||||||
return res.status(404).json({
|
throw new NotFoundError("group", groupId);
|
||||||
type: "error",
|
|
||||||
payload: {
|
|
||||||
message: `The group with ID '${groupId}' could not be found!`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,12 +140,7 @@ export const updateForeignPassword = async (
|
|||||||
if (!user_to_upate) {
|
if (!user_to_upate) {
|
||||||
// REVIEW: This allows potential attackers (which are authorized with some account)
|
// REVIEW: This allows potential attackers (which are authorized with some account)
|
||||||
// to test account names
|
// to test account names
|
||||||
return res.status(404).json({
|
throw new NotFoundError("user", req.params.pid);
|
||||||
type: "error",
|
|
||||||
payload: {
|
|
||||||
message: "The requested user was not found",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.auth.permission_level == "ELEVATED" && user_to_upate.permission_level == "STANDARD") {
|
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) {
|
if (!user_to_upate) {
|
||||||
// REVIEW: This allows potential attackers (which are authorized with some account)
|
// REVIEW: This allows potential attackers (which are authorized with some account)
|
||||||
// to test account names
|
// to test account names
|
||||||
return res.status(404).json({
|
throw new NotFoundError("user", pid);
|
||||||
type: "error",
|
|
||||||
payload: {
|
|
||||||
message: "The requested user was not found",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (await argon2.verify(user_to_upate.password, req.body.password, { type: argon2.argon2id })) {
|
if (await argon2.verify(user_to_upate.password, req.body.password, { type: argon2.argon2id })) {
|
||||||
|
|||||||
@@ -147,28 +147,16 @@ export async function handleCreateByName(
|
|||||||
|
|
||||||
// Check if link object exsits
|
// Check if link object exsits
|
||||||
|
|
||||||
try {
|
// @ts-ignore
|
||||||
// @ts-ignore
|
const linked = await prisma[link.type].findUnique({ where: { pid: link.id }, select: { id: true } });
|
||||||
const linked = await prisma[link.type].findUnique({ where: { pid: link.id }, select: { id: true } });
|
|
||||||
|
|
||||||
if (!linked) {
|
if (!linked) {
|
||||||
return res.status(404).json({
|
return res.status(404).json({
|
||||||
type: "error",
|
type: "error",
|
||||||
payload: {
|
payload: {
|
||||||
message: `Could not link to ${link.type} with ID '${link.id}'`,
|
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",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
|||||||
@@ -64,6 +64,10 @@ export const _getAllDisciplines = async (
|
|||||||
select: !authenticated ? basicDiscipline : authenticatedDiscipline,
|
select: !authenticated ? basicDiscipline : authenticatedDiscipline,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!disciplines) {
|
||||||
|
throw new NotFoundError("event", eventId);
|
||||||
|
}
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
type: "success",
|
type: "success",
|
||||||
payload: {
|
payload: {
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ export const getGroup = async (req: Request<GetGroupQueryParams>, res: Response)
|
|||||||
: basicGroup,
|
: basicGroup,
|
||||||
});
|
});
|
||||||
if (!group) {
|
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({
|
return res.status(200).json({
|
||||||
type: "success",
|
type: "success",
|
||||||
|
|||||||
@@ -91,12 +91,7 @@ export const getOrganisation = async (req: Request<GetOrganisationQueryParams>,
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!organisation) {
|
if (!organisation) {
|
||||||
return res.status(404).json({
|
throw new NotFoundError("organisation", pid);
|
||||||
type: "error",
|
|
||||||
payload: {
|
|
||||||
message: `The organisation with ID '${pid} could not be found'`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
@@ -219,13 +214,10 @@ export const deleteOrganisation = async (req: Request<DeleteOrganisationQueryPar
|
|||||||
|
|
||||||
res.status(204).end();
|
res.status(204).end();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
|
||||||
if ((e.code = "P2025")) {
|
throw new NotFoundError("organisation", pid);
|
||||||
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"));
|
|
||||||
}
|
}
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.status(500).json(genericError);
|
return res.status(500).json(genericError);
|
||||||
|
|||||||
@@ -137,8 +137,17 @@ export const createParticipant = async (req: Request<{ teamPid: string }>, res:
|
|||||||
|
|
||||||
const maxteamsize = discipline?.discipline.maxTeamSize;
|
const maxteamsize = discipline?.discipline.maxTeamSize;
|
||||||
|
|
||||||
const userCount = await prisma.participant.count({
|
const roles = await prisma.team.findUnique({
|
||||||
where: { team: { pid: teamPid } },
|
where: { pid: teamPid },
|
||||||
|
select: { roles: true },
|
||||||
|
});
|
||||||
|
|
||||||
|
let userCount: number = 0;
|
||||||
|
|
||||||
|
roles?.roles.forEach((role) => {
|
||||||
|
if (role.participantId !== null) {
|
||||||
|
userCount++;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (maxteamsize == 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 } });
|
return res.status(201).json({ type: "success", payload: { participant } });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
|
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
|
||||||
return res
|
throw new NotFoundError("team", teamPid);
|
||||||
.status(404)
|
|
||||||
.json(generateError(`Could not link to team with ID '${teamPid}, or group with ID ${body.groupPid}'`));
|
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ export const requestToken = async (req: Request, res: Response) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!team) {
|
if (!team) {
|
||||||
return res.status(404).json(generateError("Team does not exist!"));
|
throw new NotFoundError("team", teamId);
|
||||||
}
|
}
|
||||||
|
|
||||||
const usid = nanoid();
|
const usid = nanoid();
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ export const generateDefaultCredentials = async () => {
|
|||||||
|
|
||||||
const pw: string = crypto.randomBytes(8).toString("base64url");
|
const pw: string = crypto.randomBytes(8).toString("base64url");
|
||||||
|
|
||||||
await prisma.admin.upsert({
|
const defaultAdmin = await prisma.admin.upsert({
|
||||||
where: { id: 1 },
|
where: { id: 1 },
|
||||||
create: {
|
create: {
|
||||||
name,
|
name,
|
||||||
@@ -18,8 +18,11 @@ export const generateDefaultCredentials = async () => {
|
|||||||
permission_level: "ELEVATED",
|
permission_level: "ELEVATED",
|
||||||
},
|
},
|
||||||
update: {},
|
update: {},
|
||||||
|
select: { name: true },
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.notice("Default admin name is: " + name);
|
if (defaultAdmin.name === name) {
|
||||||
logger.notice("Confidential password: " + pw);
|
logger.notice("Default admin name is: " + name);
|
||||||
|
logger.notice("Confidential password: " + pw);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user