sources/*: switch API to use slug in URL

This commit is contained in:
Jens Langhammer 2021-02-09 16:08:24 +01:00
parent 101f916247
commit 552f8c6a9a
6 changed files with 36 additions and 29 deletions

View file

@ -1,4 +1,6 @@
"""Source API Views""" """Source API Views"""
from datetime import datetime
from django.core.cache import cache from django.core.cache import cache
from django.db.models.base import Model from django.db.models.base import Model
from drf_yasg2.utils import swagger_auto_schema from drf_yasg2.utils import swagger_auto_schema
@ -58,14 +60,19 @@ class LDAPSourceViewSet(ModelViewSet):
queryset = LDAPSource.objects.all() queryset = LDAPSource.objects.all()
serializer_class = LDAPSourceSerializer serializer_class = LDAPSourceSerializer
lookup_field = "slug"
@swagger_auto_schema(responses={200: LDAPSourceSyncStatusSerializer(many=False)}) @swagger_auto_schema(responses={200: LDAPSourceSyncStatusSerializer(many=False)})
@action(methods=["GET"], detail=True) @action(methods=["GET"], detail=True)
# pylint: disable=invalid-name # pylint: disable=invalid-name
def sync_status(self, request: Request, pk: int) -> Response: def sync_status(self, request: Request, slug: str) -> Response:
source = self.get_object() source = self.get_object()
last_sync = cache.get(source.state_cache_prefix("last_sync"), None) last_sync = cache.get(source.state_cache_prefix("last_sync"), None)
return Response(LDAPSourceSyncStatusSerializer({"last_sync": last_sync}).data) return Response(
LDAPSourceSyncStatusSerializer(
{"last_sync": datetime.fromtimestamp(last_sync)}
).data
)
class LDAPPropertyMappingSerializer(ModelSerializer, MetaNameSerializer): class LDAPPropertyMappingSerializer(ModelSerializer, MetaNameSerializer):

View file

@ -91,16 +91,6 @@ class LDAPSource(Source):
"""Key by which the ldap source status is saved""" """Key by which the ldap source status is saved"""
return f"source_ldap_{self.pk}_state_{suffix}" return f"source_ldap_{self.pk}_state_{suffix}"
@property
def ui_additional_info(self) -> str:
last_sync = cache.get(self.state_cache_prefix("last_sync"), None)
if last_sync:
last_sync = datetime.fromtimestamp(last_sync)
return render_to_string(
"ldap/source_list_status.html", {"source": self, "last_sync": last_sync}
)
_connection: Optional[Connection] = None _connection: Optional[Connection] = None
@property @property

View file

@ -4,6 +4,7 @@ from time import time
from django.core.cache import cache from django.core.cache import cache
from django.utils.text import slugify from django.utils.text import slugify
from ldap3.core.exceptions import LDAPException from ldap3.core.exceptions import LDAPException
from structlog.stdlib import get_logger
from authentik.events.monitored_tasks import MonitoredTask, TaskResult, TaskResultStatus from authentik.events.monitored_tasks import MonitoredTask, TaskResult, TaskResultStatus
from authentik.root.celery import CELERY_APP from authentik.root.celery import CELERY_APP
@ -12,6 +13,8 @@ from authentik.sources.ldap.sync.groups import GroupLDAPSynchronizer
from authentik.sources.ldap.sync.membership import MembershipLDAPSynchronizer from authentik.sources.ldap.sync.membership import MembershipLDAPSynchronizer
from authentik.sources.ldap.sync.users import UserLDAPSynchronizer from authentik.sources.ldap.sync.users import UserLDAPSynchronizer
LOGGER = get_logger()
@CELERY_APP.task() @CELERY_APP.task()
def ldap_sync_all(): def ldap_sync_all():
@ -21,7 +24,7 @@ def ldap_sync_all():
@CELERY_APP.task(bind=True, base=MonitoredTask) @CELERY_APP.task(bind=True, base=MonitoredTask)
def ldap_sync(self: MonitoredTask, source_pk: int): def ldap_sync(self: MonitoredTask, source_pk: str):
"""Synchronization of an LDAP Source""" """Synchronization of an LDAP Source"""
try: try:
source: LDAPSource = LDAPSource.objects.get(pk=source_pk) source: LDAPSource = LDAPSource.objects.get(pk=source_pk)
@ -49,4 +52,5 @@ def ldap_sync(self: MonitoredTask, source_pk: int):
) )
) )
except LDAPException as exc: except LDAPException as exc:
LOGGER.debug(exc)
self.set_status(TaskResult(TaskResultStatus.ERROR).with_error(exc)) self.set_status(TaskResult(TaskResultStatus.ERROR).with_error(exc))

View file

@ -26,3 +26,4 @@ class OAuthSourceViewSet(ModelViewSet):
queryset = OAuthSource.objects.all() queryset = OAuthSource.objects.all()
serializer_class = OAuthSourceSerializer serializer_class = OAuthSourceSerializer
lookup_field = "slug"

View file

@ -30,3 +30,4 @@ class SAMLSourceViewSet(ModelViewSet):
queryset = SAMLSource.objects.all() queryset = SAMLSource.objects.all()
serializer_class = SAMLSourceSerializer serializer_class = SAMLSourceSerializer
lookup_field = "slug"

View file

@ -4917,7 +4917,7 @@ paths:
tags: tags:
- sources - sources
parameters: [] parameters: []
/sources/ldap/{pbm_uuid}/: /sources/ldap/{slug}/:
get: get:
operationId: sources_ldap_read operationId: sources_ldap_read
description: LDAP Source Viewset description: LDAP Source Viewset
@ -4971,13 +4971,14 @@ paths:
tags: tags:
- sources - sources
parameters: parameters:
- name: pbm_uuid - name: slug
in: path in: path
description: A UUID string identifying this LDAP Source. description: Internal source name, used in URLs.
required: true required: true
type: string type: string
format: uuid format: slug
/sources/ldap/{pbm_uuid}/sync_status/: pattern: ^[-a-zA-Z0-9_]+$
/sources/ldap/{slug}/sync_status/:
get: get:
operationId: sources_ldap_sync_status operationId: sources_ldap_sync_status
description: LDAP Source Viewset description: LDAP Source Viewset
@ -4990,12 +4991,13 @@ paths:
tags: tags:
- sources - sources
parameters: parameters:
- name: pbm_uuid - name: slug
in: path in: path
description: A UUID string identifying this LDAP Source. description: Internal source name, used in URLs.
required: true required: true
type: string type: string
format: uuid format: slug
pattern: ^[-a-zA-Z0-9_]+$
/sources/oauth/: /sources/oauth/:
get: get:
operationId: sources_oauth_list operationId: sources_oauth_list
@ -5063,7 +5065,7 @@ paths:
tags: tags:
- sources - sources
parameters: [] parameters: []
/sources/oauth/{pbm_uuid}/: /sources/oauth/{slug}/:
get: get:
operationId: sources_oauth_read operationId: sources_oauth_read
description: Source Viewset description: Source Viewset
@ -5117,12 +5119,13 @@ paths:
tags: tags:
- sources - sources
parameters: parameters:
- name: pbm_uuid - name: slug
in: path in: path
description: A UUID string identifying this Generic OAuth Source. description: Internal source name, used in URLs.
required: true required: true
type: string type: string
format: uuid format: slug
pattern: ^[-a-zA-Z0-9_]+$
/sources/saml/: /sources/saml/:
get: get:
operationId: sources_saml_list operationId: sources_saml_list
@ -5190,7 +5193,7 @@ paths:
tags: tags:
- sources - sources
parameters: [] parameters: []
/sources/saml/{pbm_uuid}/: /sources/saml/{slug}/:
get: get:
operationId: sources_saml_read operationId: sources_saml_read
description: SAMLSource Viewset description: SAMLSource Viewset
@ -5244,12 +5247,13 @@ paths:
tags: tags:
- sources - sources
parameters: parameters:
- name: pbm_uuid - name: slug
in: path in: path
description: A UUID string identifying this SAML Source. description: Internal source name, used in URLs.
required: true required: true
type: string type: string
format: uuid format: slug
pattern: ^[-a-zA-Z0-9_]+$
/stages/all/: /stages/all/:
get: get:
operationId: stages_all_list operationId: stages_all_list