From d092093e94a7a4288aec69e67ca23ccc01120086 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 13 Nov 2023 14:00:27 +0100 Subject: [PATCH] sources/ldap: separate API Signed-off-by: Jens Langhammer --- .../sources/ldap/api/property_mappings.py | 40 +++++++++++++++++++ .../sources/ldap/{api.py => api/sources.py} | 38 +----------------- authentik/sources/ldap/models.py | 4 +- authentik/sources/ldap/urls.py | 3 +- 4 files changed, 46 insertions(+), 39 deletions(-) create mode 100644 authentik/sources/ldap/api/property_mappings.py rename authentik/sources/ldap/{api.py => api/sources.py} (82%) diff --git a/authentik/sources/ldap/api/property_mappings.py b/authentik/sources/ldap/api/property_mappings.py new file mode 100644 index 000000000..ef7042b51 --- /dev/null +++ b/authentik/sources/ldap/api/property_mappings.py @@ -0,0 +1,40 @@ +"""Source API Views""" +from django_filters.filters import AllValuesMultipleFilter +from django_filters.filterset import FilterSet +from drf_spectacular.types import OpenApiTypes +from drf_spectacular.utils import extend_schema_field +from rest_framework.viewsets import ModelViewSet + +from authentik.core.api.propertymappings import PropertyMappingSerializer +from authentik.core.api.used_by import UsedByMixin +from authentik.sources.ldap.models import LDAPPropertyMapping + + +class LDAPPropertyMappingSerializer(PropertyMappingSerializer): + """LDAP PropertyMapping Serializer""" + + class Meta: + model = LDAPPropertyMapping + fields = PropertyMappingSerializer.Meta.fields + [ + "object_field", + ] + + +class LDAPPropertyMappingFilter(FilterSet): + """Filter for LDAPPropertyMapping""" + + managed = extend_schema_field(OpenApiTypes.STR)(AllValuesMultipleFilter(field_name="managed")) + + class Meta: + model = LDAPPropertyMapping + fields = "__all__" + + +class LDAPPropertyMappingViewSet(UsedByMixin, ModelViewSet): + """LDAP PropertyMapping Viewset""" + + queryset = LDAPPropertyMapping.objects.all() + serializer_class = LDAPPropertyMappingSerializer + filterset_class = LDAPPropertyMappingFilter + search_fields = ["name"] + ordering = ["name"] diff --git a/authentik/sources/ldap/api.py b/authentik/sources/ldap/api/sources.py similarity index 82% rename from authentik/sources/ldap/api.py rename to authentik/sources/ldap/api/sources.py index 08e530bbb..d91ab025a 100644 --- a/authentik/sources/ldap/api.py +++ b/authentik/sources/ldap/api/sources.py @@ -2,10 +2,7 @@ from typing import Any, Optional from django.core.cache import cache -from django_filters.filters import AllValuesMultipleFilter -from django_filters.filterset import FilterSet -from drf_spectacular.types import OpenApiTypes -from drf_spectacular.utils import extend_schema, extend_schema_field, inline_serializer +from drf_spectacular.utils import extend_schema, inline_serializer from rest_framework.decorators import action from rest_framework.exceptions import ValidationError from rest_framework.fields import BooleanField, DictField, ListField, SerializerMethodField @@ -15,13 +12,12 @@ from rest_framework.response import Response from rest_framework.viewsets import ModelViewSet from authentik.admin.api.tasks import TaskSerializer -from authentik.core.api.propertymappings import PropertyMappingSerializer from authentik.core.api.sources import SourceSerializer from authentik.core.api.used_by import UsedByMixin from authentik.core.api.utils import PassiveSerializer from authentik.crypto.models import CertificateKeyPair from authentik.events.monitored_tasks import TaskInfo -from authentik.sources.ldap.models import LDAPPropertyMapping, LDAPSource +from authentik.sources.ldap.models import LDAPSource from authentik.sources.ldap.tasks import CACHE_KEY_STATUS, SYNC_CLASSES @@ -169,33 +165,3 @@ class LDAPSourceViewSet(UsedByMixin, ModelViewSet): obj.pop("raw_dn", None) all_objects[class_name].append(obj) return Response(data=all_objects) - - -class LDAPPropertyMappingSerializer(PropertyMappingSerializer): - """LDAP PropertyMapping Serializer""" - - class Meta: - model = LDAPPropertyMapping - fields = PropertyMappingSerializer.Meta.fields + [ - "object_field", - ] - - -class LDAPPropertyMappingFilter(FilterSet): - """Filter for LDAPPropertyMapping""" - - managed = extend_schema_field(OpenApiTypes.STR)(AllValuesMultipleFilter(field_name="managed")) - - class Meta: - model = LDAPPropertyMapping - fields = "__all__" - - -class LDAPPropertyMappingViewSet(UsedByMixin, ModelViewSet): - """LDAP PropertyMapping Viewset""" - - queryset = LDAPPropertyMapping.objects.all() - serializer_class = LDAPPropertyMappingSerializer - filterset_class = LDAPPropertyMappingFilter - search_fields = ["name"] - ordering = ["name"] diff --git a/authentik/sources/ldap/models.py b/authentik/sources/ldap/models.py index a09791593..a020b20f6 100644 --- a/authentik/sources/ldap/models.py +++ b/authentik/sources/ldap/models.py @@ -115,7 +115,7 @@ class LDAPSource(Source): @property def serializer(self) -> type[Serializer]: - from authentik.sources.ldap.api import LDAPSourceSerializer + from authentik.sources.ldap.api.sources import LDAPSourceSerializer return LDAPSourceSerializer @@ -253,7 +253,7 @@ class LDAPPropertyMapping(PropertyMapping): @property def serializer(self) -> type[Serializer]: - from authentik.sources.ldap.api import LDAPPropertyMappingSerializer + from authentik.sources.ldap.api.property_mappings import LDAPPropertyMappingSerializer return LDAPPropertyMappingSerializer diff --git a/authentik/sources/ldap/urls.py b/authentik/sources/ldap/urls.py index 30390f332..4c2e11267 100644 --- a/authentik/sources/ldap/urls.py +++ b/authentik/sources/ldap/urls.py @@ -1,5 +1,6 @@ """API URLs""" -from authentik.sources.ldap.api import LDAPPropertyMappingViewSet, LDAPSourceViewSet +from authentik.sources.ldap.api.property_mappings import LDAPPropertyMappingViewSet +from authentik.sources.ldap.api.sources import LDAPSourceViewSet api_urlpatterns = [ ("propertymappings/ldap", LDAPPropertyMappingViewSet),