authentik.enterprise.requests.api.request_rules

 1from typing import Any
 2
 3from django.utils.translation import gettext as _
 4from rest_framework.exceptions import ValidationError
 5from rest_framework.viewsets import ModelViewSet
 6
 7from authentik.core.api.used_by import UsedByMixin
 8from authentik.core.api.utils import ModelSerializer
 9from authentik.enterprise.api import EnterpriseRequiredMixin
10from authentik.enterprise.requests.models import RequestRule
11
12
13class RequestRuleSerializer(EnterpriseRequiredMixin, ModelSerializer):
14
15    def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:
16        request_flow = attrs.get("request_flow")
17        request = self.context.get("request")
18        if not request:
19            return attrs
20        brand_request_flow = request.brand.flow_request
21        if not request_flow and not brand_request_flow:
22            raise ValidationError(
23                _("A request flow must either be set on this rule, or on the brand.")
24            )
25        return attrs
26
27    class Meta:
28        model = RequestRule
29        fields = [
30            "uuid",
31            "pbm_uuid",
32            "policy_engine_mode",
33            "name",
34            "targets",
35            "notification_transports",
36            "notification_mode",
37            "min_reviewers",
38            "min_reviewers_is_per_group",
39            "request_flow",
40        ]
41
42
43class RequestRuleViewSet(UsedByMixin, ModelViewSet):
44
45    queryset = RequestRule.objects.all()
46    serializer_class = RequestRuleSerializer
47    search_fields = ["name"]
48    ordering = ["name"]
49    filterset_fields = ["name", "pbm_uuid", "request_flow__slug"]
14class RequestRuleSerializer(EnterpriseRequiredMixin, ModelSerializer):
15
16    def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:
17        request_flow = attrs.get("request_flow")
18        request = self.context.get("request")
19        if not request:
20            return attrs
21        brand_request_flow = request.brand.flow_request
22        if not request_flow and not brand_request_flow:
23            raise ValidationError(
24                _("A request flow must either be set on this rule, or on the brand.")
25            )
26        return attrs
27
28    class Meta:
29        model = RequestRule
30        fields = [
31            "uuid",
32            "pbm_uuid",
33            "policy_engine_mode",
34            "name",
35            "targets",
36            "notification_transports",
37            "notification_mode",
38            "min_reviewers",
39            "min_reviewers_is_per_group",
40            "request_flow",
41        ]

Mixin to validate that a valid enterprise license exists before allowing to save the object

def validate(self, attrs: dict[str, typing.Any]) -> dict[str, typing.Any]:
16    def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:
17        request_flow = attrs.get("request_flow")
18        request = self.context.get("request")
19        if not request:
20            return attrs
21        brand_request_flow = request.brand.flow_request
22        if not request_flow and not brand_request_flow:
23            raise ValidationError(
24                _("A request flow must either be set on this rule, or on the brand.")
25            )
26        return attrs

Check that a valid license exists

class RequestRuleSerializer.Meta:
28    class Meta:
29        model = RequestRule
30        fields = [
31            "uuid",
32            "pbm_uuid",
33            "policy_engine_mode",
34            "name",
35            "targets",
36            "notification_transports",
37            "notification_mode",
38            "min_reviewers",
39            "min_reviewers_is_per_group",
40            "request_flow",
41        ]
fields = ['uuid', 'pbm_uuid', 'policy_engine_mode', 'name', 'targets', 'notification_transports', 'notification_mode', 'min_reviewers', 'min_reviewers_is_per_group', 'request_flow']
class RequestRuleViewSet(authentik.core.api.used_by.UsedByMixin, rest_framework.viewsets.ModelViewSet):
44class RequestRuleViewSet(UsedByMixin, ModelViewSet):
45
46    queryset = RequestRule.objects.all()
47    serializer_class = RequestRuleSerializer
48    search_fields = ["name"]
49    ordering = ["name"]
50    filterset_fields = ["name", "pbm_uuid", "request_flow__slug"]

Mixin to add a used_by endpoint to return a list of all objects using this object

queryset = <InheritanceQuerySet []>
serializer_class = <class 'RequestRuleSerializer'>
search_fields = ['name']
ordering = ['name']
filterset_fields = ['name', 'pbm_uuid', 'request_flow__slug']
name = None
description = None
suffix = None
detail = None
basename = None