From 95c1473dd28d51579b73bb9d14fb65bbe8d3f6f7 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 12 Jan 2021 23:28:17 +0100 Subject: [PATCH] events: assign default triggers to default admin group, create default transport --- authentik/events/forms.py | 2 +- .../0011_notification_trigger_default_v1.py | 25 +++++++++++++------ authentik/events/models.py | 2 ++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/authentik/events/forms.py b/authentik/events/forms.py index dd2a69a4a..3343b99d2 100644 --- a/authentik/events/forms.py +++ b/authentik/events/forms.py @@ -38,9 +38,9 @@ class NotificationTriggerForm(forms.ModelForm): model = NotificationTrigger fields = [ "name", + "group", "transports", "severity", - "group", ] widgets = { "name": forms.TextInput(), diff --git a/authentik/events/migrations/0011_notification_trigger_default_v1.py b/authentik/events/migrations/0011_notification_trigger_default_v1.py index cb3055057..5601c90a3 100644 --- a/authentik/events/migrations/0011_notification_trigger_default_v1.py +++ b/authentik/events/migrations/0011_notification_trigger_default_v1.py @@ -4,7 +4,7 @@ from django.apps.registry import Apps from django.db import migrations from django.db.backends.base.schema import BaseDatabaseSchemaEditor -from authentik.events.models import EventAction +from authentik.events.models import EventAction, TransportMode def notify_configuration_error(apps: Apps, schema_editor: BaseDatabaseSchemaEditor): @@ -18,14 +18,14 @@ def notify_configuration_error(apps: Apps, schema_editor: BaseDatabaseSchemaEdit admin_group = Group.objects.using(db_alias).filter( name="authentik Admins", is_superuser=True - ) + ).first() policy, _ = EventMatcherPolicy.objects.using(db_alias).update_or_create( name="default-match-configuration-error", defaults={"action": EventAction.CONFIGURATION_ERROR}, ) trigger, _ = NotificationTrigger.objects.using(db_alias).update_or_create( - name="default-notify-configuration-error", defaults={"trigger": admin_group} + name="default-notify-configuration-error", defaults={"group": admin_group} ) PolicyBinding.objects.using(db_alias).update_or_create( target=trigger, @@ -47,14 +47,14 @@ def notify_update(apps: Apps, schema_editor: BaseDatabaseSchemaEditor): admin_group = Group.objects.using(db_alias).filter( name="authentik Admins", is_superuser=True - ) + ).first() policy, _ = EventMatcherPolicy.objects.using(db_alias).update_or_create( name="default-match-update", defaults={"action": EventAction.UPDATE_AVAILABLE}, ) trigger, _ = NotificationTrigger.objects.using(db_alias).update_or_create( - name="default-notify-update", defaults={"trigger": admin_group} + name="default-notify-update", defaults={"group": admin_group} ) PolicyBinding.objects.using(db_alias).update_or_create( target=trigger, @@ -76,7 +76,7 @@ def notify_exception(apps: Apps, schema_editor: BaseDatabaseSchemaEditor): admin_group = Group.objects.using(db_alias).filter( name="authentik Admins", is_superuser=True - ) + ).first() policy_policy_exc, _ = EventMatcherPolicy.objects.using(db_alias).update_or_create( name="default-match-policy-exception", @@ -87,7 +87,7 @@ def notify_exception(apps: Apps, schema_editor: BaseDatabaseSchemaEditor): defaults={"action": EventAction.PROPERTY_MAPPING_EXCEPTION}, ) trigger, _ = NotificationTrigger.objects.using(db_alias).update_or_create( - name="default-notify-exception", defaults={"trigger": admin_group} + name="default-notify-exception", defaults={"group": admin_group} ) PolicyBinding.objects.using(db_alias).update_or_create( target=trigger, @@ -105,6 +105,16 @@ def notify_exception(apps: Apps, schema_editor: BaseDatabaseSchemaEditor): ) +def transport_email_global(apps: Apps, schema_editor: BaseDatabaseSchemaEditor): + db_alias = schema_editor.connection.alias + NotificationTransport = apps.get_model("authentik_events", "NotificationTransport") + + NotificationTransport.objects.using(db_alias).update_or_create( + name="default-email-transport", + defaults={"mode": TransportMode.EMAIL}, + ) + + class Migration(migrations.Migration): dependencies = [ @@ -121,4 +131,5 @@ class Migration(migrations.Migration): migrations.RunPython(notify_configuration_error), migrations.RunPython(notify_update), migrations.RunPython(notify_exception), + migrations.RunPython(transport_email_global), ] diff --git a/authentik/events/models.py b/authentik/events/models.py index 3b1faebc4..6c61f6013 100644 --- a/authentik/events/models.py +++ b/authentik/events/models.py @@ -203,6 +203,8 @@ class NotificationTransport(models.Model): json={ "body": notification.body, "severity": notification.severity, + "user_email": notification.user.email, + "user_username": notification.user.username, }, ) response.raise_for_status()