mirror of
https://github.com/detleph/server.git
synced 2026-09-04 08:36:06 +02:00
Allow for groups to be specified when POSTing /api/admins
+ Add the admin router to the global app
This commit is contained in:
@@ -50,7 +50,7 @@ interface CreateAdminBody {
|
|||||||
name?: string;
|
name?: string;
|
||||||
password: string;
|
password: string;
|
||||||
permission_level: string;
|
permission_level: string;
|
||||||
// TODO: Add groups or events
|
groups?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
const PERMISSION_LEVELS: readonly AdminLevel[] = ["ELEVATED", "STANDARD"]; // TODO: Enforce completeness
|
const PERMISSION_LEVELS: readonly AdminLevel[] = ["ELEVATED", "STANDARD"]; // TODO: Enforce completeness
|
||||||
@@ -67,9 +67,13 @@ export const createAdmin = async (req: Request<{}, {}, CreateAdminBody>, res: Re
|
|||||||
return res.status(403).json(createInsufficientPermissionsError());
|
return res.status(403).json(createInsufficientPermissionsError());
|
||||||
}
|
}
|
||||||
|
|
||||||
const { name, password, permission_level } = req.body || {};
|
const { name, password, permission_level, groups } = req.body || {};
|
||||||
|
|
||||||
if (!(typeof name === "string" && typeof password == "string" && isPermissionLevel(permission_level))) {
|
const groupsIsValid = groups ? groups.filter((e) => typeof e !== "string").length === 0 : true;
|
||||||
|
|
||||||
|
if (
|
||||||
|
!(typeof name === "string" && typeof password == "string" && isPermissionLevel(permission_level) && groupsIsValid)
|
||||||
|
) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
type: "error",
|
type: "error",
|
||||||
payload: {
|
payload: {
|
||||||
@@ -87,9 +91,26 @@ export const createAdmin = async (req: Request<{}, {}, CreateAdminBody>, res: Re
|
|||||||
|
|
||||||
const password_hash = await argon2.hash(password, { type: argon2.argon2id });
|
const password_hash = await argon2.hash(password, { type: argon2.argon2id });
|
||||||
|
|
||||||
|
// 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!`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Check for uniqueness of the name
|
// TODO: Check for uniqueness of the name
|
||||||
const user = await prisma.admin.create({
|
const user = await prisma.admin.create({
|
||||||
data: { name, password: password_hash, permission_level },
|
data: {
|
||||||
|
name,
|
||||||
|
password: password_hash,
|
||||||
|
permission_level,
|
||||||
|
groups: { connect: groups?.map((group) => ({ pid: group })) },
|
||||||
|
},
|
||||||
select: { pid: true, name: true, permission_level: true },
|
select: { pid: true, name: true, permission_level: true },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import prisma from "./lib/prisma";
|
|||||||
import eventRouter from "./Routes/event.routes";
|
import eventRouter from "./Routes/event.routes";
|
||||||
import adminAuthRouter from "./Routes/admin_auth.routes";
|
import adminAuthRouter from "./Routes/admin_auth.routes";
|
||||||
import argon2 from "argon2";
|
import argon2 from "argon2";
|
||||||
|
import adminRouter from "./Routes/admin.routes";
|
||||||
|
|
||||||
require("dotenv").config(); // Load dotenv config
|
require("dotenv").config(); // Load dotenv config
|
||||||
|
|
||||||
@@ -28,6 +29,8 @@ async function main() {
|
|||||||
// All API endpoints are behind /api/...
|
// All API endpoints are behind /api/...
|
||||||
app.use("/api/events", eventRouter);
|
app.use("/api/events", eventRouter);
|
||||||
|
|
||||||
|
app.use("/api/admins", adminRouter);
|
||||||
|
|
||||||
app.listen(process.env.PORT, () => {
|
app.listen(process.env.PORT, () => {
|
||||||
console.log(`Listening on Port: ${process.env.PORT}`);
|
console.log(`Listening on Port: ${process.env.PORT}`);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user