61 lines
1.8 KiB
TypeScript
61 lines
1.8 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import {
|
|
createPeopleMutationInvocation,
|
|
peopleMutationService,
|
|
} from "@/features/housekeeping/domains/people/services/mutations";
|
|
import { createCorrelationId } from "@/features/housekeeping/foundation/contracts";
|
|
import { requirePermission } from "@/lib/admin/guard";
|
|
import { formPositiveBigInt } from "@/lib/form-data";
|
|
import { PERMS } from "@/lib/permissions";
|
|
|
|
function parse(formData: FormData, key: string): string {
|
|
return String(formData.get(key) ?? "")
|
|
.normalize("NFC")
|
|
.trim()
|
|
.slice(0, 255);
|
|
}
|
|
|
|
async function run(
|
|
formData: FormData,
|
|
action:
|
|
| "add-whitelist"
|
|
| "delete-whitelist"
|
|
| "add-blacklist"
|
|
| "delete-blacklist",
|
|
): Promise<void> {
|
|
const staff = await requirePermission(PERMS.SETTINGS_EDIT);
|
|
const adding = action.startsWith("add-");
|
|
const rawId = adding ? null : formPositiveBigInt(formData, "id");
|
|
const input = adding
|
|
? {
|
|
action,
|
|
ipAddress: parse(formData, "ipAddress"),
|
|
asn: parse(formData, "asn"),
|
|
}
|
|
: { action, id: rawId?.toString() ?? "" };
|
|
if (adding && !("ipAddress" in input && input.ipAddress)) return;
|
|
if (!adding && !rawId) return;
|
|
const result = await peopleMutationService.execute(
|
|
createPeopleMutationInvocation(staff, createCorrelationId()),
|
|
"ip.action",
|
|
input,
|
|
);
|
|
if (!result.ok) throw new Error("Could not update IP rules");
|
|
revalidatePath("/admin/ip");
|
|
}
|
|
|
|
export async function addWhitelist(formData: FormData): Promise<void> {
|
|
return run(formData, "add-whitelist");
|
|
}
|
|
export async function deleteWhitelist(formData: FormData): Promise<void> {
|
|
return run(formData, "delete-whitelist");
|
|
}
|
|
export async function addBlacklist(formData: FormData): Promise<void> {
|
|
return run(formData, "add-blacklist");
|
|
}
|
|
export async function deleteBlacklist(formData: FormData): Promise<void> {
|
|
return run(formData, "delete-blacklist");
|
|
}
|