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,672 @@
|
||||
/**
|
||||
* Phase 06 #22 — Tenant-Admin plugins UI.
|
||||
*
|
||||
* Lists installed plugins for the current tenant with capability consent,
|
||||
* enable / disable toggles and a manifest preview. The route is deliberately
|
||||
* scoped (no install dialog, no per-user toggles) — the foundation surfaces
|
||||
* what plugins exist, links manifest-declared settings UIs, and lets tenant
|
||||
* admins approve manifest-declared capabilities before flipping the kill-switch.
|
||||
*
|
||||
* Permission: `app.plugins.use` can view installed plugins and
|
||||
* `app.plugins.manage` can grant or revoke capabilities and enable/disable them.
|
||||
*/
|
||||
|
||||
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
|
||||
import {
|
||||
getPluginCapabilityMetadata,
|
||||
isPluginCapabilityName,
|
||||
type PluginCapabilityMetadata,
|
||||
type PluginCapabilityRisk,
|
||||
} from "@toolctrl/plugin-runtime/capabilities";
|
||||
import { isPluginHookProductionAvailable } from "@toolctrl/plugin-runtime/hooks/registry";
|
||||
import type { PluginCapability } from "@toolctrl/plugin-runtime/manifest";
|
||||
import { Form, Link, useActionData, useLoaderData } from "react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useState } from "react";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { useLocaleFormatters } from "~/locale/format";
|
||||
import { appPath } from "~/utils/app/CanonicalUrlUtils";
|
||||
import { useRootData } from "~/utils/data/useRootData";
|
||||
import { getUserPermission, verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
||||
import { getUserInfo, validateCSRFToken } from "~/utils/session.server";
|
||||
import { getUser } from "~/utils/db/users.db.server";
|
||||
import {
|
||||
PluginLifecycleError,
|
||||
disablePlugin,
|
||||
enablePlugin,
|
||||
grantPluginCapabilities,
|
||||
isCapabilityGrantScopeSatisfied,
|
||||
listPluginCapabilityGrants,
|
||||
listPluginCapabilityGrantsForPlugins,
|
||||
listPlugins,
|
||||
revokePluginCapabilities,
|
||||
type PluginLifecycleActor,
|
||||
type PluginListItem,
|
||||
} from "~/modules/plugins/services/.server/PluginLifecycleService";
|
||||
import { getPluginConsentRequirements, isPluginHookConsentName } from "~/modules/plugins/utils/pluginConsentRequirements";
|
||||
import { parsePluginCapabilityNameArray, readPluginConsentUrlEncodedFormData } from "~/modules/plugins/utils/capabilityConsentPayload.server";
|
||||
|
||||
async function createCapabilityReviewDigest(item: Pick<PluginListItem, "pluginId" | "version" | "manifest">, capabilityNames: ReadonlyArray<string>) {
|
||||
const { createCapabilityReviewDigest: createDigest } = await import("~/modules/plugins/utils/capabilityReviewDigest");
|
||||
return createDigest(item, capabilityNames);
|
||||
}
|
||||
|
||||
type SerializedPluginCapability = {
|
||||
name: string;
|
||||
scope: PluginCapability["scope"] | null;
|
||||
granted: boolean;
|
||||
grantedAt: string | null;
|
||||
grantedScopeJson: unknown;
|
||||
metadata: SerializedPluginCapabilityMetadata | null;
|
||||
};
|
||||
|
||||
type SerializedPluginCapabilityMetadata = Pick<PluginCapabilityMetadata, "risk" | "productionAvailable">;
|
||||
|
||||
type SerializedPluginItem = {
|
||||
id: string;
|
||||
pluginId: string;
|
||||
version: string;
|
||||
enabled: boolean;
|
||||
capabilities: ReadonlyArray<SerializedPluginCapability>;
|
||||
missingCapabilities: ReadonlyArray<string>;
|
||||
capabilityReviewDigest: string;
|
||||
hasSettingsUi: boolean;
|
||||
manifestJson: string;
|
||||
installedAt: string;
|
||||
};
|
||||
|
||||
type LoaderData = {
|
||||
title: string;
|
||||
tenantSlug: string;
|
||||
canManage: boolean;
|
||||
canAudit: boolean;
|
||||
items: ReadonlyArray<SerializedPluginItem>;
|
||||
};
|
||||
|
||||
type ActionData = { ok: boolean; message?: string };
|
||||
const USE_PERMISSION = "app.plugins.use";
|
||||
const MANAGE_PERMISSION = "app.plugins.manage";
|
||||
const AUDIT_PERMISSION = "app.settings.auditTrails.view";
|
||||
|
||||
export function formatPluginCapabilityLabel(capability: string, t: (key: string, options?: Record<string, unknown>) => string) {
|
||||
if (isPluginHookConsentName(capability)) {
|
||||
return t("settings.plugins.consent.hookLabel", { hook: capability.slice("hook:".length) });
|
||||
}
|
||||
if (isPluginCapabilityName(capability)) {
|
||||
return t(`settings.plugins.capabilityNames.${capability.replaceAll(".", "_")}`);
|
||||
}
|
||||
return t(`settings.plugins.capabilityNames.${capability.replaceAll(".", "_")}`, {
|
||||
defaultValue: t("settings.plugins.capabilityNames.unknown", { capability }),
|
||||
});
|
||||
}
|
||||
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
const tenantId = await getTenantIdFromUrl(params);
|
||||
await verifyUserHasPermission(request, USE_PERMISSION, tenantId);
|
||||
const [{ t }, info, items] = await Promise.all([getTranslations(request), getUserInfo(request), listPlugins({ tenantId })]);
|
||||
const [managePermission, auditPermission, serializedItems] = await Promise.all([
|
||||
info.userId
|
||||
? getUserPermission({
|
||||
userId: info.userId,
|
||||
permissionName: MANAGE_PERMISSION,
|
||||
tenantId,
|
||||
})
|
||||
: Promise.resolve(null),
|
||||
info.userId
|
||||
? getUserPermission({
|
||||
userId: info.userId,
|
||||
permissionName: AUDIT_PERMISSION,
|
||||
tenantId,
|
||||
})
|
||||
: Promise.resolve(null),
|
||||
serializePluginItems({ tenantId, items }),
|
||||
]);
|
||||
const data: LoaderData = {
|
||||
title: `${t("settings.plugins.title")} | ${process.env.APP_NAME}`,
|
||||
tenantSlug: params.tenant ?? "",
|
||||
canManage: Boolean(managePermission?.userPermission),
|
||||
canAudit: Boolean(auditPermission?.userPermission),
|
||||
items: serializedItems,
|
||||
};
|
||||
return Response.json(data);
|
||||
};
|
||||
|
||||
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
const tenantId = await getTenantIdFromUrl(params);
|
||||
await verifyUserHasPermission(request, MANAGE_PERMISSION, tenantId);
|
||||
const { t } = await getTranslations(request);
|
||||
const parsedForm = await readPluginConsentUrlEncodedFormData(request);
|
||||
if (!parsedForm.ok) {
|
||||
return Response.json({ ok: false, message: t("settings.plugins.errors.invalidFormPayload") } satisfies ActionData, { status: 400 });
|
||||
}
|
||||
const form = parsedForm.formData;
|
||||
await validateCSRFToken(request, { formData: form });
|
||||
const op = form.get("op");
|
||||
const pluginRowId = form.get("id");
|
||||
if (typeof pluginRowId !== "string" || pluginRowId.length === 0) {
|
||||
return Response.json({ ok: false, message: t("settings.plugins.errors.missingPluginId") } satisfies ActionData, { status: 400 });
|
||||
}
|
||||
const actor = await resolveActor(request);
|
||||
try {
|
||||
if (op === "enable") {
|
||||
await enablePlugin({ tenantId, pluginRowId, actor });
|
||||
return Response.json({ ok: true } satisfies ActionData);
|
||||
}
|
||||
if (op === "disable") {
|
||||
await disablePlugin({ tenantId, pluginRowId, actor });
|
||||
return Response.json({ ok: true } satisfies ActionData);
|
||||
}
|
||||
if (op === "revoke") {
|
||||
const submittedPluginId = form.get("pluginId");
|
||||
const parsedNames = parsePluginCapabilityNameArray(form.getAll("capabilityNames[]"));
|
||||
if (typeof submittedPluginId !== "string" || submittedPluginId.length === 0 || !parsedNames.ok) {
|
||||
return Response.json({ ok: false, message: t("settings.plugins.errors.missingCapabilityRevokeSelection") } satisfies ActionData, { status: 400 });
|
||||
}
|
||||
const item = (await listPlugins({ tenantId })).find((row) => row.id === pluginRowId);
|
||||
if (!item) {
|
||||
return Response.json({ ok: false, message: t("settings.plugins.errors.pluginNotFound") } satisfies ActionData, { status: 404 });
|
||||
}
|
||||
if (item.pluginId !== submittedPluginId) {
|
||||
return Response.json({ ok: false, message: t("settings.plugins.errors.staleCapabilityReview") } satisfies ActionData, { status: 409 });
|
||||
}
|
||||
await revokePluginCapabilities({ tenantId, pluginId: item.pluginId, capabilityNames: parsedNames.capabilityNames, actor });
|
||||
return Response.json({ ok: true } satisfies ActionData);
|
||||
}
|
||||
if (op === "grant" || op === "grantAndEnable") {
|
||||
const parsedNames = parsePluginCapabilityNameArray(form.getAll("capabilities[]"));
|
||||
if (!parsedNames.ok) {
|
||||
return Response.json({ ok: false, message: t("settings.plugins.errors.missingCapabilitySelection") } satisfies ActionData, { status: 400 });
|
||||
}
|
||||
const item = (await listPlugins({ tenantId })).find((row) => row.id === pluginRowId);
|
||||
if (!item) {
|
||||
return Response.json({ ok: false, message: t("settings.plugins.errors.pluginNotFound") } satisfies ActionData, { status: 404 });
|
||||
}
|
||||
if (form.get("capabilitiesReviewed") !== "yes") {
|
||||
return Response.json({ ok: false, message: t("settings.plugins.errors.missingCapabilityReview") } satisfies ActionData, { status: 400 });
|
||||
}
|
||||
const declaredCapabilities = new Map(getPluginConsentRequirements(item.manifest).map((requirement) => [requirement.name, requirement]));
|
||||
if (parsedNames.capabilityNames.length === 0) {
|
||||
return Response.json({ ok: false, message: t("settings.plugins.errors.missingCapabilitySelection") } satisfies ActionData, { status: 400 });
|
||||
}
|
||||
const existingGrants = await listPluginCapabilityGrants({ tenantId, pluginId: item.pluginId });
|
||||
const grantByCapability = new Map(existingGrants.map((grant) => [grant.capabilityName, grant]));
|
||||
const missingCapabilities = getPluginConsentRequirements(item.manifest)
|
||||
.filter((requirement) => {
|
||||
const grant = grantByCapability.get(requirement.name);
|
||||
return !grant || !isCapabilityGrantScopeSatisfied({ declaredScope: requirement.scope ?? null, grantedScopeJson: grant.scopeJson });
|
||||
})
|
||||
.map((requirement) => requirement.name);
|
||||
if (!areCapabilityNameSetsEqual(parsedNames.capabilityNames, missingCapabilities)) {
|
||||
return Response.json({ ok: false, message: t("settings.plugins.errors.staleCapabilityReview") } satisfies ActionData, { status: 409 });
|
||||
}
|
||||
const requestedCapabilities = missingCapabilities;
|
||||
const submittedDigest = form.get("capabilityReviewDigest");
|
||||
const expectedDigest = await createCapabilityReviewDigest(item, missingCapabilities);
|
||||
if (typeof submittedDigest !== "string" || submittedDigest !== expectedDigest) {
|
||||
return Response.json({ ok: false, message: t("settings.plugins.errors.staleCapabilityReview") } satisfies ActionData, { status: 409 });
|
||||
}
|
||||
await grantPluginCapabilities({
|
||||
tenantId,
|
||||
pluginId: item.pluginId,
|
||||
grants: requestedCapabilities.map((name) => ({
|
||||
capabilityName: name,
|
||||
scopeJson: declaredCapabilities.get(name)?.scope ?? null,
|
||||
})),
|
||||
actor,
|
||||
capabilityReviewDigest: submittedDigest,
|
||||
});
|
||||
if (op === "grantAndEnable") {
|
||||
await enablePlugin({ tenantId, pluginRowId, actor });
|
||||
}
|
||||
return Response.json({ ok: true } satisfies ActionData);
|
||||
}
|
||||
return Response.json({ ok: false, message: t("settings.plugins.errors.unknownOperation") } satisfies ActionData, { status: 400 });
|
||||
} catch (err) {
|
||||
if (err instanceof PluginLifecycleError) {
|
||||
return Response.json(
|
||||
{
|
||||
ok: false,
|
||||
message: t(`settings.plugins.errors.lifecycle.${err.code}`),
|
||||
} satisfies ActionData,
|
||||
{ status: getPluginLifecycleErrorStatus(err.code) }
|
||||
);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
|
||||
function getPluginLifecycleErrorStatus(code: PluginLifecycleError["code"]): number {
|
||||
if (
|
||||
code === "signature_key_not_trusted" ||
|
||||
code === "signature_key_mismatch" ||
|
||||
code === "binary_verification_failed" ||
|
||||
code === "capability_unavailable" ||
|
||||
code === "production_guard_violation"
|
||||
) {
|
||||
return 403;
|
||||
}
|
||||
if (code === "not_found" || code === "binary_not_found") {
|
||||
return 404;
|
||||
}
|
||||
if (code === "already_installed" || code === "capability_consent_missing") {
|
||||
return 409;
|
||||
}
|
||||
if (code === "tenant_install_limit_exceeded") {
|
||||
return 429;
|
||||
}
|
||||
return 400;
|
||||
}
|
||||
|
||||
function areCapabilityNameSetsEqual(left: ReadonlyArray<string>, right: ReadonlyArray<string>): boolean {
|
||||
if (left.length !== right.length) return false;
|
||||
const rightNames = new Set(right);
|
||||
return left.every((name) => rightNames.has(name));
|
||||
}
|
||||
|
||||
async function serializePluginItems(args: { tenantId: string; items: ReadonlyArray<PluginListItem> }): Promise<SerializedPluginItem[]> {
|
||||
const { createCapabilityReviewDigest: createDigest } = await import("~/modules/plugins/utils/capabilityReviewDigest");
|
||||
const grants = await listPluginCapabilityGrantsForPlugins({
|
||||
tenantId: args.tenantId,
|
||||
pluginIds: args.items.map((row) => row.pluginId),
|
||||
});
|
||||
const grantsByPluginId = new Map<string, typeof grants>();
|
||||
for (const grant of grants) {
|
||||
const pluginGrants = grantsByPluginId.get(grant.pluginId) ?? [];
|
||||
pluginGrants.push(grant);
|
||||
grantsByPluginId.set(grant.pluginId, pluginGrants);
|
||||
}
|
||||
|
||||
return Promise.all(
|
||||
args.items.map(async (row) => {
|
||||
const pluginGrants = grantsByPluginId.get(row.pluginId) ?? [];
|
||||
const grantByCapability = new Map(pluginGrants.map((grant) => [grant.capabilityName, grant]));
|
||||
const capabilities = getPluginConsentRequirements(row.manifest).map((capability) => {
|
||||
const grant = grantByCapability.get(capability.name);
|
||||
const granted = Boolean(grant) && isCapabilityGrantScopeSatisfied({ declaredScope: capability.scope ?? null, grantedScopeJson: grant?.scopeJson });
|
||||
return {
|
||||
name: capability.name,
|
||||
scope: capability.scope ?? null,
|
||||
granted,
|
||||
grantedAt: grant?.grantedAt.toISOString() ?? null,
|
||||
grantedScopeJson: grant?.scopeJson ?? null,
|
||||
metadata: serializePluginConsentMetadata(capability.name),
|
||||
};
|
||||
});
|
||||
const missingCapabilities = capabilities.filter((capability) => !capability.granted).map((capability) => capability.name);
|
||||
return {
|
||||
id: row.id,
|
||||
pluginId: row.pluginId,
|
||||
version: row.version,
|
||||
enabled: row.enabled,
|
||||
capabilities,
|
||||
missingCapabilities,
|
||||
capabilityReviewDigest: createDigest(row, missingCapabilities),
|
||||
hasSettingsUi: Boolean(row.manifest.ui?.settings?.entry),
|
||||
manifestJson: JSON.stringify(row.manifest, null, 2),
|
||||
installedAt: row.installedAt.toISOString(),
|
||||
};
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
function serializePluginConsentMetadata(name: string): SerializedPluginCapabilityMetadata | null {
|
||||
if (isPluginHookConsentName(name)) {
|
||||
const hookName = name.slice("hook:".length);
|
||||
return { risk: "medium", productionAvailable: isPluginHookProductionAvailable(hookName) };
|
||||
}
|
||||
const metadata = getPluginCapabilityMetadata(name);
|
||||
return metadata ? { risk: metadata.risk, productionAvailable: metadata.productionAvailable } : null;
|
||||
}
|
||||
|
||||
async function resolveActor(request: Request): Promise<PluginLifecycleActor> {
|
||||
const info = await getUserInfo(request);
|
||||
const user = info.userId ? await getUser(info.userId) : null;
|
||||
const ip = request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ?? request.headers.get("x-real-ip") ?? null;
|
||||
return { userId: info.userId ?? null, email: user?.email ?? null, ip };
|
||||
}
|
||||
|
||||
export default function TenantPluginsRoute() {
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const actionData = useActionData<ActionData>();
|
||||
const { t } = useTranslation();
|
||||
const rootData = useRootData();
|
||||
const formatters = useLocaleFormatters();
|
||||
const [expanded, setExpanded] = useState<string | null>(null);
|
||||
const [reviewingCapabilities, setReviewingCapabilities] = useState<string | null>(null);
|
||||
const [reviewedCapabilities, setReviewedCapabilities] = useState<ReadonlySet<string>>(() => new Set());
|
||||
|
||||
const getCapabilityReviewKey = (item: SerializedPluginItem, capabilityName: string) => `${item.id}:${item.capabilityReviewDigest}:${capabilityName}`;
|
||||
const denyCapabilityReview = (item: SerializedPluginItem) => {
|
||||
setReviewedCapabilities((current) => {
|
||||
const next = new Set(current);
|
||||
for (const capabilityName of item.missingCapabilities) {
|
||||
next.delete(getCapabilityReviewKey(item, capabilityName));
|
||||
}
|
||||
return next;
|
||||
});
|
||||
setReviewingCapabilities(null);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-6 p-6" data-ui-id="ui.plugins.page">
|
||||
<header>
|
||||
<h1 className="text-xl font-semibold">{t("settings.plugins.title")}</h1>
|
||||
<p className="text-sm text-gray-500">{t("settings.plugins.description", { tenantSlug: data.tenantSlug })}</p>
|
||||
</header>
|
||||
{actionData?.message ? (
|
||||
<div
|
||||
role="alert"
|
||||
className={`rounded border px-3 py-2 text-sm ${actionData.ok ? "border-green-300 bg-green-50 text-green-800" : "border-red-300 bg-red-50 text-red-800"}`}
|
||||
>
|
||||
{actionData.message}
|
||||
</div>
|
||||
) : null}
|
||||
{data.items.length === 0 ? (
|
||||
<p className="rounded border border-dashed border-gray-300 p-6 text-sm text-gray-500">{t("settings.plugins.empty")}</p>
|
||||
) : (
|
||||
<ul className="space-y-3" data-ui-id="ui.plugins.list">
|
||||
{data.items.map((item) => {
|
||||
const missingCapabilityNames = new Set(item.missingCapabilities);
|
||||
const capabilitiesToReview = item.capabilities.filter((capability) => missingCapabilityNames.has(capability.name));
|
||||
const unavailableCapabilitiesToReview = capabilitiesToReview.filter((capability) => capability.metadata?.productionAvailable === false);
|
||||
const reviewComplete =
|
||||
unavailableCapabilitiesToReview.length === 0 &&
|
||||
item.missingCapabilities.every((capabilityName) => reviewedCapabilities.has(getCapabilityReviewKey(item, capabilityName)));
|
||||
|
||||
return (
|
||||
<li
|
||||
key={item.id}
|
||||
className="rounded border border-gray-200 bg-white p-4 shadow-sm"
|
||||
data-ui-id="ui.plugins.item"
|
||||
data-plugin-row-id={item.id}
|
||||
data-plugin-id={item.pluginId}
|
||||
>
|
||||
<div className="flex flex-wrap items-center justify-between gap-3">
|
||||
<div>
|
||||
<h2 className="text-base font-medium">{item.pluginId}</h2>
|
||||
<p className="text-xs text-gray-500">
|
||||
{t("settings.plugins.versionInstalled", {
|
||||
version: item.version,
|
||||
date: formatters.date(item.installedAt, { dateStyle: "medium", timeStyle: "short" }),
|
||||
})}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex items-center gap-3">
|
||||
<span
|
||||
className={`rounded-full px-2 py-0.5 text-xs font-medium ${item.enabled ? "bg-green-100 text-green-800" : "bg-gray-100 text-gray-700"}`}
|
||||
>
|
||||
{item.enabled ? t("shared.enabled") : t("shared.disabled")}
|
||||
</span>
|
||||
{data.canManage ? (
|
||||
item.enabled ? (
|
||||
<Form method="post" className="inline" data-ui-id="ui.plugins.lifecycle.form">
|
||||
{rootData.csrf ? <input type="hidden" name="csrf" value={rootData.csrf} /> : null}
|
||||
<input type="hidden" name="id" value={item.id} />
|
||||
<input type="hidden" name="op" value="disable" />
|
||||
<button
|
||||
type="submit"
|
||||
className="rounded border border-gray-300 bg-white px-3 py-1 text-sm hover:bg-gray-50"
|
||||
data-ui-id="ui.plugins.lifecycle.button"
|
||||
>
|
||||
{t("shared.disable")}
|
||||
</button>
|
||||
</Form>
|
||||
) : item.missingCapabilities.length > 0 ? null : (
|
||||
<Form method="post" className="inline" data-ui-id="ui.plugins.lifecycle.form">
|
||||
{rootData.csrf ? <input type="hidden" name="csrf" value={rootData.csrf} /> : null}
|
||||
<input type="hidden" name="id" value={item.id} />
|
||||
<input type="hidden" name="op" value="enable" />
|
||||
<button
|
||||
type="submit"
|
||||
className="rounded border border-gray-300 bg-white px-3 py-1 text-sm hover:bg-gray-50"
|
||||
data-ui-id="ui.plugins.lifecycle.button"
|
||||
>
|
||||
{t("shared.enable")}
|
||||
</button>
|
||||
</Form>
|
||||
)
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-3 space-y-2">
|
||||
<p className="text-xs font-medium text-gray-500 uppercase">{t("settings.plugins.capabilities")}</p>
|
||||
{item.capabilities.length === 0 ? (
|
||||
<p className="text-xs text-gray-500">{t("settings.plugins.consent.none")}</p>
|
||||
) : (
|
||||
<ul className="space-y-2">
|
||||
{item.capabilities.map((capability) => (
|
||||
<li key={capability.name} className="rounded border border-gray-100 bg-gray-50 px-3 py-2">
|
||||
<div className="flex flex-wrap items-center justify-between gap-2">
|
||||
<span className="text-sm font-medium text-gray-900">{formatPluginCapabilityLabel(capability.name, t)}</span>
|
||||
<span
|
||||
className={`rounded-full px-2 py-0.5 text-xs font-medium ${capability.granted ? "bg-green-100 text-green-800" : "bg-amber-100 text-amber-800"}`}
|
||||
>
|
||||
{capability.granted ? t("settings.plugins.consent.granted") : t("settings.plugins.consent.missing")}
|
||||
</span>
|
||||
</div>
|
||||
<p className="mt-1 text-xs text-gray-500">{formatPluginCapabilityScope(capability.scope, t)}</p>
|
||||
{capability.granted && capability.grantedAt ? (
|
||||
<p className="mt-1 text-xs text-gray-500" data-ui-id="ui.plugins.capability.grantMeta">
|
||||
{t("settings.plugins.consent.grantedAt", {
|
||||
date: formatters.date(capability.grantedAt, { dateStyle: "medium", timeStyle: "short" }),
|
||||
})}
|
||||
</p>
|
||||
) : null}
|
||||
{data.canManage && capability.granted ? (
|
||||
<Form method="post" className="mt-2" data-ui-id="ui.plugins.capability.revoke.form">
|
||||
{rootData.csrf ? <input type="hidden" name="csrf" value={rootData.csrf} /> : null}
|
||||
<input type="hidden" name="id" value={item.id} />
|
||||
<input type="hidden" name="pluginId" value={item.pluginId} />
|
||||
<input type="hidden" name="op" value="revoke" />
|
||||
<input type="hidden" name="capabilityNames[]" value={capability.name} />
|
||||
<button
|
||||
type="submit"
|
||||
className="text-xs font-medium text-red-700 hover:underline"
|
||||
data-ui-id="ui.plugins.capability.revoke.button"
|
||||
>
|
||||
{t("settings.plugins.consent.revoke")}
|
||||
</button>
|
||||
<span className="ml-2 text-xs text-gray-500">{t("settings.plugins.consent.revokeDisables")}</span>
|
||||
</Form>
|
||||
) : null}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
{data.canManage && item.missingCapabilities.length > 0 ? (
|
||||
<div className="rounded border border-amber-200 bg-amber-50 p-3">
|
||||
<button
|
||||
type="button"
|
||||
aria-expanded={reviewingCapabilities === item.id}
|
||||
aria-controls={`plugin-capability-review-${item.id}`}
|
||||
onClick={() => setReviewingCapabilities(reviewingCapabilities === item.id ? null : item.id)}
|
||||
className="rounded border border-amber-300 bg-white px-3 py-1 text-sm font-medium text-amber-900 hover:bg-amber-100"
|
||||
data-ui-id="ui.plugins.capabilityReview.toggle"
|
||||
>
|
||||
{t("settings.plugins.consent.reviewCapabilities")}
|
||||
</button>
|
||||
<span className="ml-3 text-xs text-gray-500">{t("settings.plugins.consent.description")}</span>
|
||||
{reviewingCapabilities === item.id ? (
|
||||
<Form
|
||||
method="post"
|
||||
id={`plugin-capability-review-${item.id}`}
|
||||
className="mt-3 space-y-3"
|
||||
aria-label={t("settings.plugins.consent.reviewFormLabel", { pluginId: item.pluginId })}
|
||||
data-ui-id="ui.plugins.capabilityReview.form"
|
||||
>
|
||||
{rootData.csrf ? <input type="hidden" name="csrf" value={rootData.csrf} /> : null}
|
||||
<input type="hidden" name="id" value={item.id} />
|
||||
<input type="hidden" name="op" value={item.enabled ? "grant" : "grantAndEnable"} />
|
||||
<input type="hidden" name="capabilityReviewDigest" value={item.capabilityReviewDigest} />
|
||||
<div>
|
||||
<p className="text-sm font-medium text-amber-950">{t("settings.plugins.consent.reviewTitle", { pluginId: item.pluginId })}</p>
|
||||
<p className="mt-1 text-xs text-amber-900">{t("settings.plugins.consent.reviewDescription", { version: item.version })}</p>
|
||||
</div>
|
||||
<ul className="space-y-2">
|
||||
{capabilitiesToReview.map((capability) => {
|
||||
const reviewKey = getCapabilityReviewKey(item, capability.name);
|
||||
const label = formatPluginCapabilityLabel(capability.name, t);
|
||||
const unavailable = capability.metadata?.productionAvailable === false;
|
||||
return (
|
||||
<li key={capability.name} className="rounded border border-amber-200 bg-white px-3 py-2">
|
||||
<label className="flex items-start gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
name="capabilities[]"
|
||||
value={capability.name}
|
||||
disabled={unavailable}
|
||||
checked={reviewedCapabilities.has(reviewKey)}
|
||||
onChange={(event) => {
|
||||
const isChecked = event.currentTarget.checked;
|
||||
setReviewedCapabilities((current) => {
|
||||
const next = new Set(current);
|
||||
if (isChecked) {
|
||||
next.add(reviewKey);
|
||||
} else {
|
||||
next.delete(reviewKey);
|
||||
}
|
||||
return next;
|
||||
});
|
||||
}}
|
||||
className="mt-0.5 h-3.5 w-3.5 rounded border-gray-300 disabled:cursor-not-allowed disabled:opacity-50"
|
||||
data-ui-id="ui.plugins.capabilityReview.checkbox"
|
||||
/>
|
||||
<span className="text-sm font-medium text-gray-900">
|
||||
{t("settings.plugins.consent.reviewCapability", { capability: label })}
|
||||
</span>
|
||||
</label>
|
||||
{capability.metadata ? (
|
||||
<div className="mt-2 flex flex-wrap gap-2">
|
||||
<span
|
||||
className={`rounded-full px-2 py-0.5 text-xs font-medium ${getPluginCapabilityRiskClassName(capability.metadata.risk)}`}
|
||||
data-ui-id="ui.plugins.capabilityReview.risk"
|
||||
>
|
||||
{t(`settings.plugins.consent.risk.${capability.metadata.risk}`)}
|
||||
</span>
|
||||
<span
|
||||
className={`rounded-full px-2 py-0.5 text-xs font-medium ${
|
||||
capability.metadata.productionAvailable ? "bg-green-100 text-green-800" : "bg-red-100 text-red-800"
|
||||
}`}
|
||||
data-ui-id="ui.plugins.capabilityReview.availability"
|
||||
>
|
||||
{capability.metadata.productionAvailable
|
||||
? t("settings.plugins.consent.available")
|
||||
: t("settings.plugins.consent.unavailable")}
|
||||
</span>
|
||||
</div>
|
||||
) : null}
|
||||
{unavailable ? (
|
||||
<p className="mt-2 text-xs text-red-700" data-ui-id="ui.plugins.capabilityReview.unavailableReason">
|
||||
{t("settings.plugins.consent.unavailableDescription")}
|
||||
</p>
|
||||
) : null}
|
||||
<p className="mt-1 text-xs text-gray-500">{formatPluginCapabilityScope(capability.scope, t)}</p>
|
||||
<code
|
||||
className="mt-2 inline-flex rounded border border-gray-200 bg-gray-50 px-1.5 py-0.5 text-xs text-gray-700"
|
||||
data-ui-id="ui.plugins.capabilityReview.grantName"
|
||||
>
|
||||
{capability.name}
|
||||
</code>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
<input type="hidden" name="capabilitiesReviewed" value={reviewComplete ? "yes" : "no"} />
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<button
|
||||
type="submit"
|
||||
disabled={!reviewComplete}
|
||||
className="rounded border border-amber-300 bg-amber-100 px-3 py-1 text-sm font-medium text-amber-950 hover:bg-amber-200 disabled:cursor-not-allowed disabled:opacity-50"
|
||||
data-ui-id="ui.plugins.capabilityReview.button"
|
||||
>
|
||||
{item.enabled ? t("settings.plugins.consent.approve") : t("settings.plugins.consent.approveAndEnable")}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => denyCapabilityReview(item)}
|
||||
className="rounded border border-gray-300 bg-white px-3 py-1 text-sm font-medium text-gray-700 hover:bg-gray-50"
|
||||
data-ui-id="ui.plugins.capabilityReview.deny"
|
||||
>
|
||||
{t("shared.deny")}
|
||||
</button>
|
||||
</div>
|
||||
</Form>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
aria-expanded={expanded === item.id}
|
||||
aria-controls={`plugin-manifest-${item.id}`}
|
||||
onClick={() => setExpanded(expanded === item.id ? null : item.id)}
|
||||
className="mt-2 text-xs text-blue-600 hover:underline"
|
||||
data-ui-id="ui.plugins.manifest.toggle"
|
||||
>
|
||||
{expanded === item.id ? t("settings.plugins.hideManifest") : t("settings.plugins.viewManifest")}
|
||||
</button>
|
||||
{item.enabled && item.hasSettingsUi ? (
|
||||
<Link
|
||||
to={appPath({ tenantSlug: data.tenantSlug, path: `settings/plugins/${item.id}` })}
|
||||
className="ml-4 text-xs font-medium text-blue-600 hover:underline"
|
||||
data-ui-id="ui.plugins.settings.open"
|
||||
>
|
||||
{t("settings.plugins.openSettings")}
|
||||
</Link>
|
||||
) : null}
|
||||
{data.canAudit ? (
|
||||
<Link
|
||||
to={appPath({ tenantSlug: data.tenantSlug, path: `settings/plugins/${item.id}/audit` })}
|
||||
className="ml-4 text-xs font-medium text-blue-600 hover:underline"
|
||||
data-ui-id="ui.plugins.audit.open"
|
||||
>
|
||||
{t("settings.plugins.viewAudit")}
|
||||
</Link>
|
||||
) : null}
|
||||
{expanded === item.id ? (
|
||||
<pre
|
||||
id={`plugin-manifest-${item.id}`}
|
||||
// Scrollable regions must be keyboard-focusable (axe
|
||||
// scrollable-region-focusable) so keyboard users can scroll the
|
||||
// manifest content.
|
||||
tabIndex={0}
|
||||
className="mt-2 overflow-auto rounded bg-gray-50 p-3 text-xs text-gray-800"
|
||||
data-ui-id="ui.plugins.manifest.panel"
|
||||
>
|
||||
{item.manifestJson}
|
||||
</pre>
|
||||
) : null}
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function getPluginCapabilityRiskClassName(risk: PluginCapabilityRisk): string {
|
||||
switch (risk) {
|
||||
case "low":
|
||||
return "bg-green-100 text-green-800";
|
||||
case "medium":
|
||||
return "bg-blue-100 text-blue-800";
|
||||
case "high":
|
||||
return "bg-amber-100 text-amber-800";
|
||||
case "critical":
|
||||
return "bg-red-100 text-red-800";
|
||||
}
|
||||
}
|
||||
|
||||
function formatPluginCapabilityScope(scope: PluginCapability["scope"] | null, t: (key: string, options?: Record<string, unknown>) => string) {
|
||||
if (!scope) {
|
||||
return t("settings.plugins.consent.noScope");
|
||||
}
|
||||
const parts = [
|
||||
scope.allowedHosts?.length ? t("settings.plugins.consent.allowedHosts", { hosts: scope.allowedHosts.join(", ") }) : null,
|
||||
scope.keyPattern ? t("settings.plugins.consent.keyPattern", { pattern: scope.keyPattern }) : null,
|
||||
scope.tablePrefix ? t("settings.plugins.consent.tablePrefix", { prefix: scope.tablePrefix }) : null,
|
||||
].filter(Boolean);
|
||||
return parts.length > 0 ? parts.join(" / ") : t("settings.plugins.consent.noScope");
|
||||
}
|
||||
Reference in new issue
Block a user