penpot/backend/resources/admin/js/pages/error-reports.js
Andrey Antukh 8853445951 🎉 Add the superuser admin panel
Operators get a full instance administration panel behind a new
"superuser" access rule. The :app.auth/superusers service resolves
the configured :superusers emails to profile ids once at startup,
so guarded calls are a plain contains? with no query. A token
carrying "superuser" as an operator-granted scope also passes, for
automation without a session; tokens are created with empty perms,
so this opens nothing by itself.

The check is a branch of wrap-authentication, after auth and
before params validation, so outsiders get 403
:superuser-required without leaking expected params. Other token
scopes keep their exact meaning. get-profile and the login
response report the same answer as :is-superuser; the team-scoped
:is-admin is untouched. On devenv any authenticated profile
passes, as the /dbg gate does. The :admins config becomes
:superusers (PENPOT_SUPERUSERS) with no compatibility layer:
operators must rename it or instance-admin access fails closed.
Quote notifications go to the same set.

Admin operations live in app.rpc.admin.* behind
/api/admin/methods, with the same auth wrapping as main but
excluded from the main API docs. Profiles can be listed,
inspected, blocked, unblocked, deleted and restored, teams listed
with members and features, projects and files listed with validate
and repair, error reports listed and shared through JWE links,
and the virtual clock read and set.

Delete and restore run as durable jobs on the unified job table:
the command stamps the object's own row inline so it reads back
instantly, then a delete-object or restore-object job walks the
rest of the tree through app.features.object-cascade. Listing
uses created-at cursors over the new 0157 indexes.

The panel itself is a static SPA served under /admin (public like
any asset; the JS gate reads :is-superuser from get-profile)
with dashboard, users, teams, projects, files and error-report
pages. Devenv points /admin at the backend.

Covered by backend-tests rpc-superuser-test plus
rpc-admin-{users,teams,project,file,delete,restore,misc,
error-reports,error-share} and http-admin tests, with green
regressions on profile, auth and quotes.

Relates to #12015

AI-assisted-by: muse-spark-1.3-contributor
2026-10-21 12:59:22 +02:00

174 lines
5.7 KiB
JavaScript

// Error reports list: source filter with pagination. Every value
// is painted as text; source badges use a fixed class map, never the
// raw value. JSON responses use camelCase (`nextSince`, …).
// The `hint` search is gone from the panel on purpose (no index
// serves it at PRO scale); a bookmarked `?hint=` is ignored and
// shows everything.
import { rpc } from "../api.js";
import { formatDate } from "../components/date.js";
import { renderHeader } from "../components/header.js";
import { renderTable } from "../components/table.js";
import { showToast } from "../components/toast.js";
import { readQuery, writeQuery, detailUrl } from "../url.js";
const SOURCES = ["logging", "audit-log", "rlimit", "legacy-v1", "legacy-v2"];
const PAGE_SIZE = 25;
const SOURCE_BADGES = {
logging: "admin-badge-source-logging",
"audit-log": "admin-badge-source-audit",
rlimit: "admin-badge-source-rlimit",
"legacy-v1": "admin-badge-source-legacy",
"legacy-v2": "admin-badge-source-legacy",
};
const COLUMNS = [
{ key: "createdAt", label: "Date", class: "admin-cell-date" },
{ key: "source", label: "Source", class: "admin-cell-source" },
{ key: "kind", label: "Kind", class: "admin-cell-kind" },
{ key: "hint", label: "Hint", class: "admin-cell-hint" },
];
function sourceCell(source) {
const badge = document.createElement("span");
badge.className = "admin-badge " + (SOURCE_BADGES[source] || "admin-badge-source-legacy");
badge.textContent = source;
return badge;
}
export function errorReportsPage(root, { onNavigate }) {
const fromUrl = readQuery(["source"]);
const state = {
source: SOURCES.includes(fromUrl.source) ? fromUrl.source : "",
items: [],
nextSince: null,
nextId: null,
loading: false,
loadedOnce: false,
};
root.appendChild(renderHeader("Error reports"));
const controls = document.createElement("div");
controls.className = "admin-controls";
const sourceSelect = document.createElement("select");
sourceSelect.className = "admin-select";
sourceSelect.setAttribute("aria-label", "Filter by source");
const allOption = document.createElement("option");
allOption.value = "";
allOption.textContent = "All sources";
sourceSelect.appendChild(allOption);
for (const source of SOURCES) {
const option = document.createElement("option");
option.value = source;
option.textContent = source;
sourceSelect.appendChild(option);
}
sourceSelect.value = state.source;
controls.appendChild(sourceSelect);
const searchButton = document.createElement("button");
searchButton.className = "admin-button";
searchButton.textContent = "Search";
searchButton.addEventListener("click", () => {
state.source = sourceSelect.value;
state.nextSince = null;
state.nextId = null;
load();
});
controls.appendChild(searchButton);
const clearButton = document.createElement("button");
clearButton.className = "admin-button admin-button-ghost";
clearButton.textContent = "Clear";
clearButton.addEventListener("click", () => {
sourceSelect.value = "";
state.source = "";
state.nextSince = null;
state.nextId = null;
load();
});
controls.appendChild(clearButton);
root.appendChild(controls);
const countLine = document.createElement("p");
countLine.className = "admin-count";
root.appendChild(countLine);
const tableWrap = document.createElement("div");
root.appendChild(tableWrap);
const pager = document.createElement("div");
pager.className = "admin-pager";
const nextButton = document.createElement("button");
nextButton.className = "admin-button";
nextButton.textContent = "Next page";
nextButton.addEventListener("click", () => load({ append: true }));
pager.appendChild(nextButton);
root.appendChild(pager);
function paint() {
tableWrap.replaceChildren();
if (state.loading && !state.loadedOnce) {
countLine.textContent = "Loading…";
} else if (state.items.length === 0) {
countLine.textContent = "No error reports match the current filters.";
} else {
countLine.textContent =
`Showing ${state.items.length} report` + (state.items.length === 1 ? "" : "s") +
(state.nextSince === null ? " (all)" : " — more available");
}
const rows = state.items.map((item) => ({
...item,
createdAt: formatDate(item.createdAt),
}));
const table = renderTable(COLUMNS, rows);
const bodyRows = table.querySelector("tbody").rows;
for (let i = 0; i < bodyRows.length; i++) {
const row = bodyRows[i];
const item = state.items[i];
row.cells[1].replaceChildren(sourceCell(item.source));
row.addEventListener("click", () =>
onNavigate(detailUrl("error-report", item.id)));
}
tableWrap.appendChild(table);
nextButton.disabled = state.loading || state.nextSince === null;
searchButton.disabled = state.loading;
clearButton.disabled = state.loading;
pager.style.display =
state.items.length === 0 || state.nextSince === null ? "none" : "";
}
async function load({ append = false } = {}) {
state.loading = true;
paint();
writeQuery({ source: state.source });
const params = { limit: PAGE_SIZE };
if (state.source) {
params.source = state.source;
}
if (append && state.nextSince) {
params.since = state.nextSince;
params.sinceId = state.nextId;
}
try {
const data = await rpc("get-error-reports", params);
state.items = append ? state.items.concat(data.items) : data.items;
state.nextSince = data.nextSince ?? null;
state.nextId = data.nextId ?? null;
state.loadedOnce = true;
} catch {
showToast("Could not load error reports.", "error");
} finally {
state.loading = false;
paint();
}
}
paint();
load();
}