Delete Authentik groups
Each group in Apps → Authentik → Users and groups gets a Delete button: the group's name has to be typed, and the server checks it against the group as Authentik has it now. A group that makes its members administrators is refused (deleted in Authentik). A group that decides roles in this app may be deleted after a warning, and the audit log records its member count and that it decided a role. Members stay, just not in the group. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e7e3c73e77
commit
e4cd01e9ba
8 files changed
+151
-12
No files matched your search
@@ -6,7 +6,7 @@
|
||||
* person gets to choose their own password: this app never sets, sees or keeps one. The link is handed back once and not stored.
|
||||
*
|
||||
* Looking after groups: the groups with who is in them (`GET /core/groups/`, one `GET /core/groups/{id}/`), making one (`POST /core/groups/` with a name and never as a group that makes administrators),
|
||||
* renaming one (`PATCH` with only `name`) and putting a user in or taking one out
|
||||
* renaming one (`PATCH` with only `name`), deleting one (`DELETE /core/groups/{id}/` — never one that makes administrators, which the caller refuses) and putting a user in or taking one out
|
||||
* (`POST /core/groups/{id}/add_user/` and `remove_user/`, one user each — never a whole list of members, so someone who joined meanwhile is not dropped). Whether a group makes its members
|
||||
* administrators of Authentik (`is_superuser`) is shown, and neither changed nor set when one is made, from here.
|
||||
*
|
||||
@@ -212,6 +212,7 @@ export interface AuthentikUsersAdapter {
|
||||
getGroup(id: string): Promise<AuthentikGroupDetail>;
|
||||
createGroup(name: string): Promise<AuthentikGroupDetail>;
|
||||
renameGroup(id: string, name: string): Promise<AuthentikGroupDetail>;
|
||||
deleteGroup(id: string): Promise<void>;
|
||||
addMember(id: string, userPk: number): Promise<void>;
|
||||
removeMember(id: string, userPk: number): Promise<void>;
|
||||
createUser(input: NewAuthentikUser): Promise<CreatedAuthentikUser>;
|
||||
@@ -314,6 +315,10 @@ export function createAuthentikUsersAdapter(config: AuthentikUsersConfig): Authe
|
||||
return group;
|
||||
}
|
||||
|
||||
async function deleteGroup(id: string): Promise<void> {
|
||||
await call("DELETE", `/core/groups/${groupId(id)}/`);
|
||||
}
|
||||
|
||||
async function addMember(id: string, userPk: number): Promise<void> {
|
||||
await call("POST", `/core/groups/${groupId(id)}/add_user/`, { pk: wholeNumber(userPk) }, true);
|
||||
}
|
||||
@@ -342,5 +347,5 @@ export function createAuthentikUsersAdapter(config: AuthentikUsersConfig): Authe
|
||||
return link;
|
||||
}
|
||||
|
||||
return withDiagLogging("authentik", { listUsers, getUser, whoAmI, activeAdmins, setActive, updateUser, deleteUser, listGroupDetails, getGroup, createGroup, renameGroup, addMember, removeMember, listGroups, createUser, recoveryLink });
|
||||
return withDiagLogging("authentik", { listUsers, getUser, whoAmI, activeAdmins, setActive, updateUser, deleteUser, listGroupDetails, getGroup, createGroup, renameGroup, deleteGroup, addMember, removeMember, listGroups, createUser, recoveryLink });
|
||||
}
|
||||
@@ -444,3 +444,37 @@ authentikRouter.post("/:id/groups", requireRole("admin"), asyncHandler(async (re
|
||||
sendFailure(res, err);
|
||||
}
|
||||
}));
|
||||
|
||||
const deleteGroupSchema = z.object({ name: z.string().min(1).max(200) });
|
||||
|
||||
/**
|
||||
* Deletes a group for good; its members stay, just not in it. The group's name has to be sent along and has to be the one it has now (a page that is minutes old cannot delete another group),
|
||||
* and a group that makes its members administrators of Authentik is refused: deleting it would take that from all of them at once, possibly from the last administrator. A group that decides
|
||||
* roles in this app (Settings → Sign-in) may be deleted — the page asks first — and the audit log says it was one.
|
||||
*/
|
||||
authentikRouter.delete("/:id/groups/:gid", requireRole("admin"), asyncHandler(async (req, res) => {
|
||||
const parsed = deleteGroupSchema.safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: "The name of the group has to be sent along to delete it." });
|
||||
const gid = req.params.gid;
|
||||
if (!GROUP_ID.test(gid)) return res.status(400).json({ error: "invalid_group" });
|
||||
const target = await usersAdapterFor(req.params.id, res);
|
||||
if (!target) return;
|
||||
try {
|
||||
const group = await target.adapter.getGroup(gid);
|
||||
if (group.name !== parsed.data.name) return res.status(400).json({ error: "name_mismatch", message: "That is not the name of that group." });
|
||||
if (group.admin) return res.status(409).json({ error: "refused", message: "That group makes its members administrators of Authentik; deleting it would take that from all of them at once. Do it in Authentik if you mean to." });
|
||||
const settings = await getSettings();
|
||||
await target.adapter.deleteGroup(gid);
|
||||
await recordAudit({
|
||||
actor: req.currentUser!,
|
||||
category: "integration",
|
||||
action: "delete_authentik_group",
|
||||
targetType: "authentik_group",
|
||||
targetId: gid,
|
||||
detail: { integrationId: target.id, integration: target.name, group: group.name, members: group.members?.length ?? null, decidesRole: roleForGroupName(settings.signIn, group.name) },
|
||||
});
|
||||
res.status(204).end();
|
||||
} catch (err) {
|
||||
sendFailure(res, err);
|
||||
}
|
||||
}));
|
||||
Reference in new issue
Block a user