From 945d5bfaf65be586801101ec505527cd47ed2152 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 21 Sep 2020 20:30:30 +0200 Subject: [PATCH] *: use Audit custom event action, add SOURCE_LINKED event action --- .../migrations/0004_auto_20200921_1829.py | 37 +++++++++++++++++++ passbook/audit/models.py | 2 + passbook/audit/signals.py | 4 +- passbook/audit/tests/test_event.py | 2 +- passbook/sources/oauth/views/callback.py | 2 +- passbook/sources/oauth/views/flows.py | 4 +- passbook/stages/otp_static/views.py | 4 +- passbook/stages/otp_time/views.py | 8 ++-- 8 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 passbook/audit/migrations/0004_auto_20200921_1829.py diff --git a/passbook/audit/migrations/0004_auto_20200921_1829.py b/passbook/audit/migrations/0004_auto_20200921_1829.py new file mode 100644 index 000000000..ee5ec816e --- /dev/null +++ b/passbook/audit/migrations/0004_auto_20200921_1829.py @@ -0,0 +1,37 @@ +# Generated by Django 3.1.1 on 2020-09-21 18:29 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("passbook_audit", "0003_auto_20200917_1155"), + ] + + operations = [ + migrations.AlterField( + model_name="event", + name="action", + field=models.TextField( + choices=[ + ("login", "Login"), + ("login_failed", "Login Failed"), + ("logout", "Logout"), + ("sign_up", "Sign Up"), + ("authorize_application", "Authorize Application"), + ("suspicious_request", "Suspicious Request"), + ("password_set", "Password Set"), + ("invitation_created", "Invite Created"), + ("invitation_used", "Invite Used"), + ("source_linked", "Source Linked"), + ("impersonation_started", "Impersonation Started"), + ("impersonation_ended", "Impersonation Ended"), + ("model_created", "Model Created"), + ("model_updated", "Model Updated"), + ("model_deleted", "Model Deleted"), + ("custom_", "Custom Prefix"), + ] + ), + ), + ] diff --git a/passbook/audit/models.py b/passbook/audit/models.py index b964d5147..db3848ed8 100644 --- a/passbook/audit/models.py +++ b/passbook/audit/models.py @@ -104,6 +104,8 @@ class EventAction(models.TextChoices): INVITE_CREATED = "invitation_created" INVITE_USED = "invitation_used" + SOURCE_LINKED = "source_linked" + IMPERSONATION_STARTED = "impersonation_started" IMPERSONATION_ENDED = "impersonation_ended" diff --git a/passbook/audit/signals.py b/passbook/audit/signals.py index 0094df5dc..0e8da6a20 100644 --- a/passbook/audit/signals.py +++ b/passbook/audit/signals.py @@ -57,9 +57,7 @@ def on_user_logged_out(sender, request: HttpRequest, user: User, **_): # pylint: disable=unused-argument def on_user_write(sender, request: HttpRequest, user: User, data: Dict[str, Any], **_): """Log User write""" - thread = EventNewThread( - EventAction.CUSTOM, request, caller="stages/user_write", **data - ) + thread = EventNewThread("stages/user_write", request, **data) thread.user = user thread.run() diff --git a/passbook/audit/tests/test_event.py b/passbook/audit/tests/test_event.py index 30bed99e3..af2970254 100644 --- a/passbook/audit/tests/test_event.py +++ b/passbook/audit/tests/test_event.py @@ -4,7 +4,7 @@ from django.contrib.contenttypes.models import ContentType from django.test import TestCase from guardian.shortcuts import get_anonymous_user -from passbook.audit.models import Event, EventAction +from passbook.audit.models import Event from passbook.policies.dummy.models import DummyPolicy diff --git a/passbook/sources/oauth/views/callback.py b/passbook/sources/oauth/views/callback.py index 8d6058794..911812daa 100644 --- a/passbook/sources/oauth/views/callback.py +++ b/passbook/sources/oauth/views/callback.py @@ -182,7 +182,7 @@ class OAuthCallback(OAuthClientMixin, View): access.save() UserOAuthSourceConnection.objects.filter(pk=access.pk).update(user=user) Event.new( - EventAction.CUSTOM, message="Linked OAuth Source", source=source + EventAction.SOURCE_LINKED, message="Linked OAuth Source", source=source ).from_http(self.request) messages.success( self.request, diff --git a/passbook/sources/oauth/views/flows.py b/passbook/sources/oauth/views/flows.py index 1cc89eab1..e60391028 100644 --- a/passbook/sources/oauth/views/flows.py +++ b/passbook/sources/oauth/views/flows.py @@ -23,6 +23,8 @@ class PostUserEnrollmentStage(StageView): access.save() UserOAuthSourceConnection.objects.filter(pk=access.pk).update(user=user) Event.new( - EventAction.CUSTOM, message="Linked OAuth Source", source=access.source + EventAction.SOURCE_LINKED, + message="Linked OAuth Source", + source=access.source, ).from_http(self.request) return self.executor.stage_ok() diff --git a/passbook/stages/otp_static/views.py b/passbook/stages/otp_static/views.py index c3bf9a4db..f766d6158 100644 --- a/passbook/stages/otp_static/views.py +++ b/passbook/stages/otp_static/views.py @@ -7,7 +7,7 @@ from django.views import View from django.views.generic import TemplateView from django_otp.plugins.otp_static.models import StaticDevice, StaticToken -from passbook.audit.models import Event, EventAction +from passbook.audit.models import Event class UserSettingsView(LoginRequiredMixin, TemplateView): @@ -36,6 +36,6 @@ class DisableView(LoginRequiredMixin, View): messages.success(request, "Successfully disabled Static OTP Tokens") # Create event with email notification Event.new( - EventAction.CUSTOM, message="User disabled Static OTP Tokens." + "static_otp_disable", message="User disabled Static OTP Tokens." ).from_http(request) return redirect("passbook_stages_otp:otp-user-settings") diff --git a/passbook/stages/otp_time/views.py b/passbook/stages/otp_time/views.py index 63f5afdfc..41534e509 100644 --- a/passbook/stages/otp_time/views.py +++ b/passbook/stages/otp_time/views.py @@ -7,7 +7,7 @@ from django.views import View from django.views.generic import TemplateView from django_otp.plugins.otp_totp.models import TOTPDevice -from passbook.audit.models import Event, EventAction +from passbook.audit.models import Event class UserSettingsView(LoginRequiredMixin, TemplateView): @@ -32,7 +32,7 @@ class DisableView(LoginRequiredMixin, View): totp.delete() messages.success(request, "Successfully disabled Time-based OTP") # Create event with email notification - Event.new( - EventAction.CUSTOM, message="User disabled Time-based OTP." - ).from_http(request) + Event.new("totp_disable", message="User disabled Time-based OTP.").from_http( + request + ) return redirect("passbook_stages_otp:otp-user-settings")