151 lines
5.8 KiB
TypeScript
151 lines
5.8 KiB
TypeScript
import { Tenant } from "@toolctrl/db/prisma-browser";
|
|
import { ActionFunction, LoaderFunctionArgs, redirect, useLoaderData } from "react-router";
|
|
import { useNavigate } from "react-router";
|
|
import ApiKeyForm from "~/components/core/apiKeys/ApiKeyForm";
|
|
import OpenModal from "~/components/ui/modals/OpenModal";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { useAdminData } from "~/utils/data/useAdminData";
|
|
import { RedactedApiKeyWithDetails, adminGetApiKeyById, deleteApiKey, getApiKeys, updateApiKey } from "~/utils/db/apiKeys.db.server";
|
|
import { createAdminLog } from "~/utils/db/logs.db.server";
|
|
import { adminGetAllTenants } from "~/utils/db/tenants.db.server";
|
|
import { getUser } from "~/utils/db/users.db.server";
|
|
import { getApiKeyEntityPermissions } from "~/utils/helpers/.server/ApiKeyHelperService";
|
|
import EventsService from "~/modules/events/services/.server/EventsService";
|
|
import { getUserInfo, validateCSRFToken } from "~/utils/session.server";
|
|
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
|
import { getUserHasPermission } from "~/utils/helpers/PermissionsHelper";
|
|
import { ApiKeyDeletedDto } from "~/modules/events/dtos/ApiKeyDeletedDto";
|
|
import { ApiKeyUpdatedDto } from "~/modules/events/dtos/ApiKeyUpdatedDto";
|
|
import { adminPath } from "~/utils/app/CanonicalUrlUtils";
|
|
|
|
type LoaderData = {
|
|
tenants: Tenant[];
|
|
item: RedactedApiKeyWithDetails;
|
|
};
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
await verifyUserHasPermission(request, "admin.apiKeys.view");
|
|
const item = await adminGetApiKeyById(params.id ?? "");
|
|
if (!item) {
|
|
return redirect(adminPath("api", { mode: "host-aware" }));
|
|
}
|
|
const tenants = await adminGetAllTenants();
|
|
const data: LoaderData = {
|
|
item,
|
|
tenants,
|
|
};
|
|
return data;
|
|
};
|
|
|
|
type ActionData = {
|
|
error?: string;
|
|
};
|
|
const badRequest = (data: ActionData) => Response.json(data, { status: 400 });
|
|
export const action: ActionFunction = async ({ request, params }) => {
|
|
await verifyUserHasPermission(request, "admin.apiKeys.update");
|
|
const { t } = await getTranslations(request);
|
|
const userInfo = await getUserInfo(request);
|
|
const currentUser = await getUser(userInfo.userId);
|
|
await validateCSRFToken(request);
|
|
|
|
const form = await request.formData();
|
|
const action = form.get("action")?.toString() ?? "";
|
|
const existing = await adminGetApiKeyById(params.id ?? "");
|
|
if (!existing) {
|
|
return badRequest({ error: t("shared.notFound") });
|
|
}
|
|
if (action === "edit") {
|
|
await verifyUserHasPermission(request, "admin.apiKeys.update");
|
|
const entities: { entityId: string; create: boolean; read: boolean; update: boolean; delete: boolean }[] = form
|
|
.getAll("entities[]")
|
|
.map((f: FormDataEntryValue) => {
|
|
return JSON.parse(f.toString());
|
|
});
|
|
let expirationDate: Date | null = null;
|
|
let expires = form.get("expires")?.toString();
|
|
if (expires) {
|
|
expirationDate = new Date(expires);
|
|
}
|
|
const tenantId = form.get("tenant-id")?.toString() ?? "";
|
|
const alias = form.get("alias")?.toString() ?? "";
|
|
const existingAlias = await getApiKeys(tenantId);
|
|
if (existingAlias.filter((f) => f.id !== existing.id && f.alias === alias).length > 0) {
|
|
return badRequest({ error: t("models.apiKey.errors.aliasExists", { alias }) });
|
|
}
|
|
const active = Boolean(form.get("active"));
|
|
await updateApiKey(
|
|
params.id ?? "",
|
|
{
|
|
tenantId,
|
|
alias,
|
|
expires: expirationDate,
|
|
active,
|
|
},
|
|
entities
|
|
);
|
|
await createAdminLog(request, "admin.apiKeys.updated", JSON.stringify({ tenantId, alias, expirationDate, active, entities }));
|
|
await EventsService.create({
|
|
request,
|
|
event: "api_key.updated",
|
|
tenantId,
|
|
userId: currentUser?.id ?? null,
|
|
data: {
|
|
id: existing.id,
|
|
new: {
|
|
alias: alias,
|
|
expires: expirationDate,
|
|
active: active,
|
|
entities: await getApiKeyEntityPermissions(entities),
|
|
},
|
|
old: {
|
|
alias: existing.alias,
|
|
expires: existing.expires,
|
|
active: existing.active,
|
|
entities: await getApiKeyEntityPermissions(existing.entities),
|
|
},
|
|
user: { id: currentUser?.id ?? "", email: currentUser?.email ?? "" },
|
|
} satisfies ApiKeyUpdatedDto,
|
|
});
|
|
return redirect(adminPath(`api/keys`, { mode: "host-aware" }));
|
|
} else if (action === "delete") {
|
|
await verifyUserHasPermission(request, "admin.apiKeys.delete");
|
|
await deleteApiKey(params.id ?? "", existing.tenantId);
|
|
await createAdminLog(request, "admin.apiKeys.deleted", "");
|
|
await EventsService.create({
|
|
request,
|
|
event: "api_key.deleted",
|
|
tenantId: existing.tenantId,
|
|
userId: currentUser?.id ?? null,
|
|
data: {
|
|
id: existing.id,
|
|
alias: existing.alias,
|
|
expires: existing.expires,
|
|
active: existing.active,
|
|
entities: await getApiKeyEntityPermissions(existing.entities),
|
|
user: { id: currentUser?.id ?? "", email: currentUser?.email ?? "" },
|
|
} satisfies ApiKeyDeletedDto,
|
|
});
|
|
return redirect(adminPath(`api/keys`, { mode: "host-aware" }));
|
|
} else {
|
|
return badRequest({ error: t("shared.invalidForm") });
|
|
}
|
|
};
|
|
|
|
export default function AdminApiEditKeyRoute() {
|
|
const data = useLoaderData<LoaderData>();
|
|
const navigate = useNavigate();
|
|
const adminData = useAdminData();
|
|
return (
|
|
<>
|
|
<OpenModal className="sm:max-w-xl" onClose={() => navigate(adminPath(`api/keys`, { mode: "host-aware" }))}>
|
|
<ApiKeyForm
|
|
entities={adminData.entities}
|
|
tenants={data.tenants}
|
|
item={data.item}
|
|
canUpdate={getUserHasPermission(adminData, "admin.apiKeys.update")}
|
|
canDelete={getUserHasPermission(adminData, "admin.apiKeys.delete")}
|
|
/>
|
|
</OpenModal>
|
|
</>
|
|
);
|
|
}
|