authentik.policies.event_matcher.api

Event Matcher Policy API

 1"""Event Matcher Policy API"""
 2
 3from django.utils.translation import gettext_lazy as _
 4from rest_framework.exceptions import ValidationError
 5from rest_framework.fields import ChoiceField
 6from rest_framework.viewsets import ModelViewSet
 7
 8from authentik.core.api.used_by import UsedByMixin
 9from authentik.lib.api import app_choices, model_choices
10from authentik.policies.api.policies import PolicySerializer
11from authentik.policies.event_matcher.models import EventMatcherPolicy
12
13
14class EventMatcherPolicySerializer(PolicySerializer):
15    """Event Matcher Policy Serializer"""
16
17    app = ChoiceField(
18        choices=app_choices(),
19        required=False,
20        allow_null=True,
21        help_text=_(
22            "Match events created by selected application. When left empty, "
23            "all applications are matched."
24        ),
25    )
26    model = ChoiceField(
27        choices=model_choices(),
28        required=False,
29        allow_null=True,
30        help_text=_(
31            "Match events created by selected model. "
32            "When left empty, all models are matched. When an app is selected, "
33            "all the application's models are matched."
34        ),
35    )
36
37    def validate(self, attrs: dict) -> dict:
38        criteria = ["action", "client_ip", "app", "model", "query"]
39        # A partial update omits unchanged criteria, so fall back to the stored values
40        if not any(attrs.get(field, getattr(self.instance, field, None)) for field in criteria):
41            raise ValidationError(_("At least one criteria must be set."))
42        return super().validate(attrs)
43
44    class Meta:
45        model = EventMatcherPolicy
46        fields = PolicySerializer.Meta.fields + ["action", "client_ip", "app", "model", "query"]
47
48
49class EventMatcherPolicyViewSet(UsedByMixin, ModelViewSet):
50    """Event Matcher Policy Viewset"""
51
52    queryset = EventMatcherPolicy.objects.all()
53    serializer_class = EventMatcherPolicySerializer
54    filterset_fields = "__all__"
55    ordering = ["name"]
56    search_fields = ["name"]
class EventMatcherPolicySerializer(authentik.policies.api.policies.PolicySerializer):
15class EventMatcherPolicySerializer(PolicySerializer):
16    """Event Matcher Policy Serializer"""
17
18    app = ChoiceField(
19        choices=app_choices(),
20        required=False,
21        allow_null=True,
22        help_text=_(
23            "Match events created by selected application. When left empty, "
24            "all applications are matched."
25        ),
26    )
27    model = ChoiceField(
28        choices=model_choices(),
29        required=False,
30        allow_null=True,
31        help_text=_(
32            "Match events created by selected model. "
33            "When left empty, all models are matched. When an app is selected, "
34            "all the application's models are matched."
35        ),
36    )
37
38    def validate(self, attrs: dict) -> dict:
39        criteria = ["action", "client_ip", "app", "model", "query"]
40        # A partial update omits unchanged criteria, so fall back to the stored values
41        if not any(attrs.get(field, getattr(self.instance, field, None)) for field in criteria):
42            raise ValidationError(_("At least one criteria must be set."))
43        return super().validate(attrs)
44
45    class Meta:
46        model = EventMatcherPolicy
47        fields = PolicySerializer.Meta.fields + ["action", "client_ip", "app", "model", "query"]

Event Matcher Policy Serializer

app
model
def validate(self, attrs: dict) -> dict:
38    def validate(self, attrs: dict) -> dict:
39        criteria = ["action", "client_ip", "app", "model", "query"]
40        # A partial update omits unchanged criteria, so fall back to the stored values
41        if not any(attrs.get(field, getattr(self.instance, field, None)) for field in criteria):
42            raise ValidationError(_("At least one criteria must be set."))
43        return super().validate(attrs)
class EventMatcherPolicySerializer.Meta:
45    class Meta:
46        model = EventMatcherPolicy
47        fields = PolicySerializer.Meta.fields + ["action", "client_ip", "app", "model", "query"]
fields = ['pk', 'name', 'execution_logging', 'component', 'verbose_name', 'verbose_name_plural', 'meta_model_name', 'bound_to', 'last_updated', 'created', 'action', 'client_ip', 'app', 'model', 'query']
class EventMatcherPolicyViewSet(authentik.core.api.used_by.UsedByMixin, rest_framework.viewsets.ModelViewSet):
50class EventMatcherPolicyViewSet(UsedByMixin, ModelViewSet):
51    """Event Matcher Policy Viewset"""
52
53    queryset = EventMatcherPolicy.objects.all()
54    serializer_class = EventMatcherPolicySerializer
55    filterset_fields = "__all__"
56    ordering = ["name"]
57    search_fields = ["name"]

Event Matcher Policy Viewset

queryset = <InheritanceQuerySet []>
serializer_class = <class 'EventMatcherPolicySerializer'>
filterset_fields = '__all__'
ordering = ['name']
search_fields = ['name']
name = None
description = None
suffix = None
detail = None
basename = None