From f62786e58b6b4b3a7b994df1d1bd9946634944ae Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Fri, 15 Oct 2021 17:49:08 +0200 Subject: [PATCH] policies: add additional filters to create flow charts on frontend Signed-off-by: Jens Langhammer --- authentik/policies/api/bindings.py | 21 ++++++++++++++++++++- schema.yml | 14 ++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/authentik/policies/api/bindings.py b/authentik/policies/api/bindings.py index 844c9acde..e6214d91a 100644 --- a/authentik/policies/api/bindings.py +++ b/authentik/policies/api/bindings.py @@ -2,6 +2,8 @@ from typing import OrderedDict from django.core.exceptions import ObjectDoesNotExist +from django_filters.filters import BooleanFilter, ModelMultipleChoiceFilter +from django_filters.filterset import FilterSet from rest_framework.serializers import ModelSerializer, PrimaryKeyRelatedField, ValidationError from rest_framework.viewsets import ModelViewSet from structlog.stdlib import get_logger @@ -96,6 +98,22 @@ class PolicyBindingSerializer(ModelSerializer): return attrs +class PolicyBindingFilter(FilterSet): + """Filter for PolicyBindings""" + + target_in = ModelMultipleChoiceFilter( + field_name="target__pbm_uuid", + to_field_name="pbm_uuid", + queryset=PolicyBindingModel.objects.select_subclasses(), + ) + policy__isnull = BooleanFilter("policy", "isnull") + + class Meta: + + model = PolicyBinding + fields = ["policy", "policy__isnull", "target", "target_in", "enabled", "order", "timeout"] + + class PolicyBindingViewSet(UsedByMixin, ModelViewSet): """PolicyBinding Viewset""" @@ -105,5 +123,6 @@ class PolicyBindingViewSet(UsedByMixin, ModelViewSet): .prefetch_related("policy") ) # prefetching policy so we resolve the subclass serializer_class = PolicyBindingSerializer - filterset_fields = ["policy", "target", "enabled", "order", "timeout"] search_fields = ["policy__name"] + filterset_class = PolicyBindingFilter + ordering = ["target", "order"] diff --git a/schema.yml b/schema.yml index 7f1bf2222..285e8a846 100644 --- a/schema.yml +++ b/schema.yml @@ -7033,6 +7033,10 @@ paths: schema: type: string format: uuid + - in: query + name: policy__isnull + schema: + type: boolean - name: search required: false in: query @@ -7044,6 +7048,15 @@ paths: schema: type: string format: uuid + - in: query + name: target_in + schema: + type: array + items: + type: string + format: uuid + explode: true + style: form - in: query name: timeout schema: @@ -29418,6 +29431,7 @@ components: - SUCCESSFUL - WARNING - ERROR + - UNKNOWN type: string SubModeEnum: enum: