authentik.enterprise.lifecycle.offboarding.actions

Shared user offboarding logic.

This is the single place that actually deactivates/deletes a user and revokes their access. The single-user offboarding feature uses it via a scheduled sweeper task, and the upcoming policy-driven action lifecycle will reuse it too.

 1"""Shared user offboarding logic.
 2
 3This is the single place that actually deactivates/deletes a user and revokes
 4their access. The single-user offboarding feature uses it via a scheduled
 5sweeper task, and the upcoming policy-driven action lifecycle will reuse it too.
 6"""
 7
 8from django.http import HttpRequest
 9from django.utils.translation import gettext as _
10from structlog.stdlib import get_logger
11
12from authentik.core.models import User
13from authentik.core.signals import deactivation_inhibit_cleanup
14from authentik.enterprise.core.revocation import revoke_user_access
15from authentik.enterprise.lifecycle.offboarding.models import OffboardingAction
16from authentik.events.models import Event, EventAction
17
18LOGGER = get_logger()
19
20
21def offboard_user(
22    user: User,
23    action: str,
24    *,
25    revoke_sessions: bool = True,
26    revoke_tokens: bool = True,
27    request: HttpRequest | None = None,
28    initiator: User | None = None,
29    **context,
30):
31    """Offboard `user` by applying `action`, optionally revoking sessions/tokens.
32
33    The event is built and saved before any destructive action so that a
34    `DELETE` (which cascades this user's related rows away) is still audited.
35
36    The audit event is attributed to the initiator (request user, or the
37    scheduling admin via `initiator`) — never to the offboarded user.
38    """
39    username = user.username
40    user_pk = user.pk
41
42    revoke_user_access(user, sessions=revoke_sessions, tokens=revoke_tokens)
43
44    event = Event.new(
45        EventAction.USER_OFFBOARDED,
46        message=_("User %(username)s was offboarded (%(action)s)")
47        % {"username": username, "action": action},
48        offboarding_action=action,
49        revoke_sessions=revoke_sessions,
50        revoke_tokens=revoke_tokens,
51        user_pk=user_pk,
52        **context,
53    )
54    if request is not None:
55        event.from_http(request, user=initiator)
56    else:
57        if initiator is not None:
58            event.set_user(initiator)
59        event.save()
60
61    if action == OffboardingAction.DEACTIVATE:
62        user.is_active = False
63        with deactivation_inhibit_cleanup(sessions=not revoke_sessions, tokens=not revoke_tokens):
64            user.save(update_fields=["is_active"])
65    elif action == OffboardingAction.DELETE:
66        user.delete()
67    else:
68        LOGGER.warning("Unknown offboarding action", action=action, user=username)
LOGGER = <BoundLoggerLazyProxy(logger=None, wrapper_class=None, processors=None, context_class=None, initial_values={}, logger_factory_args=())>
def offboard_user( user: authentik.core.models.User, action: str, *, revoke_sessions: bool = True, revoke_tokens: bool = True, request: django.http.request.HttpRequest | None = None, initiator: authentik.core.models.User | None = None, **context):
22def offboard_user(
23    user: User,
24    action: str,
25    *,
26    revoke_sessions: bool = True,
27    revoke_tokens: bool = True,
28    request: HttpRequest | None = None,
29    initiator: User | None = None,
30    **context,
31):
32    """Offboard `user` by applying `action`, optionally revoking sessions/tokens.
33
34    The event is built and saved before any destructive action so that a
35    `DELETE` (which cascades this user's related rows away) is still audited.
36
37    The audit event is attributed to the initiator (request user, or the
38    scheduling admin via `initiator`) — never to the offboarded user.
39    """
40    username = user.username
41    user_pk = user.pk
42
43    revoke_user_access(user, sessions=revoke_sessions, tokens=revoke_tokens)
44
45    event = Event.new(
46        EventAction.USER_OFFBOARDED,
47        message=_("User %(username)s was offboarded (%(action)s)")
48        % {"username": username, "action": action},
49        offboarding_action=action,
50        revoke_sessions=revoke_sessions,
51        revoke_tokens=revoke_tokens,
52        user_pk=user_pk,
53        **context,
54    )
55    if request is not None:
56        event.from_http(request, user=initiator)
57    else:
58        if initiator is not None:
59            event.set_user(initiator)
60        event.save()
61
62    if action == OffboardingAction.DEACTIVATE:
63        user.is_active = False
64        with deactivation_inhibit_cleanup(sessions=not revoke_sessions, tokens=not revoke_tokens):
65            user.save(update_fields=["is_active"])
66    elif action == OffboardingAction.DELETE:
67        user.delete()
68    else:
69        LOGGER.warning("Unknown offboarding action", action=action, user=username)

Offboard user by applying action, optionally revoking sessions/tokens.

The event is built and saved before any destructive action so that a DELETE (which cascades this user's related rows away) is still audited.

The audit event is attributed to the initiator (request user, or the scheduling admin via initiator) — never to the offboarded user.