Edit Authentik users: name, email and groups

Each person in Apps → Authentik → Users gets an Edit form. The user name is
shown but not changeable. Authentik is sent only what differs, and groups as
what goes in and what comes out on top of the user's current groups, so a
membership added meanwhile is kept; a user whose groups Authentik doesn't
report can't have them changed. The server refuses service accounts, the
token's own account, and an edit that would leave no active administrator.
Audited by field name, never by value.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-01 00:39:51 +01:00
1 parent 0cd11268ee
commit 3618cb3ae8
9 files changed
+370 -30

No files matched your search

+3
View File
@@ -1781,6 +1781,8 @@ export interface AuthentikPerson {
/** May do everything in Authentik. */
admin: boolean;
lastLogin: string | null;
/** Group ids; null when Authentik did not say, and then they are not changed from here. */
groups: string[] | null;
}
/** A user Authentik made, and the one-time link for them to choose a password with (null, with the reason, when that could not be made). */
@@ -3426,6 +3428,7 @@ export const api = {
createUser: (id: number, body: AuthentikNewUser) => request<AuthentikUserMade>(`/api/authentik/${id}/users`, { method: "POST", body: JSON.stringify(body) }),
users: (id: number, search = "") => request<{ users: AuthentikPerson[]; more: boolean; self: number | null }>(`/api/authentik/${id}/users${search.trim() ? `?search=${encodeURIComponent(search.trim())}` : ""}`),
setUserActive: (id: number, pk: number, active: boolean) => request<{ user: AuthentikPerson }>(`/api/authentik/${id}/users/${pk}/active`, { method: "POST", body: JSON.stringify({ active }) }),
updateUser: (id: number, pk: number, body: { name?: string; email?: string; groupsAdd?: string[]; groupsRemove?: string[] }) => request<{ user: AuthentikPerson; changed: string[] }>(`/api/authentik/${id}/users/${pk}`, { method: "PATCH", body: JSON.stringify(body) }),
deleteUser: (id: number, pk: number, username: string) => request<void>(`/api/authentik/${id}/users/${pk}`, { method: "DELETE", body: JSON.stringify({ username }) }),
removeWriteToken: (id: number) => request<void>(`/api/authentik/${id}/write-token`, { method: "DELETE" }),
},
+87
View File
@@ -0,0 +1,87 @@
import { useState, type FormEvent } from "react";
import type { AuthentikGroup, AuthentikPerson } from "../api/client";
export interface AuthentikChange {
name?: string;
email?: string;
groupsAdd?: string[];
groupsRemove?: string[];
}
/**
* The form for changing what a person is called, their email and the groups they belong to. The user name is shown and can't be changed here: it is how the person is known elsewhere.
* Groups are sent as what goes in and what comes out, not as a list, so a group somebody else gave them meanwhile is not lost.
*/
export default function AuthentikEditUser({ p, groups, groupsError, busy, onSave, onCancel }: { p: AuthentikPerson; groups: AuthentikGroup[] | null; groupsError: string | null; busy: boolean; onSave: (change: AuthentikChange) => void; onCancel: () => void }) {
const [name, setName] = useState(p.name);
const [email, setEmail] = useState(p.email ?? "");
const [picked, setPicked] = useState<string[]>(p.groups ?? []);
const canEditGroups = p.groups !== null && groups !== null && !groupsError;
const toggle = (id: string) => setPicked((cur) => (cur.includes(id) ? cur.filter((x) => x !== id) : [...cur, id]));
function submit(e: FormEvent) {
e.preventDefault();
const change: AuthentikChange = {};
if (name.trim() !== p.name) change.name = name.trim();
if (email.trim() !== (p.email ?? "")) change.email = email.trim();
if (canEditGroups && p.groups) {
const add = picked.filter((g) => !p.groups!.includes(g));
const remove = p.groups.filter((g) => !picked.includes(g) && groups!.some((x) => x.id === g));
if (add.length > 0) change.groupsAdd = add;
if (remove.length > 0) change.groupsRemove = remove;
}
if (Object.keys(change).length === 0) return onCancel();
onSave(change);
}
return (
<form onSubmit={submit} className="p-2">
<div className="text-secondary small mb-2">
User name: <strong>{p.username}</strong> — it can&rsquo;t be changed here, it is how they are known elsewhere.
</div>
<div className="row g-2 mb-3">
<div className="col-md-6">
<label className="form-label" htmlFor={`ak-edit-name-${p.pk}`}>
Name
</label>
<input id={`ak-edit-name-${p.pk}`} className="form-control form-control-sm" required maxLength={200} autoComplete="off" value={name} onChange={(e) => setName(e.target.value)} />
</div>
<div className="col-md-6">
<label className="form-label" htmlFor={`ak-edit-email-${p.pk}`}>
Email <span className="text-secondary">(empty to clear)</span>
</label>
<input id={`ak-edit-email-${p.pk}`} type="email" className="form-control form-control-sm" maxLength={254} autoComplete="off" value={email} onChange={(e) => setEmail(e.target.value)} />
</div>
</div>
<div className="form-label">Groups</div>
{p.groups === null && <div className="text-secondary small mb-3">Authentik did not say which groups this user is in, so only the name and email can be changed here.</div>}
{p.groups !== null && groupsError && <div className="text-yellow small mb-3">The groups could not be read: {groupsError}. The name and email can still be changed.</div>}
{p.groups !== null && !groupsError && groups === null && <div className="text-secondary small mb-3">Reading the groups…</div>}
{canEditGroups && groups!.length === 0 && <div className="text-secondary small mb-3">There are no groups.</div>}
{canEditGroups && groups!.length > 0 && (
<div className="mb-3">
{groups!.map((g) => (
<label key={g.id} className="form-check">
<input type="checkbox" className="form-check-input" checked={picked.includes(g.id)} onChange={() => toggle(g.id)} />
<span className="form-check-label">
{g.name}
{g.admin && <span className="badge bg-yellow-lt text-yellow ms-2">administrators</span>}
</span>
</label>
))}
</div>
)}
<div className="d-flex gap-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>
);
}
+45 -4
View File
@@ -1,12 +1,13 @@
import { useState, type FormEvent } from "react";
import { api, type AuthentikInfo, type AuthentikPerson } from "../api/client";
import { api, type AuthentikGroup, type AuthentikInfo, type AuthentikPerson } from "../api/client";
import AuthentikEditUser, { type AuthentikChange } from "./AuthentikEditUser";
import TimeAgo from "./TimeAgo";
import { ErrorNotice } from "./Notice";
import { confirmDialog, promptDialog } from "../utils/dialogs";
import { readableError } from "../utils/errors";
/**
* The people in an Authentik, for an administrator to switch off, switch on again or delete. Switching off is the way that can be undone; deleting asks for the user name to be typed.
* The people in an Authentik, for an administrator to edit, switch off, switch on again or delete. Switching off is the way that can be undone; deleting asks for the user name to be typed.
* The account the token belongs to has no buttons, and neither does the last active administrator: the server refuses them too, and says why.
*/
export default function AuthentikUserList({ a }: { a: AuthentikInfo }) {
@@ -16,9 +17,38 @@ export default function AuthentikUserList({ a }: { a: AuthentikInfo }) {
const [search, setSearch] = useState("");
const [busy, setBusy] = useState<number | "list" | null>(null);
const [error, setError] = useState<string | null>(null);
const [editing, setEditing] = useState<number | null>(null);
const [groups, setGroups] = useState<AuthentikGroup[] | null>(null);
const [groupsError, setGroupsError] = useState<string | null>(null);
if (!a.canCreateUsers) return null;
async function startEdit(p: AuthentikPerson) {
setEditing(p.pk);
setError(null);
if (groups !== null) return;
try {
setGroups((await api.authentik.groups(a.id)).groups);
setGroupsError(null);
} catch (err) {
setGroups([]);
setGroupsError(readableError(err));
}
}
async function save(p: AuthentikPerson, change: AuthentikChange) {
setBusy(p.pk);
setError(null);
try {
await api.authentik.updateUser(a.id, p.pk, change);
setEditing(null);
await load();
} catch (err) {
setError(readableError(err));
setBusy(null);
}
}
async function load(q = search) {
setBusy("list");
setError(null);
@@ -114,7 +144,14 @@ export default function AuthentikUserList({ a }: { a: AuthentikInfo }) {
</tr>
</thead>
<tbody>
{people.map((p) => (
{people.map((p) =>
editing === p.pk ? (
<tr key={p.pk}>
<td colSpan={4}>
<AuthentikEditUser p={p} groups={groups} groupsError={groupsError} busy={busy === p.pk} onSave={(change) => void save(p, change)} onCancel={() => setEditing(null)} />
</td>
</tr>
) : (
<tr key={p.pk} className={p.active ? "" : "text-secondary"}>
<td>
{p.username}
@@ -133,6 +170,9 @@ export default function AuthentikUserList({ a }: { a: AuthentikInfo }) {
</span>
) : (
<>
<button type="button" className="btn btn-sm btn-outline-secondary me-1" disabled={busy !== null} onClick={() => void startEdit(p)}>
Edit
</button>
<button type="button" className="btn btn-sm btn-outline-secondary me-1" disabled={busy !== null} onClick={() => void switchTo(p, !p.active)}>
{p.active ? "Switch off" : "Switch on"}
</button>
@@ -143,7 +183,8 @@ export default function AuthentikUserList({ a }: { a: AuthentikInfo }) {
)}
</td>
</tr>
))}
),
)}
</tbody>
</table>
</div>