Events 3.3.0, Accounts 1.3.0, contracts 1.11.0: Accounts looks like Events, backups; sign-out and upload fixes
- Accounts: account and admin pages in Cross Events' layout (top bar, sidebar, a page per section, dark footer); quick links to the church's sites (GET /me/apps); nightly pg_dump backups with Admin -> Backups (run, keep, download, audited; heartbeat lastBackupAt); a fuller built-in privacy notice with headings and bullets; a fixed favicon. /backups/ ignored (anchored, so src/backups is kept). - Contracts 1.11.0: people.removeDemo (only @example.com, never an admin). - Events: uploads served from the folder they're saved in (a backend started outside backend/ answered 404 for a logo it had just saved); Branding checks the file loads before saving; logout waits for the sign-out call so Accounts signs out too; set-demo removes the last demo's people from Accounts. Tests: Accounts 62 + e2e 8, Events 782, contracts 225; npm run check passes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,63 +1,70 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import React, { useCallback, useState } from "react";
|
||||
import { Button, Card, Checkbox, Field, Heading, Notice, Shell } from "@crosscode/ui";
|
||||
import { useBranding } from "@/components/BrandingProvider";
|
||||
import { DatabaseBackup, Download } from "lucide-react";
|
||||
import { Button, Checkbox, Field, Notice } from "@crosscode/ui";
|
||||
import { AppShell, PageTitle, ShellLoading, type ShellUser } from "@/components/AppShell";
|
||||
import { NoticeText } from "@/components/NoticeText";
|
||||
import { Card, Heading } from "@/components/Panel";
|
||||
import { call, selfSignIn } from "@/lib/api";
|
||||
import { useExternalEffect } from "@/lib/hooks";
|
||||
import { ADMIN_SECTIONS, type AdminSection } from "@/lib/sections";
|
||||
|
||||
const TABS = [
|
||||
["people", "People"], ["deletions", "Deletion requests"], ["church", "Church profile"], ["linked", "Linked products"],
|
||||
["signin", "Sign-in settings"], ["messages", "Email and WhatsApp"], ["notice", "Privacy notice"], ["licence", "Licence"], ["audit", "Audit log"],
|
||||
] as const;
|
||||
type Tab = (typeof TABS)[number][0];
|
||||
|
||||
const when = (iso: string | null | undefined) => (iso ? new Date(iso).toLocaleString("en-ZA", { day: "numeric", month: "short", year: "numeric", hour: "2-digit", minute: "2-digit" }) : "—");
|
||||
|
||||
type Flash = (tone: "success" | "error", text: string) => void;
|
||||
|
||||
/** Accounts' admin (docs/ACCOUNTS-1.0-PLAN.md §8). Admins only, signed in with their second step. */
|
||||
export function Admin() {
|
||||
const branding = useBranding();
|
||||
const [tab, setTab] = useState<Tab>("people");
|
||||
/**
|
||||
* Accounts' admin (docs/ACCOUNTS-1.0-PLAN.md §8). Admins only, signed in with their second step.
|
||||
* 1.3.0: one section per address, down the side (as Cross Events' settings), and Backups.
|
||||
*/
|
||||
export function Admin({ section = "people" }: { section?: AdminSection }) {
|
||||
const s = ADMIN_SECTIONS[section] ?? ADMIN_SECTIONS.people;
|
||||
const [me, setMe] = useState<ShellUser | null>(null);
|
||||
const [allowed, setAllowed] = useState<boolean | null>(null);
|
||||
const [msg, setMsg] = useState<{ tone: "success" | "error"; text: string } | null>(null);
|
||||
|
||||
useExternalEffect(() => {
|
||||
let alive = true;
|
||||
call("GET", "/admin/licence").then((r) => {
|
||||
Promise.all([call<ShellUser>("GET", "/me"), call("GET", "/admin/licence")]).then(([who, r]) => {
|
||||
if (!alive) return;
|
||||
if (r.status === 401) return window.location.assign(selfSignIn("/admin"));
|
||||
if (!r.ok && r.error.code === "MFA_REQUIRED") return window.location.assign(selfSignIn("/admin", "&acr=mfa"));
|
||||
if (who.status === 401 || r.status === 401) return window.location.assign(selfSignIn(s.path));
|
||||
if (!r.ok && r.error.code === "MFA_REQUIRED") return window.location.assign(selfSignIn(s.path, "&acr=mfa"));
|
||||
if (who.ok) setMe(who.data);
|
||||
setAllowed(r.ok);
|
||||
});
|
||||
return () => { alive = false; };
|
||||
}, []);
|
||||
}, [s.path]);
|
||||
|
||||
const flash: Flash = (tone, text) => setMsg({ tone, text });
|
||||
if (allowed === null) return <Shell branding={branding} wide><p className="text-gray-600">Loading…</p></Shell>;
|
||||
if (!allowed) return <Shell branding={branding}><Card><Notice tone="error">Only Accounts admins can open this page.</Notice><a href="/account" className="text-brand underline">Your account</a></Card></Shell>;
|
||||
if (allowed === null || !me) return <ShellLoading />;
|
||||
if (!allowed) {
|
||||
return (
|
||||
<AppShell me={me} active={s.path}>
|
||||
<div className="max-w-xl"><Card><Notice tone="error">Only Accounts admins can open this page.</Notice><Link href="/account" className="text-brand underline">Your account</Link></Card></div>
|
||||
</AppShell>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Shell branding={branding} wide aside={<a href="/account" className="text-sm text-brand underline">Your account</a>}>
|
||||
<h1 className="mb-4 text-2xl font-semibold">Accounts admin</h1>
|
||||
<nav className="mb-6 flex flex-wrap gap-2" aria-label="Admin sections">
|
||||
{TABS.map(([id, label]) => (
|
||||
<button key={id} type="button" onClick={() => { setTab(id); setMsg(null); }} aria-current={tab === id ? "page" : undefined}
|
||||
className={`rounded-full px-3 py-1.5 text-sm ${tab === id ? "bg-brand text-white" : "border bg-white text-gray-700"}`}>{label}</button>
|
||||
))}
|
||||
</nav>
|
||||
<AppShell me={me} active={s.path}>
|
||||
<PageTitle sub={s.sub}>{s.title}</PageTitle>
|
||||
{msg && <Notice tone={msg.tone}>{msg.text}</Notice>}
|
||||
{tab === "people" && <People flash={flash} />}
|
||||
{tab === "deletions" && <Deletions flash={flash} />}
|
||||
{tab === "church" && <Church flash={flash} />}
|
||||
{tab === "linked" && <Linked flash={flash} />}
|
||||
{tab === "signin" && <Settings flash={flash} keys={["signup_enabled", "two_factor_required_for_admins", "session_days"]} />}
|
||||
{tab === "messages" && <Settings flash={flash} keys={["smtp_host", "smtp_port", "smtp_user", "smtp_pass", "smtp_from", "smtp_secure", "wawp_access_token", "wawp_instance_id"]} />}
|
||||
{tab === "notice" && <NoticeTab flash={flash} />}
|
||||
{tab === "licence" && <Licence flash={flash} />}
|
||||
{tab === "audit" && <Audit />}
|
||||
</Shell>
|
||||
{section === "people" && <People flash={flash} />}
|
||||
<div className="max-w-3xl">
|
||||
{section === "deletions" && <Deletions flash={flash} />}
|
||||
{section === "church" && <Church flash={flash} />}
|
||||
{section === "linked" && <Linked flash={flash} />}
|
||||
{section === "signin" && <Settings flash={flash} keys={["signup_enabled", "two_factor_required_for_admins", "session_days"]} />}
|
||||
{section === "messages" && <Settings flash={flash} keys={["smtp_host", "smtp_port", "smtp_user", "smtp_pass", "smtp_from", "smtp_secure", "wawp_access_token", "wawp_instance_id"]} />}
|
||||
{section === "notice" && <NoticeTab flash={flash} />}
|
||||
{section === "backups" && <Backups flash={flash} />}
|
||||
{section === "licence" && <Licence flash={flash} />}
|
||||
{section === "audit" && <Audit />}
|
||||
</div>
|
||||
</AppShell>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -292,30 +299,48 @@ function Settings({ flash, keys }: { flash: Flash; keys: string[] }) {
|
||||
|
||||
// ─── Privacy notice, licence, audit ───────────────────────────────────────────
|
||||
|
||||
type NoticeVersion = { version: number; title: string; body: string };
|
||||
|
||||
function NoticeTab({ flash }: { flash: Flash }) {
|
||||
const [current, setCurrent] = useState<{ version: number; title: string; body: string } | null>(null);
|
||||
const [data, setData] = useState<{ current: NoticeVersion; builtIn: NoticeVersion | null } | null>(null);
|
||||
const [draft, setDraft] = useState<{ title: string; body: string } | null>(null);
|
||||
const load = useCallback(async () => {
|
||||
const r = await call<{ current: { version: number; title: string; body: string } }>("GET", "/admin/notices");
|
||||
if (r.ok) setCurrent(r.data.current);
|
||||
const r = await call<{ current: NoticeVersion; builtIn?: NoticeVersion }>("GET", "/admin/notices");
|
||||
if (r.ok) { setData({ current: r.data.current, builtIn: r.data.builtIn ?? null }); setDraft({ title: r.data.current.title, body: r.data.current.body }); }
|
||||
}, []);
|
||||
useExternalEffect(() => { void load(); }, [load]);
|
||||
if (!current) return <p className="text-gray-600">Loading…</p>;
|
||||
if (!data || !draft) return <p className="text-gray-600">Loading…</p>;
|
||||
const { current, builtIn } = data;
|
||||
const isBuiltIn = current.version === 1;
|
||||
return (
|
||||
<Card>
|
||||
<Heading sub={`Version ${current.version}. Publishing a new version asks everyone to read it the next time they sign in.`}>Privacy notice</Heading>
|
||||
<form onSubmit={async (e) => {
|
||||
e.preventDefault();
|
||||
if (!window.confirm("Publish this as a new version? Everyone will be asked to read it when they next sign in.")) return;
|
||||
const f = new FormData(e.currentTarget);
|
||||
const r = await call("POST", "/admin/notices", { title: f.get("title"), body: f.get("body") });
|
||||
if (r.ok) { flash("success", "Published."); void load(); } else flash("error", r.error.message);
|
||||
}}>
|
||||
<Field label="Title" name="title" defaultValue={current.title} required />
|
||||
<label htmlFor="notice-body" className="mb-1 block text-sm font-medium">Text</label>
|
||||
<textarea id="notice-body" name="body" defaultValue={current.body} rows={14} className="mb-4 w-full rounded-lg border p-3 text-sm" required />
|
||||
<Button type="submit">Publish a new version</Button>
|
||||
</form>
|
||||
</Card>
|
||||
<div className="space-y-6">
|
||||
<Card>
|
||||
<Heading sub={`Version ${current.version}${isBuiltIn ? " (the built-in notice, filled in from the church profile)" : ""}. Publishing a new version asks everyone to read it the next time they sign in.`}>Edit and publish</Heading>
|
||||
{builtIn && !isBuiltIn && (
|
||||
<p className="mb-4 text-sm text-gray-600">
|
||||
The built-in notice was rewritten in Accounts 1.3.0 to say more fully what Accounts does.{" "}
|
||||
<Button type="button" variant="link" className="text-sm" onClick={() => setDraft({ title: builtIn.title, body: builtIn.body })}>Start from the built-in notice</Button>
|
||||
</p>
|
||||
)}
|
||||
<form onSubmit={async (e) => {
|
||||
e.preventDefault();
|
||||
if (!window.confirm("Publish this as a new version? Everyone will be asked to read it when they next sign in.")) return;
|
||||
const r = await call("POST", "/admin/notices", draft);
|
||||
if (r.ok) { flash("success", "Published."); void load(); } else flash("error", r.error.message);
|
||||
}}>
|
||||
<Field label="Title" name="title" value={draft.title} onChange={(e) => setDraft({ ...draft, title: e.target.value })} required />
|
||||
<label htmlFor="notice-body" className="mb-1 block text-sm font-medium">Text</label>
|
||||
<textarea id="notice-body" name="body" value={draft.body} onChange={(e) => setDraft({ ...draft, body: e.target.value })} rows={18} className="mb-1 w-full rounded-lg border p-3 font-mono text-xs" required />
|
||||
<p className="mb-4 text-xs text-gray-500">A line starting with "## " is a heading, and one starting with "• " is a bullet.</p>
|
||||
<Button type="submit">Publish a new version</Button>
|
||||
</form>
|
||||
</Card>
|
||||
<Card>
|
||||
<Heading sub="As people will see it.">Preview</Heading>
|
||||
<h3 className="mb-3 text-base font-semibold text-gray-900">{draft.title}</h3>
|
||||
<div className="max-h-[32rem] overflow-y-auto rounded-lg border bg-gray-50 p-4"><NoticeText body={draft.body} compact /></div>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -368,3 +393,73 @@ function Audit() {
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
// ─── Backups (1.3.0) ─────────────────────────────────────────────────────────
|
||||
|
||||
type BackupList = { items: { filename: string; size: number; createdAt: string }[]; nightly: boolean; retain: number; lastError: string | null };
|
||||
|
||||
const size = (n: number) => (n < 1024 ? `${n} B` : n < 1024 * 1024 ? `${(n / 1024).toFixed(1)} KB` : `${(n / (1024 * 1024)).toFixed(1)} MB`);
|
||||
|
||||
function Backups({ flash }: { flash: Flash }) {
|
||||
const [data, setData] = useState<BackupList | null>(null);
|
||||
const [running, setRunning] = useState(false);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const load = useCallback(async () => {
|
||||
const r = await call<BackupList>("GET", "/admin/backups");
|
||||
if (r.ok) setData(r.data);
|
||||
}, []);
|
||||
useExternalEffect(() => { void load(); }, [load]);
|
||||
if (!data) return <p className="text-gray-600">Loading…</p>;
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<Card>
|
||||
<Heading icon={<DatabaseBackup className="h-4 w-4" />} sub="Every night at 02:00, and before every update of Accounts. Kept on this server only, never sent anywhere.">Database backups</Heading>
|
||||
{data.lastError && <Notice tone="warning">The last backup didn't work: {data.lastError}</Notice>}
|
||||
<Button busy={running} onClick={async () => {
|
||||
setRunning(true);
|
||||
const r = await call<{ filename: string }>("POST", "/admin/backups/run");
|
||||
setRunning(false);
|
||||
if (r.ok) { flash("success", `Backup made: ${r.data.filename}`); } else flash("error", r.error.message);
|
||||
void load();
|
||||
}}>Back up now</Button>
|
||||
|
||||
<form className="mt-5 flex flex-wrap items-end gap-x-4 gap-y-2 border-t pt-4" onSubmit={async (e) => {
|
||||
e.preventDefault();
|
||||
const f = new FormData(e.currentTarget);
|
||||
setSaving(true);
|
||||
const r = await call<{ deleted: number }>("PUT", "/admin/backups/settings", { retain: Number(f.get("retain")), nightly: f.get("nightly") === "on" });
|
||||
setSaving(false);
|
||||
if (r.ok) { flash("success", r.data.deleted ? `Saved. ${r.data.deleted} older backups were deleted.` : "Saved."); void load(); } else flash("error", r.error.message);
|
||||
}}>
|
||||
<div className="w-full"><Checkbox name="nightly" label="Back up every night" defaultChecked={data.nightly} hint="Backups before each update are made either way." /></div>
|
||||
<Field label="Keep the newest" name="retain" type="number" min={1} max={365} defaultValue={String(data.retain)} className="mb-0 w-32" />
|
||||
<span className="pb-3 text-sm text-gray-500">backups, and delete older ones</span>
|
||||
<Button type="submit" variant="secondary" busy={saving} className="mb-1 ml-auto">Save</Button>
|
||||
</form>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<Heading sub="A backup holds everyone's details: keep a downloaded copy somewhere safe, and delete it when you're done.">Backups on this server</Heading>
|
||||
<div className="overflow-x-auto rounded-lg border">
|
||||
<table className="min-w-full text-sm">
|
||||
<thead><tr className="border-b bg-gray-50 text-left text-gray-600"><th className="p-2.5 font-medium">Made</th><th className="p-2.5 font-medium">Size</th><th className="p-2.5 font-medium"><span className="sr-only">Download</span></th></tr></thead>
|
||||
<tbody>
|
||||
{data.items.map((b) => (
|
||||
<tr key={b.filename} className="border-t">
|
||||
<td className="p-2.5">{when(b.createdAt)}</td>
|
||||
<td className="p-2.5 text-gray-500">{size(b.size)}</td>
|
||||
<td className="p-2.5 text-right">
|
||||
<a href={`/api/v2/admin/backups/${encodeURIComponent(b.filename)}/download`} download className="inline-flex items-center gap-1.5 text-brand hover:underline">
|
||||
<Download className="h-3.5 w-3.5" aria-hidden /> Download
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
{!data.items.length && <tr><td colSpan={3} className="p-3 text-gray-500">No backups yet.</td></tr>}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { notFound } from "next/navigation";
|
||||
import { ADMIN_SECTIONS, type AdminSection } from "@/lib/sections";
|
||||
import { Admin } from "../Admin";
|
||||
|
||||
export async function generateMetadata({ params }: { params: Promise<{ section: string }> }) {
|
||||
const { section } = await params;
|
||||
return { title: ADMIN_SECTIONS[section as AdminSection]?.title ?? "Admin" };
|
||||
}
|
||||
|
||||
/** One section of admin (1.3.0): /admin/deletions, /church, /linked, … (/admin itself is People). */
|
||||
export default async function AdminSectionPage({ params }: { params: Promise<{ section: string }> }) {
|
||||
const { section } = await params;
|
||||
if (section === "people" || !(section in ADMIN_SECTIONS)) notFound();
|
||||
return <Admin section={section as AdminSection} />;
|
||||
}
|
||||
Reference in New Issue
Block a user