feat(admin): users and roles added to the frontend to manage easier
This commit is contained in:
@@ -4,22 +4,30 @@ import { requireAuth } from "../../pkg/middleware/authMiddleware.js";
|
||||
//admin routes
|
||||
import users from "./routes/getUserRoles.js";
|
||||
import grantRoles from "./routes/grantRole.js";
|
||||
import revokeRoles from "./routes/revokeRole.js";
|
||||
import servers from "./routes/servers/serverRoutes.js";
|
||||
|
||||
export const setupAdminRoutes = (app: Express, basePath: string) => {
|
||||
app.use(
|
||||
basePath + "/api/admin/server", // will pass bc system admin but this is just telling us we need this
|
||||
servers
|
||||
);
|
||||
app.use(
|
||||
basePath + "/api/admin/server", // will pass bc system admin but this is just telling us we need this
|
||||
servers,
|
||||
);
|
||||
|
||||
app.use(
|
||||
basePath + "/api/admin/users",
|
||||
requireAuth("user", ["systemAdmin"]), // will pass bc system admin but this is just telling us we need this
|
||||
users
|
||||
);
|
||||
app.use(
|
||||
basePath + "/api/admin",
|
||||
requireAuth("user", ["systemAdmin", "admin"]), // will pass bc system admin but this is just telling us we need this
|
||||
grantRoles
|
||||
);
|
||||
app.use(
|
||||
basePath + "/api/admin/users",
|
||||
requireAuth("user", ["systemAdmin"]), // will pass bc system admin but this is just telling us we need this
|
||||
users,
|
||||
);
|
||||
|
||||
app.use(
|
||||
basePath + "/api/admin",
|
||||
requireAuth("user", ["systemAdmin", "admin"]), // will pass bc system admin but this is just telling us we need this
|
||||
grantRoles,
|
||||
);
|
||||
|
||||
app.use(
|
||||
basePath + "/api/admin",
|
||||
requireAuth("user", ["systemAdmin", "admin"]), // will pass bc system admin but this is just telling us we need this
|
||||
revokeRoles,
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,74 +1,82 @@
|
||||
import { Router } from "express";
|
||||
import type { Request, Response } from "express";
|
||||
import { tryCatch } from "../../../pkg/utils/tryCatch.js";
|
||||
import { db } from "../../../pkg/db/db.js";
|
||||
import { Router } from "express";
|
||||
import z from "zod";
|
||||
import { db } from "../../../pkg/db/db.js";
|
||||
import { userRoles } from "../../../pkg/db/schema/user_roles.js";
|
||||
import { createLogger } from "../../../pkg/logger/logger.js";
|
||||
import { tryCatch } from "../../../pkg/utils/tryCatch.js";
|
||||
|
||||
const roleSchema = z.object({
|
||||
module: z.enum([
|
||||
"users",
|
||||
"system",
|
||||
"ocp",
|
||||
"siloAdjustments",
|
||||
"demandManagement",
|
||||
"logistics",
|
||||
"production",
|
||||
"quality",
|
||||
"eom",
|
||||
"forklifts",
|
||||
]),
|
||||
role: z.enum(["admin", "manager", "supervisor", "test,", "viewer"]),
|
||||
module: z.enum([
|
||||
"users",
|
||||
"system",
|
||||
"ocp",
|
||||
"siloAdjustments",
|
||||
"demandManagement",
|
||||
"logistics",
|
||||
"production",
|
||||
"quality",
|
||||
"eom",
|
||||
"forklifts",
|
||||
]),
|
||||
role: z.enum([
|
||||
"systemAdmin",
|
||||
"admin",
|
||||
"manager",
|
||||
"supervisor",
|
||||
"tester",
|
||||
"user",
|
||||
"viewer",
|
||||
]),
|
||||
});
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post("/:userId/grant", async (req: Request, res: Response) => {
|
||||
const log = createLogger({
|
||||
module: "admin",
|
||||
subModule: "grantRoles",
|
||||
});
|
||||
const userId = req.params.userId;
|
||||
console.log(userId);
|
||||
router.patch("/:userId/grant", async (req: Request, res: Response) => {
|
||||
const log = createLogger({
|
||||
module: "admin",
|
||||
subModule: "grantRoles",
|
||||
});
|
||||
const userId = req.params.userId;
|
||||
|
||||
try {
|
||||
const validated = roleSchema.parse(req.body);
|
||||
try {
|
||||
const validated = roleSchema.parse(req.body);
|
||||
|
||||
const data = await db
|
||||
.insert(userRoles)
|
||||
.values({
|
||||
userId,
|
||||
module: validated.module,
|
||||
role: validated.role,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: [userRoles.userId, userRoles.module],
|
||||
set: { module: validated.module, role: validated.role },
|
||||
});
|
||||
log.info(
|
||||
{},
|
||||
`Module: ${validated.module}, Role: ${validated.role} as was just granted to userID: ${userId}`
|
||||
);
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: `Module: ${validated.module}, Role: ${validated.role} as was just granted`,
|
||||
data,
|
||||
});
|
||||
} catch (err) {
|
||||
if (err instanceof z.ZodError) {
|
||||
const flattened = z.flattenError(err);
|
||||
return res.status(400).json({
|
||||
error: "Validation failed",
|
||||
details: flattened,
|
||||
});
|
||||
}
|
||||
const data = await db
|
||||
.insert(userRoles)
|
||||
.values({
|
||||
userId: userId,
|
||||
module: validated.module,
|
||||
role: validated.role,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: [userRoles.userId, userRoles.module],
|
||||
set: { module: validated.module, role: validated.role },
|
||||
});
|
||||
log.info(
|
||||
{},
|
||||
`Module: ${validated.module}, Role: ${validated.role} as was just granted to userID: ${userId}`,
|
||||
);
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: `Module: ${validated.module}, Role: ${validated.role} as was just granted`,
|
||||
data,
|
||||
});
|
||||
} catch (err) {
|
||||
if (err instanceof z.ZodError) {
|
||||
const flattened = z.flattenError(err);
|
||||
return res.status(400).json({
|
||||
error: "Validation failed",
|
||||
details: flattened,
|
||||
});
|
||||
}
|
||||
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Invalid input please try again.",
|
||||
});
|
||||
}
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Invalid input please try again.",
|
||||
error: err,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
71
app/src/internal/admin/routes/revokeRole.ts
Normal file
71
app/src/internal/admin/routes/revokeRole.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import type { Request, Response } from "express";
|
||||
import { Router } from "express";
|
||||
import z from "zod";
|
||||
import { db } from "../../../pkg/db/db.js";
|
||||
import { userRoles } from "../../../pkg/db/schema/user_roles.js";
|
||||
import { createLogger } from "../../../pkg/logger/logger.js";
|
||||
import { tryCatch } from "../../../pkg/utils/tryCatch.js";
|
||||
|
||||
const roleSchema = z.object({
|
||||
module: z.enum([
|
||||
"users",
|
||||
"system",
|
||||
"ocp",
|
||||
"siloAdjustments",
|
||||
"demandManagement",
|
||||
"logistics",
|
||||
"production",
|
||||
"quality",
|
||||
"eom",
|
||||
"forklifts",
|
||||
]),
|
||||
});
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.patch("/:userId/revoke", async (req: Request, res: Response) => {
|
||||
const log = createLogger({
|
||||
module: "admin",
|
||||
subModule: "grantRoles",
|
||||
});
|
||||
const userId = req.params.userId;
|
||||
|
||||
try {
|
||||
const validated = roleSchema.parse(req.body);
|
||||
|
||||
const data = await db
|
||||
.delete(userRoles)
|
||||
.where(
|
||||
and(
|
||||
eq(userRoles.userId, userId),
|
||||
eq(userRoles.module, validated.module),
|
||||
),
|
||||
);
|
||||
log.info(
|
||||
{},
|
||||
`Module: ${validated.module}, was just revoked fron userID: ${userId}`,
|
||||
);
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: `Module: ${validated.module}, was just revoked fron userID: ${userId}`,
|
||||
data,
|
||||
});
|
||||
} catch (err) {
|
||||
if (err instanceof z.ZodError) {
|
||||
const flattened = z.flattenError(err);
|
||||
return res.status(400).json({
|
||||
error: "Validation failed",
|
||||
details: flattened,
|
||||
});
|
||||
}
|
||||
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Invalid input please try again.",
|
||||
error: err,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user