Let Google Workspace groups grant admin access
Site settings → Access names a group for each of admin, editor and viewer (migration 047). Someone in one who signs in with Google gets a Google-only account with their highest group's role, and the role follows the groups: at every sign-in, hourly while signed in, and on Panel → Google groups → Re-sync now. Out of every group, the account is disabled and signed out until they're back in one. Group accounts' roles are locked in the Panel; a superadmin's Disable sticks. Superadmins and admin-cli accounts are never changed. If Google can't be asked, sign-ins are refused but the hourly check leaves accounts alone. Panel → Check their groups tests an email. Lookups use the service account through the Directory API as GOOGLE_DIRECTORY_AS. Setup: docs/google-sign-in.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
d7835dd5d4
commit
80877a4659
13 changed files with 563 additions and 36 deletions
|
|
@ -33,6 +33,9 @@ const GOOGLE_ERRORS: Record<string, string> = {
|
|||
unverified: "That Google account's email isn't verified.",
|
||||
google_unreachable: "Couldn't reach Google. Try again in a moment.",
|
||||
google_failed: "Google sign-in didn't work. Try again, or use your password.",
|
||||
not_in_group:
|
||||
"That Google account isn't in a website access group. Ask a superadmin to add you to one.",
|
||||
groups_unreachable: "Couldn't check your Google groups just now. Try again in a moment.",
|
||||
};
|
||||
|
||||
const fieldClass =
|
||||
|
|
|
|||
|
|
@ -6,11 +6,16 @@
|
|||
assumes neither: it renders whatever the API gives it and shows
|
||||
whatever the API refuses.
|
||||
|
||||
Three blocks, deliberately boring:
|
||||
Four blocks, deliberately boring:
|
||||
|
||||
System what's actually running, for when something is off
|
||||
Content row counts, the cheapest "is the database there"
|
||||
Accounts roles, access and live sessions
|
||||
System what's actually running, for when something is off
|
||||
Content row counts, the cheapest "is the database there"
|
||||
Accounts roles, access and live sessions
|
||||
Google groups check someone's groups, re-sync every group
|
||||
account (server/src/accessGroups.js)
|
||||
|
||||
A group account's role comes from its Google group, so its role
|
||||
select is locked; Disable still works, and sticks.
|
||||
|
||||
The role select is driven by ROLES from lib/roles.ts, so a new
|
||||
rung on the ladder appears here without this file changing. The
|
||||
|
|
@ -25,7 +30,7 @@
|
|||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { del, get, patch } from "../../lib/api.ts";
|
||||
import { ApiError, del, get, patch, post } from "../../lib/api.ts";
|
||||
import { isUnauthorized, useAuth } from "../../lib/auth.tsx";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { ROLES, ROLE_LABELS, ROLE_NOTES, isSuper } from "../../lib/roles.ts";
|
||||
|
|
@ -73,6 +78,107 @@ function Stat({ label, value }) {
|
|||
);
|
||||
}
|
||||
|
||||
/* Google groups: what's set, who a group would let in, and a
|
||||
re-sync of every group account. */
|
||||
function AccessGroups({ access, onSynced }) {
|
||||
const [email, setEmail] = useState("");
|
||||
const [busy, setBusy] = useState<"check" | "sync" | null>(null);
|
||||
const [note, setNote] = useState<{ tone: "ok" | "error"; text: string } | null>(null);
|
||||
const [changes, setChanges] = useState<string[]>([]);
|
||||
const groups = Object.entries(access?.groups ?? {});
|
||||
|
||||
async function send(kind: "check" | "sync") {
|
||||
setBusy(kind);
|
||||
setNote(null);
|
||||
setChanges([]);
|
||||
try {
|
||||
if (kind === "check") {
|
||||
const { role } = await post("/admin/panel/access-check", { email });
|
||||
setNote(
|
||||
role
|
||||
? { tone: "ok", text: `${email} would sign in as ${ROLE_LABELS[role] ?? role}.` }
|
||||
: { tone: "error", text: `${email} isn't in any of these groups.` },
|
||||
);
|
||||
} else {
|
||||
const result = await post("/admin/panel/access-sync", {});
|
||||
setNote({
|
||||
tone: result.failed.length ? "error" : "ok",
|
||||
text:
|
||||
`Checked ${result.checked} group account${result.checked === 1 ? "" : "s"}, ` +
|
||||
`${result.changes.length} changed` +
|
||||
(result.failed.length ? `, ${result.failed.length} couldn't be checked.` : "."),
|
||||
});
|
||||
setChanges([...result.changes, ...result.failed]);
|
||||
onSynced();
|
||||
}
|
||||
} catch (err) {
|
||||
setNote({ tone: "error", text: err instanceof ApiError ? err.message : "Couldn't reach the server." });
|
||||
} finally {
|
||||
setBusy(null);
|
||||
}
|
||||
}
|
||||
|
||||
const button =
|
||||
"rounded-full border border-brand px-4 py-1.5 text-sm font-medium text-link transition-colors hover:bg-tint disabled:cursor-default disabled:opacity-50";
|
||||
|
||||
return (
|
||||
<Block
|
||||
title="Google groups"
|
||||
note="Members of these groups sign in with Google and get the group's role. Set them in Site settings → Access."
|
||||
>
|
||||
{!access?.directory && (
|
||||
<p className="mb-3 text-sm text-danger">
|
||||
Group lookups aren't set up on the server (GOOGLE_DIRECTORY_AS, docs/google-sign-in.md).
|
||||
</p>
|
||||
)}
|
||||
{groups.length > 0 ? (
|
||||
<dl className="mb-4 grid gap-x-6 gap-y-1.5 text-sm sm:grid-cols-3">
|
||||
{groups.map(([role, group]) => (
|
||||
<div key={role}>
|
||||
<dt className="font-medium text-ink">{ROLE_LABELS[role] ?? role}</dt>
|
||||
<dd className="break-all text-muted">{group as string}</dd>
|
||||
</div>
|
||||
))}
|
||||
</dl>
|
||||
) : (
|
||||
<p className="mb-4 text-sm text-muted">No groups set: accounts come from admin-cli only.</p>
|
||||
)}
|
||||
|
||||
<div className="flex flex-wrap items-center gap-3">
|
||||
<label htmlFor="access-check" className="sr-only">
|
||||
Email to check
|
||||
</label>
|
||||
<input
|
||||
id="access-check"
|
||||
type="email"
|
||||
value={email}
|
||||
placeholder="someone@nextgenerationofunity.org"
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
className="min-w-0 flex-1 rounded-lg border border-muted/25 bg-surface px-3 py-1.5 text-sm text-body outline-none focus:border-brand focus:ring-2 focus:ring-brand/25 sm:max-w-sm"
|
||||
/>
|
||||
<button type="button" className={button} disabled={busy !== null || !email.trim()} onClick={() => send("check")}>
|
||||
{busy === "check" ? "Checking…" : "Check their groups"}
|
||||
</button>
|
||||
<button type="button" className={button} disabled={busy !== null} onClick={() => send("sync")}>
|
||||
{busy === "sync" ? "Re-syncing…" : "Re-sync now"}
|
||||
</button>
|
||||
</div>
|
||||
{note && (
|
||||
<p role="status" className={`mt-3 text-sm ${note.tone === "ok" ? "text-link" : "text-danger"}`}>
|
||||
{note.text}
|
||||
</p>
|
||||
)}
|
||||
{changes.length > 0 && (
|
||||
<ul className="mt-2 space-y-1 text-xs text-muted">
|
||||
{changes.map((line) => (
|
||||
<li key={line}>{line}</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</Block>
|
||||
);
|
||||
}
|
||||
|
||||
export default function AdminPanel() {
|
||||
const { user: me } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
|
|
@ -103,6 +209,16 @@ export default function AdminPanel() {
|
|||
load();
|
||||
}, [load]);
|
||||
|
||||
// After a re-sync: the accounts table again, without the loading
|
||||
// screen (which would clear the sync's result).
|
||||
const refresh = async () => {
|
||||
try {
|
||||
setData(await get("/admin/panel/overview", { ttl: 0 }));
|
||||
} catch {
|
||||
// The table stays as it was; the next load shows any error.
|
||||
}
|
||||
};
|
||||
|
||||
/* Replace the one row the server returns rather than refetching
|
||||
the whole overview; the counts didn't change. */
|
||||
function mergeUser(updated) {
|
||||
|
|
@ -164,7 +280,7 @@ export default function AdminPanel() {
|
|||
);
|
||||
}
|
||||
|
||||
const { system, content, users } = data;
|
||||
const { system, content, users, access } = data;
|
||||
const activeSupers = users.filter(
|
||||
(u) => isSuper(u) && u.is_active === 1,
|
||||
).length;
|
||||
|
|
@ -227,6 +343,7 @@ export default function AdminPanel() {
|
|||
const lastSuper =
|
||||
isSuper(row) && row.is_active === 1 && activeSupers <= 1;
|
||||
const locked = isMe || lastSuper;
|
||||
const fromGroup = row.access_source === "group";
|
||||
const busy = busyId === row.id;
|
||||
|
||||
return (
|
||||
|
|
@ -243,6 +360,12 @@ export default function AdminPanel() {
|
|||
{row.name && (
|
||||
<div className="text-xs text-muted/80">{row.email}</div>
|
||||
)}
|
||||
{fromGroup && (
|
||||
<div className="mt-1 text-xs text-muted">
|
||||
Google group
|
||||
{row.group_disabled === 1 && ": in no access group"}
|
||||
</div>
|
||||
)}
|
||||
{rowError?.id === row.id && (
|
||||
<div className="mt-1 text-xs text-danger">
|
||||
{rowError.message}
|
||||
|
|
@ -253,11 +376,13 @@ export default function AdminPanel() {
|
|||
<td className="px-4 py-3">
|
||||
<select
|
||||
value={row.role}
|
||||
disabled={locked || busy}
|
||||
disabled={locked || busy || fromGroup}
|
||||
onChange={(e) => changeRole(row, e.target.value)}
|
||||
className="rounded-lg border border-brand/30 bg-surface px-2 py-1 text-sm text-ink disabled:cursor-not-allowed disabled:bg-tint-soft disabled:text-muted/60"
|
||||
title={
|
||||
isMe
|
||||
fromGroup
|
||||
? "The role comes from this person's Google group."
|
||||
: isMe
|
||||
? "You can't change your own role."
|
||||
: lastSuper
|
||||
? "The last active superadmin can't be demoted."
|
||||
|
|
@ -323,9 +448,12 @@ export default function AdminPanel() {
|
|||
</dl>
|
||||
|
||||
<p className="mt-4 text-xs text-muted/80">
|
||||
New accounts are still created with <code>admin-cli.js</code> on the server.
|
||||
New accounts are created with <code>admin-cli.js</code> on the server, or by signing in
|
||||
with Google while in one of the groups below.
|
||||
</p>
|
||||
</Block>
|
||||
|
||||
<AccessGroups access={access} onSynced={refresh} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue