stages/user_write: add signals

This commit is contained in:
Jens Langhammer 2020-07-07 15:46:57 +02:00
parent 6bc6f947dd
commit 17a2ac73e7
5 changed files with 24 additions and 8 deletions

View file

@ -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()

View file

@ -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"])

View file

@ -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"

View file

@ -0,0 +1,4 @@
"""passbook user_write signals"""
from django.core.signals import Signal
user_write = Signal(providing_args=["request", "user", "data"])

View file

@ -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)