72 lines
2.2 KiB
TypeScript
72 lines
2.2 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
|
|
import { requireAdminAccess } from "@/lib/session";
|
|
import {
|
|
createUser,
|
|
deleteUser,
|
|
resetUserPassword,
|
|
toBytesFromGiB,
|
|
updateUser,
|
|
} from "@/lib/store";
|
|
|
|
function boolFromForm(value: FormDataEntryValue | null) {
|
|
return value === "on";
|
|
}
|
|
|
|
function optionalDateTime(value: FormDataEntryValue | null) {
|
|
const raw = String(value ?? "").trim();
|
|
return raw ? new Date(raw).toISOString() : null;
|
|
}
|
|
|
|
export async function createUserAction(formData: FormData) {
|
|
await requireAdminAccess();
|
|
await createUser({
|
|
username: String(formData.get("username") ?? "").trim(),
|
|
password: String(formData.get("password") ?? ""),
|
|
enabled: boolFromForm(formData.get("enabled")),
|
|
isAdmin: false,
|
|
expiresAt: optionalDateTime(formData.get("expires_at")),
|
|
trafficLimitBytes: toBytesFromGiB(String(formData.get("traffic_limit_gib") ?? "")),
|
|
notes: String(formData.get("notes") ?? "").trim(),
|
|
});
|
|
|
|
revalidatePath("/admin/users");
|
|
revalidatePath("/admin/dashboard");
|
|
}
|
|
|
|
export async function updateUserAction(formData: FormData) {
|
|
await requireAdminAccess();
|
|
await updateUser({
|
|
id: Number(formData.get("id")),
|
|
username: String(formData.get("username") ?? "").trim(),
|
|
enabled: boolFromForm(formData.get("enabled")),
|
|
subscriptionLabel:
|
|
String(formData.get("subscription_label") ?? "").trim() ||
|
|
String(formData.get("username") ?? "").trim(),
|
|
expiresAt: optionalDateTime(formData.get("expires_at")),
|
|
trafficLimitBytes: toBytesFromGiB(String(formData.get("traffic_limit_gib") ?? "")),
|
|
notes: String(formData.get("notes") ?? "").trim(),
|
|
});
|
|
|
|
revalidatePath("/admin/users");
|
|
revalidatePath("/admin/dashboard");
|
|
}
|
|
|
|
export async function deleteUserAction(formData: FormData) {
|
|
await requireAdminAccess();
|
|
await deleteUser(Number(formData.get("id")));
|
|
revalidatePath("/admin/users");
|
|
revalidatePath("/admin/dashboard");
|
|
}
|
|
|
|
export async function resetUserPasswordAction(formData: FormData) {
|
|
await requireAdminAccess();
|
|
const password = String(formData.get("password") ?? "");
|
|
if (!password) return;
|
|
|
|
await resetUserPassword(Number(formData.get("id")), password);
|
|
revalidatePath("/admin/users");
|
|
}
|