From 17a2ac73e73ba6b0f84d79eb454c16f0971a86d5 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 7 Jul 2020 15:46:57 +0200 Subject: [PATCH] stages/user_write: add signals --- passbook/audit/signals.py | 23 +++++++++++++++++------ passbook/core/signals.py | 1 - passbook/stages/invitation/stage.py | 2 +- passbook/stages/user_write/signals.py | 4 ++++ passbook/stages/user_write/stage.py | 2 ++ 5 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 passbook/stages/user_write/signals.py diff --git a/passbook/audit/signals.py b/passbook/audit/signals.py index 7dd97db0e..d0687739a 100644 --- a/passbook/audit/signals.py +++ b/passbook/audit/signals.py @@ -12,8 +12,10 @@ from django.http import HttpRequest from passbook.audit.models import Event, EventAction from passbook.core.models import User -from passbook.core.signals import user_signed_up +from passbook.stages.invitation.models import Invitation from passbook.stages.invitation.signals import invitation_created, invitation_used +from passbook.stages.user_write.signals import user_write + class EventNewThread(Thread): """Create Event in background thread""" @@ -21,7 +23,7 @@ class EventNewThread(Thread): action: EventAction request: HttpRequest kwargs: Dict[str, Any] - user: Optional[User] + user: Optional[User] = None def __init__(self, action: EventAction, request: HttpRequest, **kwargs): super().__init__() @@ -51,6 +53,15 @@ def on_user_logged_out(sender, request: HttpRequest, user: User, **_): thread.run() +@receiver(user_write) +# 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, **data) + thread.user = user + thread.run() + + @receiver(user_login_failed) # pylint: disable=unused-argument def on_user_login_failed( @@ -63,19 +74,19 @@ def on_user_login_failed( @receiver(invitation_created) # pylint: disable=unused-argument -def on_invitation_created(sender, request: HttpRequest, invitation, **_): +def on_invitation_created(sender, request: HttpRequest, invitation: Invitation, **_): """Log Invitation creation""" thread = EventNewThread( - EventAction.INVITE_CREATED, request, invitation_uuid=invitation.uuid.hex + EventAction.INVITE_CREATED, request, invitation_uuid=invitation.invite_uuid.hex ) thread.run() @receiver(invitation_used) # pylint: disable=unused-argument -def on_invitation_used(sender, request: HttpRequest, invitation, **_): +def on_invitation_used(sender, request: HttpRequest, invitation: Invitation, **_): """Log Invitation usage""" thread = EventNewThread( - EventAction.INVITE_USED, request, invitation_uuid=invitation.uuid.hex + EventAction.INVITE_USED, request, invitation_uuid=invitation.invite_uuid.hex ) thread.run() diff --git a/passbook/core/signals.py b/passbook/core/signals.py index 1126efd9a..ec0aea598 100644 --- a/passbook/core/signals.py +++ b/passbook/core/signals.py @@ -1,5 +1,4 @@ """passbook core signals""" from django.core.signals import Signal -user_signed_up = Signal(providing_args=["request", "user"]) password_changed = Signal(providing_args=["user", "password"]) diff --git a/passbook/stages/invitation/stage.py b/passbook/stages/invitation/stage.py index ec3f98b37..bd62f0a27 100644 --- a/passbook/stages/invitation/stage.py +++ b/passbook/stages/invitation/stage.py @@ -4,8 +4,8 @@ from django.shortcuts import get_object_or_404 from passbook.flows.stage import StageView from passbook.stages.invitation.models import Invitation, InvitationStage -from passbook.stages.prompt.stage import PLAN_CONTEXT_PROMPT from passbook.stages.invitation.signals import invitation_used +from passbook.stages.prompt.stage import PLAN_CONTEXT_PROMPT INVITATION_TOKEN_KEY = "token" INVITATION_IN_EFFECT = "invitation_in_effect" diff --git a/passbook/stages/user_write/signals.py b/passbook/stages/user_write/signals.py new file mode 100644 index 000000000..e70401ca5 --- /dev/null +++ b/passbook/stages/user_write/signals.py @@ -0,0 +1,4 @@ +"""passbook user_write signals""" +from django.core.signals import Signal + +user_write = Signal(providing_args=["request", "user", "data"]) diff --git a/passbook/stages/user_write/stage.py b/passbook/stages/user_write/stage.py index bf1973b8e..008eca12c 100644 --- a/passbook/stages/user_write/stage.py +++ b/passbook/stages/user_write/stage.py @@ -12,6 +12,7 @@ from passbook.flows.stage import StageView from passbook.lib.utils.reflection import class_to_path from passbook.stages.password.stage import PLAN_CONTEXT_AUTHENTICATION_BACKEND from passbook.stages.prompt.stage import PLAN_CONTEXT_PROMPT +from passbook.stages.user_write.signals import user_write LOGGER = get_logger() @@ -49,6 +50,7 @@ class UserWriteStageView(StageView): else: user.attributes[key] = value user.save() + user_write.send(sender=self, request=request, user=user, data=data) # Check if the password has been updated, and update the session auth hash if any(["password" in x for x in data.keys()]): update_session_auth_hash(self.request, user)