Edit Authentik groups: rename and change members

Apps → Authentik gets a Groups section for administrators: the groups with
member counts, and an Edit form to rename one and put users in and take them
out. Members are changed one user at a time, never by replacing the list.
Everything is checked first and nothing changes unless all of it may; service
accounts, the token's own account and the last active administrator losing
that are refused. Whether a group makes administrators is shown, not changed.
Groups that decide roles in this app (Settings → Sign-in) are marked and ask
first. Audited with who was put in and taken out.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-01 00:49:43 +01:00
1 parent 3618cb3ae8
commit 6c5a5f0bd6
10 files changed
+794 -9

No files matched your search

+188
View File
@@ -0,0 +1,188 @@
import { useState, type FormEvent } from "react";
import { api, type AuthentikGroupDetail, type AuthentikGroupPerson, type AuthentikInfo } from "../api/client";
import { ErrorNotice, SuccessNotice } from "./Notice";
import { confirmDialog } from "../utils/dialogs";
import { readableError } from "../utils/errors";
function EditGroup({ g, people, self, busy, onSave, onCancel }: { g: AuthentikGroupDetail; people: AuthentikGroupPerson[]; self: number | null; busy: boolean; onSave: (name: string, add: number[], remove: number[]) => void; onCancel: () => void }) {
const [name, setName] = useState(g.name);
const [members, setMembers] = useState<number[]>(g.members ?? []);
const [filter, setFilter] = useState("");
const canEditMembers = g.members !== null;
const shown = people.filter((p) => !filter.trim() || `${p.username} ${p.name}`.toLowerCase().includes(filter.trim().toLowerCase()));
// Members who are not in the list of people — service accounts, or beyond the first 200 — are not shown and are never taken out from here.
const hidden = (g.members ?? []).filter((m) => !people.some((p) => p.pk === m)).length;
const toggle = (pk: number) => setMembers((cur) => (cur.includes(pk) ? cur.filter((x) => x !== pk) : [...cur, pk]));
function submit(e: FormEvent) {
e.preventDefault();
const was = g.members ?? [];
const add = canEditMembers ? members.filter((m) => !was.includes(m)) : [];
const remove = canEditMembers ? was.filter((m) => !members.includes(m) && people.some((p) => p.pk === m)) : [];
if (name.trim() === g.name && add.length === 0 && remove.length === 0) return onCancel();
onSave(name.trim(), add, remove);
}
return (
<form onSubmit={submit} className="p-2">
{g.decidesRole && (
<div className="alert alert-warning py-2 small">
This group decides who is {g.decidesRole === "admin" ? "an administrator" : "an operator"} <em>here</em> (Settings → Sign-in). Renaming it, or changing who is in it, changes that at their next sign-in.
</div>
)}
{g.admin && <div className="text-secondary small mb-2">Members of this group are administrators of Authentik. Whether a group is one is changed in Authentik, not here.</div>}
<label className="form-label" htmlFor={`ak-group-name-${g.id}`}>
Name
</label>
<input id={`ak-group-name-${g.id}`} className="form-control form-control-sm mb-3" style={{ maxWidth: 360 }} required maxLength={200} autoComplete="off" value={name} onChange={(e) => setName(e.target.value)} />
<div className="form-label">Members</div>
{!canEditMembers ? (
<div className="text-secondary small mb-3">Authentik did not say who is in this group, so only its name can be changed here.</div>
) : (
<>
<input className="form-control form-control-sm mb-2" style={{ maxWidth: 360 }} placeholder="Find a user" aria-label="Find a user" value={filter} onChange={(e) => setFilter(e.target.value)} />
<div className="border rounded p-2 mb-1" style={{ maxHeight: 240, overflowY: "auto" }}>
{shown.length === 0 && <div className="text-secondary small">No user matches.</div>}
{shown.map((p) => (
<label key={p.pk} className="form-check">
<input type="checkbox" className="form-check-input" checked={members.includes(p.pk)} disabled={p.pk === self} onChange={() => toggle(p.pk)} />
<span className="form-check-label">
{p.username}
{p.name !== p.username && <span className="text-secondary"> — {p.name}</span>}
{!p.active && <span className="badge bg-secondary-lt text-secondary ms-2">off</span>}
{p.pk === self && <span className="text-secondary small ms-2">this token&rsquo;s account — looked after in Authentik</span>}
</span>
</label>
))}
</div>
{hidden > 0 && <div className="text-secondary small mb-2">{hidden} member{hidden === 1 ? " is" : "s are"} not shown (service accounts, or beyond the first 200 users) and are left as they are.</div>}
</>
)}
<div className="d-flex gap-2 mt-2">
<button type="submit" className="btn btn-sm btn-primary" disabled={busy}>
{busy ? "Saving…" : "Save"}
</button>
<button type="button" className="btn btn-sm btn-link text-secondary" onClick={onCancel} disabled={busy}>
Cancel
</button>
</div>
</form>
);
}
/**
* The groups of an Authentik, for an administrator to rename and to put users in and take them out of. Whether a group makes its members administrators is shown, not changed. A group that
* decides somebody's role in this app says so, and asks before it is changed.
*/
export default function AuthentikGroupList({ a }: { a: AuthentikInfo }) {
const [groups, setGroups] = useState<AuthentikGroupDetail[] | null>(null);
const [people, setPeople] = useState<AuthentikGroupPerson[]>([]);
const [self, setSelf] = useState<number | null>(null);
const [editing, setEditing] = useState<string | null>(null);
const [busy, setBusy] = useState<string | "list" | null>(null);
const [error, setError] = useState<string | null>(null);
const [notice, setNotice] = useState<string | null>(null);
if (!a.canCreateUsers) return null;
async function load() {
setBusy("list");
setError(null);
try {
const r = await api.authentik.groupDetails(a.id);
setGroups(r.groups);
setPeople(r.people);
setSelf(r.self);
} catch (err) {
setError(readableError(err));
} finally {
setBusy(null);
}
}
async function save(g: AuthentikGroupDetail, name: string, add: number[], remove: number[]) {
if (g.decidesRole) {
const ok = await confirmDialog({
title: "Change a group that decides roles",
message: `"${g.name}" decides who is ${g.decidesRole === "admin" ? "an administrator" : "an operator"} in this app. Changing its name or its members changes people's roles here at their next sign-in. Go on?`,
confirmLabel: "Save",
danger: true,
});
if (!ok) return;
}
setBusy(g.id);
setError(null);
setNotice(null);
try {
const r = await api.authentik.updateGroup(a.id, g.id, { name: name !== g.name ? name : undefined, membersAdd: add, membersRemove: remove });
setEditing(null);
setNotice(r.changed.length === 0 ? "Nothing was different." : `Saved "${name}"${r.added + r.removed > 0 ? ` — ${r.added} put in, ${r.removed} taken out` : ""}.`);
await load();
} catch (err) {
setError(readableError(err));
setBusy(null);
}
}
return (
<div className="mt-3">
<h4>Groups</h4>
<ErrorNotice error={error} />
<SuccessNotice notice={notice} />
{groups === null ? (
<button type="button" className="btn btn-sm btn-outline-secondary" onClick={() => void load()} disabled={busy !== null}>
{busy === "list" ? "Reading…" : "Show groups"}
</button>
) : (
<>
{groups.length === 0 ? (
<div className="text-secondary mb-2">There are no groups.</div>
) : (
<div className="table-responsive">
<table className="table table-vcenter table-sm">
<thead>
<tr>
<th>Group</th>
<th className="text-end">Members</th>
<th className="text-end">&nbsp;</th>
</tr>
</thead>
<tbody>
{groups.map((g) =>
editing === g.id ? (
<tr key={g.id}>
<td colSpan={3}>
<EditGroup g={g} people={people} self={self} busy={busy === g.id} onSave={(name, add, remove) => void save(g, name, add, remove)} onCancel={() => setEditing(null)} />
</td>
</tr>
) : (
<tr key={g.id}>
<td>
{g.name}
{g.admin && <span className="badge bg-yellow-lt text-yellow ms-2">administrators</span>}
{g.decidesRole && <span className="badge bg-blue-lt text-blue ms-1">decides {g.decidesRole === "admin" ? "admins" : "operators"} here</span>}
</td>
<td className="text-end text-secondary">{g.members === null ? "?" : g.members.length}</td>
<td className="text-end">
<button type="button" className="btn btn-sm btn-outline-secondary" disabled={busy !== null} onClick={() => { setEditing(g.id); setNotice(null); }}>
Edit
</button>
</td>
</tr>
),
)}
</tbody>
</table>
</div>
)}
<button type="button" className="btn btn-sm btn-link text-secondary px-0" onClick={() => { setGroups(null); setEditing(null); }}>
Hide groups
</button>
</>
)}
</div>
);
}