77 lines
2.1 KiB
TypeScript
77 lines
2.1 KiB
TypeScript
import { getClientIp } from "@minecraft-account-manager/network";
|
|
import { ipObservations, recordEvent } from "@minecraft-account-manager/database";
|
|
import { headers } from "next/headers";
|
|
import { db } from "@/lib/database";
|
|
|
|
const UI_ACCESSED = "games.minecraft.account-manager.ui.accessed";
|
|
|
|
export async function recordAdminSubjectEvent(
|
|
admin: { email: string | null; name: string | null },
|
|
subject: string,
|
|
type: string,
|
|
data: Record<string, unknown>,
|
|
) {
|
|
const requestHeaders = await headers();
|
|
const ipAddress = getClientIp(requestHeaders, process.env.TRUST_PROXY === "true");
|
|
return recordEvent(db, {
|
|
type,
|
|
source: "/web/admin",
|
|
subject,
|
|
ipAddress: ipAddress ?? undefined,
|
|
data: { ...data, adminEmail: admin.email, adminName: admin.name },
|
|
});
|
|
}
|
|
|
|
export async function recordAdminEvent(
|
|
admin: { email: string | null; name: string | null },
|
|
targetUserId: string,
|
|
type: string,
|
|
data: Record<string, unknown>,
|
|
) {
|
|
return recordAdminSubjectEvent(admin, `user/${targetUserId}`, type, data);
|
|
}
|
|
|
|
export async function recordUserEvent(
|
|
user: { id: string },
|
|
type: string,
|
|
data: Record<string, unknown>,
|
|
) {
|
|
const requestHeaders = await headers();
|
|
const ipAddress = getClientIp(requestHeaders, process.env.TRUST_PROXY === "true");
|
|
return recordEvent(db, {
|
|
type,
|
|
source: "/web",
|
|
subject: `user/${user.id}`,
|
|
actorUserId: user.id,
|
|
ipAddress: ipAddress ?? undefined,
|
|
data,
|
|
});
|
|
}
|
|
|
|
export async function recordAuthenticatedUiAccess(
|
|
user: { id: string },
|
|
path: string,
|
|
) {
|
|
const requestHeaders = await headers();
|
|
const ipAddress = getClientIp(requestHeaders, process.env.TRUST_PROXY === "true");
|
|
|
|
await Promise.all([
|
|
recordEvent(db, {
|
|
type: UI_ACCESSED,
|
|
source: "/web",
|
|
subject: `user/${user.id}`,
|
|
actorUserId: user.id,
|
|
ipAddress: ipAddress ?? undefined,
|
|
data: { path },
|
|
}),
|
|
ipAddress
|
|
? db.insert(ipObservations).values({
|
|
userId: user.id,
|
|
source: "web",
|
|
ipAddress,
|
|
classification: "unknown",
|
|
})
|
|
: Promise.resolve(),
|
|
]);
|
|
}
|