From 8ae7403abce73ec0ead087f2d5f8d686227c7159 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Fri, 23 Jul 2021 19:35:41 +0200 Subject: [PATCH] core: add group filter by member username and pk Signed-off-by: Jens Langhammer --- authentik/core/api/groups.py | 23 ++++++++++++++++++++++- schema.yml | 18 ++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/authentik/core/api/groups.py b/authentik/core/api/groups.py index d08731756..6ca292fcb 100644 --- a/authentik/core/api/groups.py +++ b/authentik/core/api/groups.py @@ -1,5 +1,7 @@ """Groups API Viewset""" from django.db.models.query import QuerySet +from django_filters.filters import ModelMultipleChoiceFilter +from django_filters.filterset import FilterSet from rest_framework.fields import BooleanField, CharField, JSONField from rest_framework.serializers import ListSerializer, ModelSerializer from rest_framework.viewsets import ModelViewSet @@ -57,13 +59,32 @@ class GroupSerializer(ModelSerializer): ] +class GroupFilter(FilterSet): + """Filter for groups""" + + members_by_username = ModelMultipleChoiceFilter( + field_name="users__username", + to_field_name="username", + queryset=User.objects.all(), + ) + members_by_pk = ModelMultipleChoiceFilter( + field_name="users", + queryset=User.objects.all(), + ) + + class Meta: + + model = Group + fields = ["name", "is_superuser", "members_by_pk", "members_by_username"] + + class GroupViewSet(UsedByMixin, ModelViewSet): """Group Viewset""" queryset = Group.objects.all() serializer_class = GroupSerializer search_fields = ["name", "is_superuser"] - filterset_fields = ["name", "is_superuser"] + filterset_class = GroupFilter ordering = ["name"] def _filter_queryset_for_list(self, queryset: QuerySet) -> QuerySet: diff --git a/schema.yml b/schema.yml index 5af8b357b..188405b40 100644 --- a/schema.yml +++ b/schema.yml @@ -1932,6 +1932,24 @@ paths: name: is_superuser schema: type: boolean + - in: query + name: members_by_pk + schema: + type: array + items: + type: integer + explode: true + style: form + - in: query + name: members_by_username + schema: + type: array + items: + type: string + description: Required. 150 characters or fewer. Letters, digits and @/./+/-/_ + only. + explode: true + style: form - in: query name: name schema: