penpot/backend/resources/admin/js/pages/virtual-clock.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

123 lines
3.6 KiB
JavaScript

// Virtual clock: show the per-profile time offset and set or
// reset it. The offset only affects the current profile's session
// and is a test aid, never a production control (production
// refuses changes). Every value is painted as text.
import { rpc } from "../api.js";
import { formatDate } from "../components/date.js";
import { renderHeader } from "../components/header.js";
import { showToast } from "../components/toast.js";
function formatOffset(millis) {
if (millis === null || millis === undefined) {
return "none";
}
const totalSeconds = Math.round(millis / 1000);
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = totalSeconds % 60;
const parts = [];
if (hours) {
parts.push(`${hours}h`);
}
if (minutes) {
parts.push(`${minutes}m`);
}
if (!parts.length || seconds) {
parts.push(`${seconds}s`);
}
return parts.join("");
}
export function virtualClockPage(root) {
root.appendChild(renderHeader("Virtual clock"));
const hint = document.createElement("p");
hint.className = "admin-count";
hint.textContent =
"Shift this session's clock for testing (for example 1h, 90m, 2h30m). " +
"Reset returns to real time. Production refuses changes.";
root.appendChild(hint);
const status = document.createElement("p");
status.className = "admin-count admin-count-spaced";
status.textContent = "Loading…";
root.appendChild(status);
const controls = document.createElement("div");
controls.className = "admin-controls";
const offsetInput = document.createElement("input");
offsetInput.className = "admin-input";
offsetInput.type = "text";
offsetInput.placeholder = "Offset, e.g. 1h";
offsetInput.setAttribute("aria-label", "Clock offset");
offsetInput.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
setButton.click();
}
});
controls.appendChild(offsetInput);
const setButton = document.createElement("button");
setButton.className = "admin-button";
setButton.textContent = "Set offset";
setButton.addEventListener("click", async () => {
const offset = offsetInput.value.trim();
if (!offset) {
showToast("Type an offset first.", "error");
return;
}
if (!window.confirm(`Shift this session's clock by ${offset}?`)) {
return;
}
setButton.disabled = true;
try {
const data = await rpc("set-virtual-clock", { offset });
paint(data);
showToast("Clock offset set.");
} catch {
showToast("Could not set the clock offset.", "error");
} finally {
setButton.disabled = false;
}
});
controls.appendChild(setButton);
const resetButton = document.createElement("button");
resetButton.className = "admin-button admin-button-ghost";
resetButton.textContent = "Reset";
resetButton.addEventListener("click", async () => {
resetButton.disabled = true;
try {
const data = await rpc("set-virtual-clock", { reset: true });
offsetInput.value = "";
paint(data);
showToast("Clock reset to real time.");
} catch {
showToast("Could not reset the clock.", "error");
} finally {
resetButton.disabled = false;
}
});
controls.appendChild(resetButton);
root.appendChild(controls);
function paint(data) {
status.textContent =
`Offset: ${formatOffset(data.offsetMillis)} · ` +
`Time: ${formatDate(data.now)} · ` +
`Clock: ${data.clock}`;
}
async function load() {
try {
paint(await rpc("get-virtual-clock"));
} catch {
status.textContent = "Could not load the clock.";
}
}
load();
}