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:
1 parent
3618cb3ae8
commit
6c5a5f0bd6
10 files changed
+794
-9
No files matched your search
@@ -1764,6 +1764,21 @@ export interface AuthentikGroup {
|
||||
admin: boolean;
|
||||
}
|
||||
|
||||
/** A group with who is in it, and the role it decides in this app, if any (Settings → Sign-in). */
|
||||
export interface AuthentikGroupDetail extends AuthentikGroup {
|
||||
/** User ids; null when Authentik did not say, and then they are not changed from here. */
|
||||
members: number[] | null;
|
||||
decidesRole: "admin" | "operator" | null;
|
||||
}
|
||||
|
||||
/** A person to name a group's members by. */
|
||||
export interface AuthentikGroupPerson {
|
||||
pk: number;
|
||||
username: string;
|
||||
name: string;
|
||||
active: boolean;
|
||||
}
|
||||
|
||||
export interface AuthentikNewUser {
|
||||
username: string;
|
||||
name: string;
|
||||
@@ -3428,6 +3443,8 @@ 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 }) }),
|
||||
groupDetails: (id: number) => request<{ groups: AuthentikGroupDetail[]; people: AuthentikGroupPerson[]; more: boolean; self: number | null }>(`/api/authentik/${id}/group-details`),
|
||||
updateGroup: (id: number, gid: string, body: { name?: string; membersAdd?: number[]; membersRemove?: number[] }) => request<{ changed: string[]; added: number; removed: number }>(`/api/authentik/${id}/groups/${gid}`, { method: "PATCH", body: JSON.stringify(body) }),
|
||||
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" }),
|
||||
|
||||
@@ -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’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"> </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>
|
||||
);
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { useCallback, useEffect, useState } from "react";
|
||||
import { Link } from "react-router-dom";
|
||||
import { api, type AuthentikInfo, type CurrentUser } from "../api/client";
|
||||
import AuthentikNewUser from "../components/AuthentikNewUser";
|
||||
import AuthentikGroupList from "../components/AuthentikGroupList";
|
||||
import AuthentikUserList from "../components/AuthentikUserList";
|
||||
import EmptyState from "../components/EmptyState";
|
||||
import TimeAgo from "../components/TimeAgo";
|
||||
@@ -130,6 +131,7 @@ function Card({ a, isAdmin, onChanged }: { a: AuthentikInfo; isAdmin: boolean; o
|
||||
)}
|
||||
{isAdmin && a.enabled && <AuthentikNewUser a={a} onChanged={onChanged} />}
|
||||
{isAdmin && a.enabled && <AuthentikUserList a={a} />}
|
||||
{isAdmin && a.enabled && <AuthentikGroupList a={a} />}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
@@ -160,7 +162,7 @@ export default function Authentik({ user }: { user: CurrentUser }) {
|
||||
<h2 className="page-title">Authentik</h2>
|
||||
<p className="text-secondary">
|
||||
The sign-in in front of everything else. It is read every few minutes with an API token (see <Link to="/integrations">Integrations</Link>). An <strong>outpost that goes quiet</strong> (the apps it guards stop letting anyone in), a <strong>certificate</strong> or an{" "}
|
||||
<strong>API token</strong> that is about to run out, <strong>no worker</strong> and a burst of <strong>failed sign-ins</strong> are alerts — see <Link to="/alerts">Alerts</Link>.{user.role === "admin" && " An administrator can also make a user here, with a one-time link for them to choose a password, and switch users off or delete them."}
|
||||
<strong>API token</strong> that is about to run out, <strong>no worker</strong> and a burst of <strong>failed sign-ins</strong> are alerts — see <Link to="/alerts">Alerts</Link>.{user.role === "admin" && " An administrator can also make a user here, with a one-time link for them to choose a password, and switch users off or delete them, and rename groups and change who is in them."}
|
||||
</p>
|
||||
<ErrorNotice error={error} />
|
||||
{!data && !error && <BlockSkeleton lines={4} />}
|
||||
|
||||
Reference in new issue
Block a user