From 59f4d53af5ba2c08714bba7282ff8648cf90960c Mon Sep 17 00:00:00 2001 From: Stephan <57194608+stephan418@users.noreply.github.com> Date: Wed, 1 Dec 2021 16:37:28 +0100 Subject: [PATCH] Add a controller for getting all admin user details + Requires Authentication --- src/Controllers/admin.controller.ts | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/Controllers/admin.controller.ts diff --git a/src/Controllers/admin.controller.ts b/src/Controllers/admin.controller.ts new file mode 100644 index 0000000..bdd64d0 --- /dev/null +++ b/src/Controllers/admin.controller.ts @@ -0,0 +1,40 @@ +import prisma from "../lib/prisma"; +import { Request, Response } from "express"; + +// requires: auth(elevated) +export const getAllAdmins = async (req: Request, res: Response) => { + if (!req.auth?.isAuthenticated) { + return res.status(500).json({ + type: "failure", + payload: { + message: "The server was not able to validate your credentials; Please try again later", + }, + }); + } + + if (req.auth.permission_level !== "ELEVATED") { + return res.status(403).json({ + type: "error", + payload: { + message: "You do not have sufficient permissions to use this feature", + }, + _links: [ + { + rel: "authentication", + href: "/api/authentication", + type: "POST", + }, + ], + }); + } + + // TODO: Add exception handling + const users = await prisma.admin.findMany({ select: { pid: true, name: true, permission_level: true } }); + + res.status(200).json({ + type: "success", + payload: { + users, + }, + }); +};