Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,150 @@
|
||||
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>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
import { Tenant } from "@toolctrl/db/prisma-browser";
|
||||
import { ActionFunction, LoaderFunctionArgs, redirect, useLoaderData } from "react-router";
|
||||
import { useActionData, useNavigate } from "react-router";
|
||||
import ApiKeyCreatedModal from "~/components/core/apiKeys/ApiKeyCreatedModal";
|
||||
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 { createApiKey, getApiKeyByAlias } 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 { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import EventsService from "~/modules/events/services/.server/EventsService";
|
||||
import { getUserInfo, validateCSRFToken } from "~/utils/session.server";
|
||||
import { ApiKeyCreatedDto } from "~/modules/events/dtos/ApiKeyCreatedDto";
|
||||
import { adminPath } from "~/utils/app/CanonicalUrlUtils";
|
||||
|
||||
type LoaderData = {
|
||||
tenants: Tenant[];
|
||||
};
|
||||
export const loader = async ({ request }: LoaderFunctionArgs) => {
|
||||
await verifyUserHasPermission(request, "admin.apiKeys.create");
|
||||
const tenants = await adminGetAllTenants();
|
||||
const data: LoaderData = {
|
||||
tenants,
|
||||
};
|
||||
return data;
|
||||
};
|
||||
|
||||
type ActionData = {
|
||||
error?: string;
|
||||
apiKey?: {
|
||||
key: string;
|
||||
alias: string;
|
||||
};
|
||||
};
|
||||
const badRequest = (data: ActionData) => Response.json(data, { status: 400 });
|
||||
export const action: ActionFunction = async ({ request, params }) => {
|
||||
await verifyUserHasPermission(request, "admin.apiKeys.create");
|
||||
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() ?? "";
|
||||
if (action === "create") {
|
||||
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 getApiKeyByAlias(tenantId, alias);
|
||||
if (existingAlias) {
|
||||
return badRequest({ error: t("models.apiKey.errors.aliasExists", { alias }) });
|
||||
}
|
||||
const active = Boolean(form.get("active"));
|
||||
const apiKey = await createApiKey(
|
||||
{
|
||||
tenantId,
|
||||
createdByUserId: userInfo.userId,
|
||||
alias,
|
||||
expires: expirationDate,
|
||||
active,
|
||||
},
|
||||
entities
|
||||
);
|
||||
await createAdminLog(request, "admin.apiKeys.created", JSON.stringify({ id: apiKey.id, tenantId, alias, expirationDate, active, entities }));
|
||||
await EventsService.create({
|
||||
request,
|
||||
event: "api_key.created",
|
||||
tenantId,
|
||||
userId: currentUser?.id ?? "",
|
||||
data: {
|
||||
id: apiKey.id,
|
||||
alias: apiKey.alias,
|
||||
expires: expirationDate,
|
||||
active: active,
|
||||
entities: await getApiKeyEntityPermissions(entities),
|
||||
user: { id: currentUser?.id ?? "", email: currentUser?.email ?? "" },
|
||||
} satisfies ApiKeyCreatedDto,
|
||||
});
|
||||
// return success({ apiKey });
|
||||
return redirect(adminPath("api/keys", { mode: "host-aware" }));
|
||||
} else {
|
||||
return badRequest({ error: t("shared.invalidForm") });
|
||||
}
|
||||
};
|
||||
|
||||
export default function AdminApiNewKeyRoute() {
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const actionData = useActionData<ActionData>();
|
||||
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} />
|
||||
{actionData?.apiKey !== undefined && <ApiKeyCreatedModal apiKey={actionData?.apiKey} redirectTo={adminPath("api/keys", { mode: "host-aware" })} />}
|
||||
</OpenModal>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user