authentik.enterprise.required_actions.api

Required action validation for users

 1"""Required action validation for users"""
 2
 3from django.utils.translation import gettext_lazy as _
 4from rest_framework.exceptions import ValidationError
 5
 6from authentik.enterprise.required_actions import USER_ATTRIBUTE_REQUIRED_ACTIONS
 7from authentik.enterprise.required_actions.flows import resolve_required_actions
 8
 9
10class RequiredActionsUserSerializerMixin:
11    """Validate required actions on users"""
12
13    def validate_attributes(self, attributes: dict) -> dict:
14        """Reject required actions that cannot run"""
15        key = USER_ATTRIBUTE_REQUIRED_ACTIONS
16        current = self.instance.attributes if self.instance else {}
17        # Only validate changes, so a flow deleted later doesn't block unrelated edits
18        if key not in attributes or (key in current and attributes[key] == current[key]):
19            return attributes
20        try:
21            resolve_required_actions(attributes[key])
22        except ValueError as exc:
23            raise ValidationError(
24                _(
25                    "Required actions must reference existing flows that a logged-in user "
26                    "can run, other than authentication or invalidation flows."
27                )
28            ) from exc
29        return attributes
class RequiredActionsUserSerializerMixin:
11class RequiredActionsUserSerializerMixin:
12    """Validate required actions on users"""
13
14    def validate_attributes(self, attributes: dict) -> dict:
15        """Reject required actions that cannot run"""
16        key = USER_ATTRIBUTE_REQUIRED_ACTIONS
17        current = self.instance.attributes if self.instance else {}
18        # Only validate changes, so a flow deleted later doesn't block unrelated edits
19        if key not in attributes or (key in current and attributes[key] == current[key]):
20            return attributes
21        try:
22            resolve_required_actions(attributes[key])
23        except ValueError as exc:
24            raise ValidationError(
25                _(
26                    "Required actions must reference existing flows that a logged-in user "
27                    "can run, other than authentication or invalidation flows."
28                )
29            ) from exc
30        return attributes

Validate required actions on users

def validate_attributes(self, attributes: dict) -> dict:
14    def validate_attributes(self, attributes: dict) -> dict:
15        """Reject required actions that cannot run"""
16        key = USER_ATTRIBUTE_REQUIRED_ACTIONS
17        current = self.instance.attributes if self.instance else {}
18        # Only validate changes, so a flow deleted later doesn't block unrelated edits
19        if key not in attributes or (key in current and attributes[key] == current[key]):
20            return attributes
21        try:
22            resolve_required_actions(attributes[key])
23        except ValueError as exc:
24            raise ValidationError(
25                _(
26                    "Required actions must reference existing flows that a logged-in user "
27                    "can run, other than authentication or invalidation flows."
28                )
29            ) from exc
30        return attributes

Reject required actions that cannot run