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

149 lines
4.7 KiB
JavaScript

// Bulk delete by email: paste a list, preview the count, confirm
// the alert, and run delete-profiles. The answer paints deleted,
// not-found, and skipped-self groups separately.
// Everything is painted as text.
import { rpc } from "../api.js";
import { renderHeader } from "../components/header.js";
import { showToast } from "../components/toast.js";
import { backUrl } from "../url.js";
const MAX_EMAILS = 100;
function parseEmails(raw) {
return raw
.split(/[\n,]+/)
.map((email) => email.trim())
.filter((email) => email !== "");
}
export function usersDeletePage(root, { onNavigate }) {
root.appendChild(renderHeader("Delete users by email"));
const back = document.createElement("button");
back.className = "admin-button admin-button-ghost";
back.textContent = "Back to list";
back.addEventListener("click", () => onNavigate(backUrl("?screen=users")));
root.appendChild(back);
const hint = document.createElement("p");
hint.className = "admin-count";
hint.textContent =
`Paste up to ${MAX_EMAILS} emails, one per line or comma separated. ` +
"Matches are marked for deletion at once; unknown emails are reported, not deleted.";
root.appendChild(hint);
const area = document.createElement("textarea");
area.className = "admin-textarea";
area.placeholder = "user1@example.com\nuser2@example.com";
area.setAttribute("aria-label", "Emails to delete");
root.appendChild(area);
const countLine = document.createElement("p");
countLine.className = "admin-count";
countLine.textContent = "Paste at least one email.";
root.appendChild(countLine);
const confirmWrap = document.createElement("div");
confirmWrap.className = "admin-confirm";
const confirm = document.createElement("button");
confirm.className = "admin-button admin-button-danger";
confirm.textContent = "Delete";
confirm.title = "Delete the listed users. This cannot be undone.";
confirm.disabled = true;
confirmWrap.appendChild(confirm);
root.appendChild(confirmWrap);
const body = document.createElement("div");
root.appendChild(body);
function emailCount() {
return parseEmails(area.value).length;
}
function refresh() {
const count = emailCount();
confirm.textContent = count > 0 ? `Delete ${count}` : "Delete";
if (count === 0) {
countLine.textContent = "Paste at least one email.";
confirm.disabled = true;
return;
}
if (count > MAX_EMAILS) {
countLine.textContent = `Too many emails: ${count} (max ${MAX_EMAILS}).`;
confirm.disabled = true;
return;
}
countLine.textContent =
`${count} email` + (count === 1 ? "" : "s") + ". This cannot be undone.";
confirm.disabled = false;
}
area.addEventListener("input", refresh);
confirm.addEventListener("click", async () => {
const emails = parseEmails(area.value);
if (!window.confirm(
`Delete ${emails.length} user` + (emails.length === 1 ? "" : "s") +
"? Unknown emails are reported, not deleted. This cannot be undone.")) {
return;
}
confirm.disabled = true;
try {
const data = await rpc("delete-profiles", { emails });
body.replaceChildren();
body.appendChild(resultBlock(data));
} catch {
showToast("Could not delete the users.", "error");
refresh();
}
});
refresh();
function resultBlock(data) {
const wrap = document.createElement("div");
const summary = document.createElement("p");
summary.className = "admin-count";
summary.textContent =
`Deleted ${data.deleted.length} of ${data.total}. ` +
`${data.notFound.length} not found, ${data.skippedSelf.length} skipped (own account).`;
wrap.appendChild(summary);
const groups = [
["Deleted", data.deleted],
["Not found", data.notFound],
["Skipped (own account)", data.skippedSelf],
];
for (const [title, items] of groups) {
if (items.length === 0) {
continue;
}
const section = document.createElement("section");
section.className = "admin-block";
const heading = document.createElement("h2");
heading.textContent = `${title} (${items.length})`;
section.appendChild(heading);
const list = document.createElement("ul");
list.className = "admin-list";
for (const item of items) {
const entry = document.createElement("li");
entry.textContent = String(item);
list.appendChild(entry);
}
section.appendChild(list);
wrap.appendChild(section);
}
const done = document.createElement("button");
done.className = "admin-button admin-button-ghost";
done.textContent = "Back to list";
done.addEventListener("click", () => onNavigate(backUrl("?screen=users")));
wrap.appendChild(done);
return wrap;
}
}