admin: show more details for policy testing

This commit is contained in:
Jens Langhammer 2021-02-03 22:04:32 +01:00
parent 1ab5289e2e
commit 1101810fea
4 changed files with 56 additions and 22 deletions

View File

@ -45,8 +45,8 @@ def update_latest_version(self: MonitoredTask):
).exists(): ).exists():
return return
event_dict = {"new_version": upstream_version} event_dict = {"new_version": upstream_version}
if m := re.search(URL_FINDER, data.get("body", "")): if match := re.search(URL_FINDER, data.get("body", "")):
event_dict["message"] = f"Changelog: {m.group()}" event_dict["message"] = f"Changelog: {match.group()}"
Event.new(EventAction.UPDATE_AVAILABLE, **event_dict).save() Event.new(EventAction.UPDATE_AVAILABLE, **event_dict).save()
except (RequestException, IndexError) as exc: except (RequestException, IndexError) as exc:
cache.set(VERSION_CACHE_KEY, "0.0.0", VERSION_CACHE_TIMEOUT) cache.set(VERSION_CACHE_KEY, "0.0.0", VERSION_CACHE_TIMEOUT)

View File

@ -3,7 +3,42 @@
{% load i18n %} {% load i18n %}
{% block above_form %} {% block above_form %}
<h1>{% blocktrans with policy=policy %}Test policy {{ policy }}{% endblocktrans %}</h1> <h1>{% blocktrans with policy=policy %}Test {{ policy }}{% endblocktrans %}</h1>
{% endblock %}
{% block beneath_form %}
{% if result %}
<div class="pf-c-form__group ">
<div class="pf-c-form__group-label">
<label class="pf-c-form__label" for="context-1">
<span class="pf-c-form__label-text">{% trans 'Passing' %}</span>
</label>
</div>
<div class="pf-c-form__group-control">
<div class="c-form__horizontal-group">
<span class="pf-c-form__label-text">{{ result.passing|yesno:"Yes,No" }}</span>
</div>
</div>
</div>
<div class="pf-c-form__group ">
<div class="pf-c-form__group-label">
<label class="pf-c-form__label" for="context-1">
<span class="pf-c-form__label-text">{% trans 'Messages' %}</span>
</label>
</div>
<div class="pf-c-form__group-control">
<div class="c-form__horizontal-group">
<ul>
{% for m in result.messages %}
<li><span class="pf-c-form__label-text">{{ m }}</span></li>
{% empty %}
<li><span class="pf-c-form__label-text">-</span></li>
{% endfor %}
</ul>
</div>
</div>
</div>
{% endif %}
{% endblock %} {% endblock %}
{% block action %} {% block action %}

View File

@ -1,7 +1,6 @@
"""authentik Policy administration""" """authentik Policy administration"""
from typing import Any, Dict from typing import Any, Dict
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.mixins import ( from django.contrib.auth.mixins import (
PermissionRequiredMixin as DjangoPermissionRequiredMixin, PermissionRequiredMixin as DjangoPermissionRequiredMixin,
@ -121,8 +120,6 @@ class PolicyTestView(LoginRequiredMixin, DetailView, PermissionRequiredMixin, Fo
proc = PolicyProcess(PolicyBinding(policy=policy), p_request, None) proc = PolicyProcess(PolicyBinding(policy=policy), p_request, None)
result = proc.execute() result = proc.execute()
if result.passing: context = self.get_context_data(form=form)
messages.success(self.request, _("User successfully passed policy.")) context["result"] = result
else: return self.render_to_response(context)
messages.error(self.request, _("User didn't pass policy."))
return self.render_to_response(self.get_context_data(form=form, result=result))

View File

@ -1,29 +1,25 @@
"""authentik PropertyMapping administration""" """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 json import dumps
from typing import Any 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 LoginRequiredMixin
from django.contrib.auth.mixins import ( from django.contrib.auth.mixins import (
PermissionRequiredMixin as DjangoPermissionRequiredMixin, PermissionRequiredMixin as DjangoPermissionRequiredMixin,
) )
from django.contrib.messages.views import SuccessMessageMixin from django.contrib.messages.views import SuccessMessageMixin
from django.http import HttpResponse
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.utils.translation import gettext as _ 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 ( from authentik.admin.views.utils import (
BackSuccessUrlMixin, BackSuccessUrlMixin,
DeleteMessageView, DeleteMessageView,
InheritanceCreateView, InheritanceCreateView,
InheritanceListView,
InheritanceUpdateView, InheritanceUpdateView,
SearchListMixin,
UserPaginateListMixin,
) )
from authentik.core.models import PropertyMapping from authentik.core.models import PropertyMapping
@ -75,7 +71,9 @@ class PropertyMappingDeleteView(
success_message = _("Successfully deleted Property Mapping") success_message = _("Successfully deleted Property Mapping")
class PropertyMappingTestView(LoginRequiredMixin, DetailView, PermissionRequiredMixin, FormView): class PropertyMappingTestView(
LoginRequiredMixin, DetailView, PermissionRequiredMixin, FormView
):
"""View to test property mappings""" """View to test property mappings"""
model = PropertyMapping model = PropertyMapping
@ -86,7 +84,9 @@ class PropertyMappingTestView(LoginRequiredMixin, DetailView, PermissionRequired
def get_object(self, queryset=None) -> PropertyMapping: def get_object(self, queryset=None) -> PropertyMapping:
return ( 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]: 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) context = self.get_context_data(form=form)
try: 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) context["result"] = dumps(result, indent=4)
except Exception as exc: # pylint: disable=broad-except except Exception as exc: # pylint: disable=broad-except
context["result"] = str(exc) context["result"] = str(exc)