From cac94792fafef96de8144e9b39ca85d8da2a383a Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 12 Jan 2021 22:03:33 +0100 Subject: [PATCH] admin: add event transport forms --- authentik/admin/urls.py | 17 +++++ .../views/events_notifications_transports.py | 64 +++++++++++++++++++ authentik/events/forms.py | 32 ++++++++++ authentik/events/models.py | 6 ++ 4 files changed, 119 insertions(+) create mode 100644 authentik/admin/views/events_notifications_transports.py create mode 100644 authentik/events/forms.py diff --git a/authentik/admin/urls.py b/authentik/admin/urls.py index d0c66180b..728957e3c 100644 --- a/authentik/admin/urls.py +++ b/authentik/admin/urls.py @@ -4,6 +4,7 @@ from django.urls import path from authentik.admin.views import ( applications, certificate_key_pair, + events_notifications_transports, flows, groups, outposts, @@ -352,4 +353,20 @@ urlpatterns = [ tasks.TaskListView.as_view(), name="tasks", ), + # Event Notification Transpots + path( + "events/transports/create/", + events_notifications_transports.NotificationTransportCreateView.as_view(), + name="notification-transport-create", + ), + path( + "events/transports//update/", + events_notifications_transports.NotificationTransportUpdateView.as_view(), + name="notification-transport-update", + ), + path( + "events/transports//delete/", + events_notifications_transports.NotificationTransportDeleteView.as_view(), + name="notification-transport-delete", + ), ] diff --git a/authentik/admin/views/events_notifications_transports.py b/authentik/admin/views/events_notifications_transports.py new file mode 100644 index 000000000..ecc0a172f --- /dev/null +++ b/authentik/admin/views/events_notifications_transports.py @@ -0,0 +1,64 @@ +"""authentik NotificationTransport administration""" +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.urls import reverse_lazy +from django.utils.translation import gettext as _ +from django.views.generic import UpdateView +from guardian.mixins import PermissionRequiredMixin + +from authentik.admin.views.utils import BackSuccessUrlMixin, DeleteMessageView +from authentik.events.forms import NotificationTransportForm +from authentik.events.models import NotificationTransport +from authentik.lib.views import CreateAssignPermView + + +class NotificationTransportCreateView( + SuccessMessageMixin, + BackSuccessUrlMixin, + LoginRequiredMixin, + DjangoPermissionRequiredMixin, + CreateAssignPermView, +): + """Create new NotificationTransport""" + + model = NotificationTransport + form_class = NotificationTransportForm + permission_required = "authentik_events.add_notificationtransport" + + template_name = "generic/create.html" + success_url = reverse_lazy("authentik_core:shell") + success_message = _("Successfully created Notification Transport") + + +class NotificationTransportUpdateView( + SuccessMessageMixin, + BackSuccessUrlMixin, + LoginRequiredMixin, + PermissionRequiredMixin, + UpdateView, +): + """Update application""" + + model = NotificationTransport + form_class = NotificationTransportForm + permission_required = "authentik_events.change_notificationtransport" + + template_name = "generic/update.html" + success_url = reverse_lazy("authentik_core:shell") + success_message = _("Successfully updated Notification Transport") + + +class NotificationTransportDeleteView( + LoginRequiredMixin, PermissionRequiredMixin, DeleteMessageView +): + """Delete application""" + + model = NotificationTransport + permission_required = "authentik_events.delete_notificationtransport" + + template_name = "generic/delete.html" + success_url = reverse_lazy("authentik_core:shell") + success_message = _("Successfully deleted Notification Transport") diff --git a/authentik/events/forms.py b/authentik/events/forms.py new file mode 100644 index 000000000..8888b021d --- /dev/null +++ b/authentik/events/forms.py @@ -0,0 +1,32 @@ +"""authentik events NotificationTransport forms""" +from django import forms +from django.utils.translation import gettext_lazy as _ + +from authentik.events.models import NotificationTransport + + +class NotificationTransportForm(forms.ModelForm): + """NotificationTransport Form""" + + class Meta: + + model = NotificationTransport + fields = [ + "name", + "mode", + "webhook_url", + ] + widgets = { + "name": forms.TextInput(), + "webhook_url": forms.TextInput(), + } + labels = { + "webhook_url": _("Webhook URL"), + } + help_texts = { + "webhook_url": _( + ( + "Only required when the Generic or Slack Webhook is used." + ) + ), + } diff --git a/authentik/events/models.py b/authentik/events/models.py index 296df1700..3b1faebc4 100644 --- a/authentik/events/models.py +++ b/authentik/events/models.py @@ -276,6 +276,9 @@ class NotificationTransport(models.Model): except (SMTPException, ConnectionError) as exc: raise NotificationTransportError from exc + def __str__(self) -> str: + return f"Notification Transport {self.name}" + class Meta: verbose_name = _("Notification Transport") @@ -344,6 +347,9 @@ class NotificationTrigger(PolicyBindingModel): on_delete=models.SET_NULL, ) + def __str__(self) -> str: + return f"Notification Trigger {self.name}" + class Meta: verbose_name = _("Notification Trigger")