From 81ac53ff0a4f589b4953f192939a486b62c5a320 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 29 Mar 2021 19:21:48 +0200 Subject: [PATCH] web/admin: migrate events notification rules to web Signed-off-by: Jens Langhammer --- authentik/admin/urls.py | 12 --- .../admin/views/events_notifications_rules.py | 47 -------- authentik/events/forms.py | 21 ---- web/src/interfaces/AdminInterface.ts | 18 ++-- web/src/pages/events/RuleForm.ts | 100 ++++++++++++++++++ web/src/pages/events/RuleListPage.ts | 37 +++++-- 6 files changed, 135 insertions(+), 100 deletions(-) delete mode 100644 authentik/admin/views/events_notifications_rules.py delete mode 100644 authentik/events/forms.py create mode 100644 web/src/pages/events/RuleForm.ts diff --git a/authentik/admin/urls.py b/authentik/admin/urls.py index 2cd19b829..fa2233539 100644 --- a/authentik/admin/urls.py +++ b/authentik/admin/urls.py @@ -3,7 +3,6 @@ from django.urls import path from authentik.admin.views import ( applications, - events_notifications_rules, flows, outposts, outposts_service_connections, @@ -171,15 +170,4 @@ urlpatterns = [ outposts_service_connections.OutpostServiceConnectionUpdateView.as_view(), name="outpost-service-connection-update", ), - # Event Notification Rules - path( - "events/rules/create/", - events_notifications_rules.NotificationRuleCreateView.as_view(), - name="notification-rule-create", - ), - path( - "events/rules//update/", - events_notifications_rules.NotificationRuleUpdateView.as_view(), - name="notification-rule-update", - ), ] diff --git a/authentik/admin/views/events_notifications_rules.py b/authentik/admin/views/events_notifications_rules.py deleted file mode 100644 index 48c69fb44..000000000 --- a/authentik/admin/views/events_notifications_rules.py +++ /dev/null @@ -1,47 +0,0 @@ -"""authentik NotificationRule 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.utils.translation import gettext as _ -from django.views.generic import UpdateView -from guardian.mixins import PermissionRequiredMixin - -from authentik.events.forms import NotificationRuleForm -from authentik.events.models import NotificationRule -from authentik.lib.views import CreateAssignPermView - - -class NotificationRuleCreateView( - SuccessMessageMixin, - LoginRequiredMixin, - DjangoPermissionRequiredMixin, - CreateAssignPermView, -): - """Create new NotificationRule""" - - model = NotificationRule - form_class = NotificationRuleForm - permission_required = "authentik_events.add_NotificationRule" - - success_url = "/" - template_name = "generic/create.html" - success_message = _("Successfully created Notification Rule") - - -class NotificationRuleUpdateView( - SuccessMessageMixin, - LoginRequiredMixin, - PermissionRequiredMixin, - UpdateView, -): - """Update application""" - - model = NotificationRule - form_class = NotificationRuleForm - permission_required = "authentik_events.change_NotificationRule" - - success_url = "/" - template_name = "generic/update.html" - success_message = _("Successfully updated Notification Rule") diff --git a/authentik/events/forms.py b/authentik/events/forms.py deleted file mode 100644 index c65740650..000000000 --- a/authentik/events/forms.py +++ /dev/null @@ -1,21 +0,0 @@ -"""authentik events NotificationTransport forms""" -from django import forms - -from authentik.events.models import NotificationRule - - -class NotificationRuleForm(forms.ModelForm): - """NotificationRule Form""" - - class Meta: - - model = NotificationRule - fields = [ - "name", - "group", - "transports", - "severity", - ] - widgets = { - "name": forms.TextInput(), - } diff --git a/web/src/interfaces/AdminInterface.ts b/web/src/interfaces/AdminInterface.ts index b710f7320..16ebea9e5 100644 --- a/web/src/interfaces/AdminInterface.ts +++ b/web/src/interfaces/AdminInterface.ts @@ -15,15 +15,6 @@ export const SIDEBAR_ITEMS: SidebarItem[] = [ ).when((): Promise => { return me().then(u => u.user.isSuperuser||false); }), - new SidebarItem("Events").children( - new SidebarItem("Logs", "/events/log").activeWhen( - `^/events/log/(?${UUID_REGEX})$` - ), - new SidebarItem("Notification Rules", "/events/rules"), - new SidebarItem("Notification Transports", "/events/transports"), - ).when((): Promise => { - return me().then(u => u.user.isSuperuser||false); - }), new SidebarItem("Resources").children( new SidebarItem("Applications", "/core/applications").activeWhen( `^/core/applications/(?${SLUG_REGEX})$` @@ -39,6 +30,15 @@ export const SIDEBAR_ITEMS: SidebarItem[] = [ ).when((): Promise => { return me().then(u => u.user.isSuperuser||false); }), + new SidebarItem("Events").children( + new SidebarItem("Logs", "/events/log").activeWhen( + `^/events/log/(?${UUID_REGEX})$` + ), + new SidebarItem("Notification Rules", "/events/rules"), + new SidebarItem("Notification Transports", "/events/transports"), + ).when((): Promise => { + return me().then(u => u.user.isSuperuser || false); + }), new SidebarItem("Customisation").children( new SidebarItem("Policies", "/policy/policies"), new SidebarItem("Property Mappings", "/core/property-mappings"), diff --git a/web/src/pages/events/RuleForm.ts b/web/src/pages/events/RuleForm.ts new file mode 100644 index 000000000..28f43d12c --- /dev/null +++ b/web/src/pages/events/RuleForm.ts @@ -0,0 +1,100 @@ +import { CoreApi, EventsApi, NotificationRule, NotificationRuleSeverityEnum } from "authentik-api"; +import { gettext } from "django"; +import { customElement, property } from "lit-element"; +import { html, TemplateResult } from "lit-html"; +import { DEFAULT_CONFIG } from "../../api/Config"; +import { Form } from "../../elements/forms/Form"; +import { ifDefined } from "lit-html/directives/if-defined"; +import "../../elements/forms/HorizontalFormElement"; +import { until } from "lit-html/directives/until"; + +@customElement("ak-event-rule-form") +export class RuleForm extends Form { + + @property({attribute: false}) + rule?: NotificationRule; + + getSuccessMessage(): string { + if (this.rule) { + return gettext("Successfully updated rule."); + } else { + return gettext("Successfully created rule."); + } + } + + send = (data: NotificationRule): Promise => { + if (this.rule) { + return new EventsApi(DEFAULT_CONFIG).eventsRulesUpdate({ + pbmUuid: this.rule.pk || "", + data: data + }); + } else { + return new EventsApi(DEFAULT_CONFIG).eventsRulesCreate({ + data: data + }); + } + }; + + renderSeverity(): TemplateResult { + return html` + + + + `; + } + + renderForm(): TemplateResult { + return html`
+ + + + + + + + +

${gettext("Select which transports should be used to notify the user. If none are selected, the notification will only be shown in the authentik UI.")}

+

${gettext("Hold control/command to select multiple items.")}

+
+ + + +
`; + } + +} diff --git a/web/src/pages/events/RuleListPage.ts b/web/src/pages/events/RuleListPage.ts index 427d70e46..e3f00d248 100644 --- a/web/src/pages/events/RuleListPage.ts +++ b/web/src/pages/events/RuleListPage.ts @@ -4,14 +4,15 @@ import { AKResponse } from "../../api/Client"; import { TablePage } from "../../elements/table/TablePage"; import "../../elements/policies/BoundPoliciesList"; -import "../../elements/buttons/ModalButton"; import "../../elements/buttons/SpinnerButton"; +import "../../elements/forms/ModalForm"; import { TableColumn } from "../../elements/table/Table"; import { PAGE_SIZE } from "../../constants"; import { EventsApi, NotificationRule } from "authentik-api"; import { DEFAULT_CONFIG } from "../../api/Config"; import { AdminURLManager } from "../../api/legacy"; import "../../elements/forms/DeleteForm"; +import "./RuleForm"; @customElement("ak-event-rule-list") export class RuleListPage extends TablePage { @@ -57,12 +58,19 @@ export class RuleListPage extends TablePage { html`${item.severity}`, html`${item.group?.name || gettext("None (rule disabled)")}`, html` - - + + + ${gettext("Update")} + + + ${gettext("Update Notification Rule")} + + + + + { renderToolbar(): TemplateResult { return html` - - + + ${gettext("Create")} - -
-
+ + + ${gettext("Create Notification Rule")} + + + + + ${super.renderToolbar()} `; }