From 58a7d67922c6bb773e187583dc014d670d9db5dd Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 30 Mar 2021 17:53:43 +0200 Subject: [PATCH] web/admin: migrate property mapping test to web Signed-off-by: Jens Langhammer --- authentik/admin/forms/__init__.py | 0 authentik/admin/forms/policies.py | 12 --- .../templates/administration/policy/test.html | 46 ------------ .../administration/property_mapping/test.html | 28 ------- authentik/admin/urls.py | 10 --- authentik/admin/views/policies.py | 47 +----------- authentik/admin/views/property_mappings.py | 48 ------------ .../PropertyMappingListPage.ts | 19 +++-- .../PropertyMappingTestForm.ts | 74 +++++++++++++++++++ 9 files changed, 89 insertions(+), 195 deletions(-) delete mode 100644 authentik/admin/forms/__init__.py delete mode 100644 authentik/admin/forms/policies.py delete mode 100644 authentik/admin/templates/administration/policy/test.html delete mode 100644 authentik/admin/templates/administration/property_mapping/test.html create mode 100644 web/src/pages/property-mappings/PropertyMappingTestForm.ts diff --git a/authentik/admin/forms/__init__.py b/authentik/admin/forms/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/authentik/admin/forms/policies.py b/authentik/admin/forms/policies.py deleted file mode 100644 index 17112b588..000000000 --- a/authentik/admin/forms/policies.py +++ /dev/null @@ -1,12 +0,0 @@ -"""authentik administration forms""" -from django import forms - -from authentik.admin.fields import CodeMirrorWidget, YAMLField -from authentik.core.models import User - - -class PolicyTestForm(forms.Form): - """Form to test policies against user""" - - user = forms.ModelChoiceField(queryset=User.objects.all()) - context = YAMLField(widget=CodeMirrorWidget(), required=False, initial=dict) diff --git a/authentik/admin/templates/administration/policy/test.html b/authentik/admin/templates/administration/policy/test.html deleted file mode 100644 index aa00f162f..000000000 --- a/authentik/admin/templates/administration/policy/test.html +++ /dev/null @@ -1,46 +0,0 @@ -{% extends 'generic/form.html' %} - -{% load i18n %} - -{% block above_form %} -

{% 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 %} -{% trans 'Test' %} -{% endblock %} diff --git a/authentik/admin/templates/administration/property_mapping/test.html b/authentik/admin/templates/administration/property_mapping/test.html deleted file mode 100644 index d52dcff56..000000000 --- a/authentik/admin/templates/administration/property_mapping/test.html +++ /dev/null @@ -1,28 +0,0 @@ -{% extends 'generic/form.html' %} - -{% load i18n %} - -{% block above_form %} -

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

-{% endblock %} - -{% block beneath_form %} -{% if result %} -
-
- -
-
-
- -
-
-
-{% endif %} -{% endblock %} - -{% block action %} -{% trans 'Test' %} -{% endblock %} diff --git a/authentik/admin/urls.py b/authentik/admin/urls.py index 56b3769a8..6d4ad1b51 100644 --- a/authentik/admin/urls.py +++ b/authentik/admin/urls.py @@ -30,11 +30,6 @@ urlpatterns = [ policies.PolicyUpdateView.as_view(), name="policy-update", ), - path( - "policies//test/", - policies.PolicyTestView.as_view(), - name="policy-test", - ), # Policy bindings path( "policies/bindings/create/", @@ -108,11 +103,6 @@ urlpatterns = [ property_mappings.PropertyMappingUpdateView.as_view(), name="property-mapping-update", ), - path( - "property-mappings//test/", - property_mappings.PropertyMappingTestView.as_view(), - name="property-mapping-test", - ), # Outpost Service Connections path( "outpost_service_connections/create/", diff --git a/authentik/admin/views/policies.py b/authentik/admin/views/policies.py index 93ea9f50e..c08017583 100644 --- a/authentik/admin/views/policies.py +++ b/authentik/admin/views/policies.py @@ -1,22 +1,15 @@ """authentik Policy administration""" -from typing import Any - 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 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 InheritanceCreateView, InheritanceUpdateView -from authentik.policies.models import Policy, PolicyBinding -from authentik.policies.process import PolicyProcess, PolicyRequest +from authentik.policies.models import Policy class PolicyCreateView( @@ -49,41 +42,3 @@ class PolicyUpdateView( template_name = "generic/update.html" success_url = reverse_lazy("authentik_core:if-admin") success_message = _("Successfully updated Policy") - - -class PolicyTestView(LoginRequiredMixin, DetailView, PermissionRequiredMixin, FormView): - """View to test policy(s)""" - - model = Policy - form_class = PolicyTestForm - permission_required = "authentik_policies.view_policy" - template_name = "administration/policy/test.html" - object = None - - def get_object(self, queryset=None) -> Policy: - return ( - Policy.objects.filter(pk=self.kwargs.get("pk")).select_subclasses().first() - ) - - def get_context_data(self, **kwargs: Any) -> dict[str, Any]: - kwargs["policy"] = self.get_object() - return super().get_context_data(**kwargs) - - def post(self, *args, **kwargs) -> HttpResponse: - self.object = self.get_object() - return super().post(*args, **kwargs) - - def form_valid(self, form: PolicyTestForm) -> HttpResponse: - policy = self.get_object() - user = form.cleaned_data.get("user") - - p_request = PolicyRequest(user) - p_request.debug = True - p_request.set_http_request(self.request) - p_request.context = form.cleaned_data.get("context", {}) - - proc = PolicyProcess(PolicyBinding(policy=policy), p_request, None) - result = proc.execute() - 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 a276a1a01..99b3d51e2 100644 --- a/authentik/admin/views/property_mappings.py +++ b/authentik/admin/views/property_mappings.py @@ -1,19 +1,12 @@ """authentik PropertyMapping administration""" -from json import dumps -from typing import Any - 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.utils.translation import gettext as _ -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 InheritanceCreateView, InheritanceUpdateView from authentik.core.models import PropertyMapping @@ -46,44 +39,3 @@ class PropertyMappingUpdateView( success_url = "/" template_name = "generic/update.html" success_message = _("Successfully updated Property Mapping") - - -class PropertyMappingTestView( - LoginRequiredMixin, DetailView, PermissionRequiredMixin, FormView -): - """View to test property mappings""" - - model = PropertyMapping - form_class = PolicyTestForm - permission_required = "authentik_core.view_propertymapping" - template_name = "administration/property_mapping/test.html" - object = None - - def get_object(self, queryset=None) -> PropertyMapping: - return ( - PropertyMapping.objects.filter(pk=self.kwargs.get("pk")) - .select_subclasses() - .first() - ) - - def get_context_data(self, **kwargs: Any) -> dict[str, Any]: - kwargs["property_mapping"] = self.get_object() - return super().get_context_data(**kwargs) - - def post(self, *args, **kwargs) -> HttpResponse: - self.object = self.get_object() - return super().post(*args, **kwargs) - - def form_valid(self, form: PolicyTestForm) -> HttpResponse: - mapping = self.get_object() - user = form.cleaned_data.get("user") - - context = self.get_context_data(form=form) - try: - 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) - return self.render_to_response(context) diff --git a/web/src/pages/property-mappings/PropertyMappingListPage.ts b/web/src/pages/property-mappings/PropertyMappingListPage.ts index 7b7dc4e3e..5c3e8738e 100644 --- a/web/src/pages/property-mappings/PropertyMappingListPage.ts +++ b/web/src/pages/property-mappings/PropertyMappingListPage.ts @@ -7,6 +7,8 @@ import "../../elements/buttons/ModalButton"; import "../../elements/buttons/Dropdown"; import "../../elements/buttons/SpinnerButton"; import "../../elements/forms/DeleteForm"; +import "../../elements/forms/ModalForm"; +import "./PropertyMappingTestForm"; import { TableColumn } from "../../elements/table/Table"; import { until } from "lit-html/directives/until"; import { PAGE_SIZE } from "../../constants"; @@ -64,12 +66,19 @@ export class PropertyMappingListPage extends TablePage {
- - + + ${gettext("Test")} - -
-
+ + + ${gettext("Test Property Mapping")} + + + + + { + + @property({attribute: false}) + mapping?: PropertyMapping; + + @property({ attribute: false}) + result?: PropertyMappingTestResult; + + getSuccessMessage(): string { + return gettext("Successfully sent test-request."); + } + + send = (data: PolicyTest): Promise => { + return new PropertymappingsApi(DEFAULT_CONFIG).propertymappingsAllTest({ + pmUuid: this.mapping?.pk || "", + data: data + }).then(result => this.result = result); + }; + + renderResult(): TemplateResult { + return html` + ${this.result?.successful ? + html` + `: + html` +
+
+ ${this.result?.result} +
+
`} +
`; + } + + renderForm(): TemplateResult { + return html`
+ + + + + + + + ${this.result ? this.renderResult(): html``} +
`; + } + +}