Scaffold Homelab Manager foundation
Monorepo (Express+TS+Drizzle/libSQL server, React+Vite+Tabler web) matching the stack used by ScheduleTaskManager and Sloth Manager. Includes Authentik OIDC login with local admin/operator/viewer roles (first user becomes admin), a generalized audit log, encrypted-at-rest storage for future integration API tokens, the DB schema for all planned modules, and the Tabler-styled app shell/nav. Also ports the Secrets (expiry tracker) and IP Addresses (IPAM) modules from Sloth Manager onto the new stack. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
commit
6bd2ed52c1
52 files changed
+8103
No files matched your search
@@ -0,0 +1,61 @@
|
||||
import { Router } from "express";
|
||||
import { eq, ne, and } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { db } from "../db/client.js";
|
||||
import { users, userRoles } from "../db/schema.js";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
|
||||
export const usersRouter = Router();
|
||||
|
||||
usersRouter.use(requireAuth, requireRole("admin"));
|
||||
|
||||
usersRouter.get("/", async (_req, res) => {
|
||||
const rows = await db.select().from(users).orderBy(users.createdAt);
|
||||
res.json({ users: rows });
|
||||
});
|
||||
|
||||
const updateRoleSchema = z.object({
|
||||
role: z.enum(userRoles),
|
||||
});
|
||||
|
||||
usersRouter.patch("/:id/role", async (req, res) => {
|
||||
const id = Number(req.params.id);
|
||||
const parsed = updateRoleSchema.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({ error: "invalid_body", details: parsed.error.flatten() });
|
||||
}
|
||||
|
||||
const [target] = await db.select().from(users).where(eq(users.id, id)).limit(1);
|
||||
if (!target) {
|
||||
return res.status(404).json({ error: "not_found" });
|
||||
}
|
||||
|
||||
if (target.role === "admin" && parsed.data.role !== "admin") {
|
||||
const otherAdmins = await db
|
||||
.select({ id: users.id })
|
||||
.from(users)
|
||||
.where(and(eq(users.role, "admin"), ne(users.id, id)))
|
||||
.limit(1);
|
||||
if (otherAdmins.length === 0) {
|
||||
return res.status(400).json({ error: "last_admin", message: "Cannot remove the only admin." });
|
||||
}
|
||||
}
|
||||
|
||||
const [updated] = await db
|
||||
.update(users)
|
||||
.set({ role: parsed.data.role })
|
||||
.where(eq(users.id, id))
|
||||
.returning();
|
||||
|
||||
await recordAudit({
|
||||
actor: req.currentUser!,
|
||||
category: "user",
|
||||
action: "update_role",
|
||||
targetType: "user",
|
||||
targetId: id,
|
||||
detail: { from: target.role, to: parsed.data.role, targetLabel: target.name ?? target.email },
|
||||
});
|
||||
|
||||
res.json({ user: updated });
|
||||
});
|
||||
Reference in new issue
Block a user