Admin page
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import { z } from "@zod/zod";
|
||||
|
||||
import { define } from "@/utils.ts";
|
||||
import type { AdminUserEntry, CreatedUser } from "@/src/admin.ts";
|
||||
import { authenticate, forbidden, unauthorized } from "@/src/auth.ts";
|
||||
import { generate_code } from "@/src/crypto.ts";
|
||||
import { db, type FileRow, type User } from "@/src/db.ts";
|
||||
|
||||
// Sent by the Form island, so the checkbox is "on" or missing
|
||||
const CreateUserBody = z.object({
|
||||
name: z.string().trim().min(1).max(100),
|
||||
admin: z.literal("on").optional(),
|
||||
});
|
||||
|
||||
async function authenticate_admin(req: Request) {
|
||||
const user = await authenticate(req);
|
||||
if (!user) {
|
||||
return unauthorized();
|
||||
}
|
||||
if ((user as Partial<User>).admin !== true) {
|
||||
return forbidden();
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export const handler = define.handlers({
|
||||
async GET(ctx) {
|
||||
const denied = await authenticate_admin(ctx.req);
|
||||
if (denied) {
|
||||
return denied;
|
||||
}
|
||||
|
||||
const files = await db.get_all_entries("files") as FileRow[];
|
||||
const usage = new Map<string, { file_count: number; storage_used: number }>();
|
||||
for (const file of files) {
|
||||
const entry = usage.get(file.user_id) ?? { file_count: 0, storage_used: 0 };
|
||||
entry.file_count++;
|
||||
entry.storage_used += file.size;
|
||||
usage.set(file.user_id, entry);
|
||||
}
|
||||
|
||||
const users = await db.get_all_entries("users") as (User & { id: string; date_created: string })[];
|
||||
return Response.json(
|
||||
users.map((user): AdminUserEntry => ({
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
admin: user.admin,
|
||||
date_created: user.date_created,
|
||||
has_key: Boolean(user.key_check),
|
||||
...(usage.get(user.id) ?? { file_count: 0, storage_used: 0 }),
|
||||
})),
|
||||
);
|
||||
},
|
||||
|
||||
async POST(ctx) {
|
||||
const denied = await authenticate_admin(ctx.req);
|
||||
if (denied) {
|
||||
return denied;
|
||||
}
|
||||
|
||||
const body = CreateUserBody.safeParse(await ctx.req.json().catch(() => null));
|
||||
if (!body.success) {
|
||||
return Response.json({ error: "Enter a name" }, { status: 400 });
|
||||
}
|
||||
|
||||
let code = generate_code();
|
||||
while (await db.get_entry_by_index("users", "code", code)) {
|
||||
code = generate_code();
|
||||
}
|
||||
|
||||
const user = await db.create_entry("users", { name: body.data.name, code, admin: body.data.admin === "on" });
|
||||
return Response.json(
|
||||
{ id: user.id, name: user.name, admin: user.admin, code: user.code } satisfies CreatedUser,
|
||||
{ status: 201 },
|
||||
);
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
import { define } from "@/utils.ts";
|
||||
import type { MeResponse } from "@/src/admin.ts";
|
||||
import { authenticate, unauthorized } from "@/src/auth.ts";
|
||||
import type { User } from "@/src/db.ts";
|
||||
|
||||
export const handler = define.handlers({
|
||||
async GET(ctx) {
|
||||
const user = await authenticate(ctx.req);
|
||||
if (!user) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
const { name, admin } = user as Partial<User>;
|
||||
return Response.json({ id: user.id, name: name ?? "", admin: admin === true } satisfies MeResponse);
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user