From 1101810feae4990e68618b753bce20f68a9065c7 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Wed, 3 Feb 2021 22:04:32 +0100 Subject: [PATCH] =?UTF-8?q?admin:=20show=20more=C2=A0details=20for=20polic?= =?UTF-8?q?y=20testing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- authentik/admin/tasks.py | 4 +- .../templates/administration/policy/test.html | 37 ++++++++++++++++++- authentik/admin/views/policies.py | 9 ++--- authentik/admin/views/property_mappings.py | 28 +++++++------- 4 files changed, 56 insertions(+), 22 deletions(-) diff --git a/authentik/admin/tasks.py b/authentik/admin/tasks.py index 3d5f937f1..bc20cf684 100644 --- a/authentik/admin/tasks.py +++ b/authentik/admin/tasks.py @@ -45,8 +45,8 @@ def update_latest_version(self: MonitoredTask): ).exists(): return event_dict = {"new_version": upstream_version} - if m := re.search(URL_FINDER, data.get("body", "")): - event_dict["message"] = f"Changelog: {m.group()}" + if match := re.search(URL_FINDER, data.get("body", "")): + event_dict["message"] = f"Changelog: {match.group()}" Event.new(EventAction.UPDATE_AVAILABLE, **event_dict).save() except (RequestException, IndexError) as exc: cache.set(VERSION_CACHE_KEY, "0.0.0", VERSION_CACHE_TIMEOUT) diff --git a/authentik/admin/templates/administration/policy/test.html b/authentik/admin/templates/administration/policy/test.html index ae4ebf84c..5110d19dd 100644 --- a/authentik/admin/templates/administration/policy/test.html +++ b/authentik/admin/templates/administration/policy/test.html @@ -3,7 +3,42 @@ {% load i18n %} {% block above_form %} -

{% blocktrans with policy=policy %}Test policy {{ policy }}{% endblocktrans %}

+

{% blocktrans with policy=policy %}Test {{ policy }}{% endblocktrans %}

+{% endblock %} + +{% block beneath_form %} +{% if result %} +
+
+ +
+
+
+ {{ result.passing|yesno:"Yes,No" }} +
+
+
+
+
+ +
+
+
+
    + {% for m in result.messages %} +
  • {{ m }}
  • + {% empty %} +
  • -
  • + {% endfor %} +
+
+
+
+{% endif %} {% endblock %} {% block action %} diff --git a/authentik/admin/views/policies.py b/authentik/admin/views/policies.py index a36c6b713..fd25167bf 100644 --- a/authentik/admin/views/policies.py +++ b/authentik/admin/views/policies.py @@ -1,7 +1,6 @@ """authentik Policy administration""" from typing import Any, Dict -from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import ( PermissionRequiredMixin as DjangoPermissionRequiredMixin, @@ -121,8 +120,6 @@ class PolicyTestView(LoginRequiredMixin, DetailView, PermissionRequiredMixin, Fo proc = PolicyProcess(PolicyBinding(policy=policy), p_request, None) result = proc.execute() - if result.passing: - messages.success(self.request, _("User successfully passed policy.")) - else: - messages.error(self.request, _("User didn't pass policy.")) - return self.render_to_response(self.get_context_data(form=form, result=result)) + context = self.get_context_data(form=form) + context["result"] = result + return self.render_to_response(context) diff --git a/authentik/admin/views/property_mappings.py b/authentik/admin/views/property_mappings.py index 53e18329e..9df13cef5 100644 --- a/authentik/admin/views/property_mappings.py +++ b/authentik/admin/views/property_mappings.py @@ -1,29 +1,25 @@ """authentik PropertyMapping administration""" -from django.contrib.messages import views -from authentik.admin.forms.policies import PolicyTestForm -from django.http import HttpResponse from json import dumps from typing import Any -from django.db.models import QuerySet -from django.views.generic import FormView -from django.views.generic.detail import DetailView + from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import ( PermissionRequiredMixin as DjangoPermissionRequiredMixin, ) from django.contrib.messages.views import SuccessMessageMixin +from django.http import HttpResponse from django.urls import reverse_lazy from django.utils.translation import gettext as _ -from guardian.mixins import PermissionListMixin, PermissionRequiredMixin +from django.views.generic import FormView +from django.views.generic.detail import DetailView +from guardian.mixins import PermissionRequiredMixin +from authentik.admin.forms.policies import PolicyTestForm from authentik.admin.views.utils import ( BackSuccessUrlMixin, DeleteMessageView, InheritanceCreateView, - InheritanceListView, InheritanceUpdateView, - SearchListMixin, - UserPaginateListMixin, ) from authentik.core.models import PropertyMapping @@ -75,7 +71,9 @@ class PropertyMappingDeleteView( success_message = _("Successfully deleted Property Mapping") -class PropertyMappingTestView(LoginRequiredMixin, DetailView, PermissionRequiredMixin, FormView): +class PropertyMappingTestView( + LoginRequiredMixin, DetailView, PermissionRequiredMixin, FormView +): """View to test property mappings""" model = PropertyMapping @@ -86,7 +84,9 @@ class PropertyMappingTestView(LoginRequiredMixin, DetailView, PermissionRequired def get_object(self, queryset=None) -> PropertyMapping: return ( - PropertyMapping.objects.filter(pk=self.kwargs.get("pk")).select_subclasses().first() + PropertyMapping.objects.filter(pk=self.kwargs.get("pk")) + .select_subclasses() + .first() ) def get_context_data(self, **kwargs: Any) -> dict[str, Any]: @@ -103,7 +103,9 @@ class PropertyMappingTestView(LoginRequiredMixin, DetailView, PermissionRequired context = self.get_context_data(form=form) try: - result = mapping.evaluate(user, self.request, **form.cleaned_data.get("context", {})) + result = mapping.evaluate( + user, self.request, **form.cleaned_data.get("context", {}) + ) context["result"] = dumps(result, indent=4) except Exception as exc: # pylint: disable=broad-except context["result"] = str(exc)