import { useEffect, useState, type FormEvent } from "react"; import { api, type AuthentikGroupDetail, type AuthentikGroupPerson, type AuthentikInfo } from "../api/client"; import { ErrorNotice, SuccessNotice } from "./Notice"; import { confirmDialog, promptDialog } from "../utils/dialogs"; import { readableError } from "../utils/errors"; import { t } from "../i18n"; 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(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 (
{g.decidesRole && (
This group decides who is {g.decidesRole === "admin" ? "an administrator" : "an operator"} here (Settings → Sign-in). Renaming it, or changing who is in it, changes that at their next sign-in.
)} {g.admin &&
Members of this group are administrators of Authentik. Whether a group is one is changed in Authentik, not here.
} setName(e.target.value)} />
{t("Members")}
{!canEditMembers ? (
Authentik did not say who is in this group, so only its name can be changed here.
) : ( <> setFilter(e.target.value)} />
{shown.length === 0 &&
{t("No user matches.")}
} {shown.map((p) => ( ))}
{hidden > 0 &&
{hidden} member{hidden === 1 ? " is" : "s are"} not shown (service accounts, or beyond the first 200 users) and are left as they are.
} )}
); } /** * The groups of an Authentik, read when the tab opens, for an administrator to make, rename, delete and to put users in and take them out of. Whether a group makes its members administrators is shown, not changed — and a group * made here never does. 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(null); const [people, setPeople] = useState([]); const [self, setSelf] = useState(null); const [editing, setEditing] = useState(null); const [busy, setBusy] = useState(null); const [error, setError] = useState(null); const [notice, setNotice] = useState(null); const [creating, setCreating] = useState(false); const [newName, setNewName] = useState(""); // Read as the tab opens, like the users. If that fails, the button below tries again. useEffect(() => { if (a.canCreateUsers) void load(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [a.id, a.canCreateUsers]); if (!a.canCreateUsers) return null; async function create(e: FormEvent) { e.preventDefault(); if (busy !== null) return; setBusy("new"); setError(null); setNotice(null); try { const { group } = await api.authentik.createGroup(a.id, newName.trim()); setCreating(false); setNewName(""); setNotice( `Made "${group.name}", with no members.${group.decidesRole ? ` Its name decides who is ${group.decidesRole === "admin" ? "an administrator" : "an operator"} here (Settings → Sign-in): whoever is put in it gets that role at their next sign-in.` : ""}`, ); if (groups !== null) await load(); else setBusy(null); } catch (err) { setError(readableError(err)); setBusy(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 removeGroup(g: AuthentikGroupDetail) { const members = g.members === null ? "" : g.members.length === 0 ? " It has no members." : ` Its ${g.members.length} member${g.members.length === 1 ? "" : "s"} stay${g.members.length === 1 ? "s" : ""}, just not in it.`; const roles = g.decidesRole ? ` It decides who is ${g.decidesRole === "admin" ? "an administrator" : "an operator"} in this app (Settings → Sign-in): people in it lose that role here at their next sign-in.` : ""; const typed = await promptDialog({ title: t("Delete group"), message: `This deletes the group "${g.name}" in ${a.name} for good. It cannot be undone.${members}${roles} Type the name of the group to confirm.`, label: t("Group name"), confirmLabel: t("Delete"), required: true, }); if (typed === null) return; if (typed.trim() !== g.name) { setError("That is not the name of the group, so nothing was deleted."); return; } setBusy(g.id); setError(null); setNotice(null); try { await api.authentik.deleteGroup(a.id, g.id, g.name); setNotice(`Deleted "${g.name}".`); await load(); } catch (err) { setError(readableError(err)); setBusy(null); } } async function save(g: AuthentikGroupDetail, name: string, add: number[], remove: number[]) { if (g.decidesRole) { const ok = await confirmDialog({ title: t("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: t("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 ? t("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 (

{t("Groups")}

{creating ? (
setNewName(e.target.value)} />
It starts with no members — put them in with Edit — and it does not make its members administrators of Authentik; that is decided in Authentik.
) : ( )} {groups === null ? ( ) : ( <> {groups.length === 0 ? (
{t("There are no groups.")}
) : (
{groups.map((g) => editing === g.id ? ( ) : ( ), )}
{t("Group")} {t("Members")}  
void save(g, name, add, remove)} onCancel={() => setEditing(null)} />
{g.name} {g.admin && administrators} {g.decidesRole && decides {g.decidesRole === "admin" ? "admins" : "operators"} here} {g.members === null ? "?" : g.members.length} {g.admin ? ( deleted in Authentik ) : ( )}
)} )}
); }