From 6bcdf36ca6c07ddb1828df27fd0d8e6a8c54f1a0 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 16 Feb 2021 20:00:52 +0100 Subject: [PATCH] admin: add ?provider for ApplicationCreateView --- authentik/admin/views/applications.py | 19 +++++++++++++++++++ authentik/core/forms/applications.py | 2 +- authentik/providers/saml/api.py | 2 +- authentik/providers/saml/views/metadata.py | 1 - 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/authentik/admin/views/applications.py b/authentik/admin/views/applications.py index abaaa4292..88e72940c 100644 --- a/authentik/admin/views/applications.py +++ b/authentik/admin/views/applications.py @@ -1,4 +1,6 @@ """authentik Application administration""" +from typing import Any + from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import ( PermissionRequiredMixin as DjangoPermissionRequiredMixin, @@ -7,6 +9,7 @@ from django.contrib.messages.views import SuccessMessageMixin from django.utils.translation import gettext as _ from django.views.generic import UpdateView from guardian.mixins import PermissionRequiredMixin +from guardian.shortcuts import get_objects_for_user from authentik.admin.views.utils import BackSuccessUrlMixin, DeleteMessageView from authentik.core.forms.applications import ApplicationForm @@ -30,6 +33,22 @@ class ApplicationCreateView( template_name = "generic/create.html" success_message = _("Successfully created Application") + def get_initial(self) -> dict[str, Any]: + if "provider" in self.request.GET: + try: + initial_provider_pk = int(self.request.GET["provider"]) + except ValueError: + return super().get_initial() + providers = ( + get_objects_for_user(self.request.user, "authentik_core.view_provider") + .filter(pk=initial_provider_pk) + .select_subclasses() + ) + if not providers.exists(): + return {} + return {"provider": providers.first()} + return super().get_initial() + class ApplicationUpdateView( SuccessMessageMixin, diff --git a/authentik/core/forms/applications.py b/authentik/core/forms/applications.py index db31e20a6..b5ff6fba3 100644 --- a/authentik/core/forms/applications.py +++ b/authentik/core/forms/applications.py @@ -9,7 +9,7 @@ from authentik.lib.widgets import GroupedModelChoiceField class ApplicationForm(forms.ModelForm): """Application Form""" - def __init__(self, *args, **kwargs): + def __init__(self, *args, **kwargs): # pragma: no cover super().__init__(*args, **kwargs) self.fields["provider"].queryset = ( Provider.objects.all().order_by("pk").select_subclasses() diff --git a/authentik/providers/saml/api.py b/authentik/providers/saml/api.py index 150e8a816..cf7ed1d75 100644 --- a/authentik/providers/saml/api.py +++ b/authentik/providers/saml/api.py @@ -64,7 +64,7 @@ class SAMLProviderViewSet(ModelViewSet): try: metadata = DescriptorDownloadView.get_metadata(request, provider) return Response({"metadata": metadata}) - except Provider.application.RelatedObjectDoesNotExist: + except Provider.application.RelatedObjectDoesNotExist: # pylint: disable=no-member return Response({"metadata": ""}) diff --git a/authentik/providers/saml/views/metadata.py b/authentik/providers/saml/views/metadata.py index 1865a88cd..7ba1b326d 100644 --- a/authentik/providers/saml/views/metadata.py +++ b/authentik/providers/saml/views/metadata.py @@ -4,7 +4,6 @@ from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin from django.http import HttpRequest, HttpResponse from django.shortcuts import get_object_or_404 -from django.urls.base import reverse_lazy from django.utils.translation import gettext_lazy as _ from django.views import View from django.views.generic.edit import FormView