2018-12-10 12:47:51 +00:00
|
|
|
"""passbook audit signal listener"""
|
2020-02-21 20:27:25 +00:00
|
|
|
from typing import Dict
|
|
|
|
|
|
|
|
from django.contrib.auth.signals import (
|
|
|
|
user_logged_in,
|
|
|
|
user_logged_out,
|
|
|
|
user_login_failed,
|
|
|
|
)
|
2018-12-10 12:47:51 +00:00
|
|
|
from django.dispatch import receiver
|
2020-02-21 20:27:25 +00:00
|
|
|
from django.http import HttpRequest
|
2018-12-10 12:47:51 +00:00
|
|
|
|
2019-12-05 15:14:08 +00:00
|
|
|
from passbook.audit.models import Event, EventAction
|
2020-02-21 20:27:25 +00:00
|
|
|
from passbook.core.models import User
|
2019-12-31 11:51:16 +00:00
|
|
|
from passbook.core.signals import invitation_created, invitation_used, user_signed_up
|
2018-12-10 12:47:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(user_logged_in)
|
2019-12-31 11:45:29 +00:00
|
|
|
# pylint: disable=unused-argument
|
2020-02-21 20:27:25 +00:00
|
|
|
def on_user_logged_in(sender, request: HttpRequest, user: User, **_):
|
2018-12-10 12:47:51 +00:00
|
|
|
"""Log successful login"""
|
2019-12-05 15:14:08 +00:00
|
|
|
Event.new(EventAction.LOGIN).from_http(request)
|
2018-12-10 12:47:51 +00:00
|
|
|
|
2019-12-31 11:45:29 +00:00
|
|
|
|
2018-12-10 12:47:51 +00:00
|
|
|
@receiver(user_logged_out)
|
2019-12-31 11:45:29 +00:00
|
|
|
# pylint: disable=unused-argument
|
2020-02-21 20:27:25 +00:00
|
|
|
def on_user_logged_out(sender, request: HttpRequest, user: User, **_):
|
2018-12-10 12:47:51 +00:00
|
|
|
"""Log successfully logout"""
|
2019-12-05 15:14:08 +00:00
|
|
|
Event.new(EventAction.LOGOUT).from_http(request)
|
2018-12-10 12:47:51 +00:00
|
|
|
|
2019-12-31 11:45:29 +00:00
|
|
|
|
2020-02-21 20:27:25 +00:00
|
|
|
@receiver(user_login_failed)
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def on_user_login_failed(
|
|
|
|
sender, credentials: Dict[str, str], request: HttpRequest, **_
|
|
|
|
):
|
|
|
|
"""Failed Login"""
|
|
|
|
credentials.pop("password")
|
|
|
|
Event.new(EventAction.LOGIN_FAILED, **credentials).from_http(request)
|
|
|
|
|
|
|
|
|
2018-12-10 13:05:27 +00:00
|
|
|
@receiver(user_signed_up)
|
2019-12-31 11:45:29 +00:00
|
|
|
# pylint: disable=unused-argument
|
2020-02-21 20:27:25 +00:00
|
|
|
def on_user_signed_up(sender, request: HttpRequest, user: User, **_):
|
2018-12-10 13:05:27 +00:00
|
|
|
"""Log successfully signed up"""
|
2019-12-05 15:14:08 +00:00
|
|
|
Event.new(EventAction.SIGN_UP).from_http(request)
|
2018-12-10 13:05:27 +00:00
|
|
|
|
2019-12-31 11:45:29 +00:00
|
|
|
|
2018-12-10 13:21:42 +00:00
|
|
|
@receiver(invitation_created)
|
2019-12-31 11:45:29 +00:00
|
|
|
# pylint: disable=unused-argument
|
2020-02-21 20:27:25 +00:00
|
|
|
def on_invitation_created(sender, request: HttpRequest, invitation, **_):
|
2018-12-10 13:49:15 +00:00
|
|
|
"""Log Invitation creation"""
|
2019-12-31 11:51:16 +00:00
|
|
|
Event.new(
|
|
|
|
EventAction.INVITE_CREATED, invitation_uuid=invitation.uuid.hex
|
|
|
|
).from_http(request)
|
2018-12-10 13:05:27 +00:00
|
|
|
|
2019-12-31 11:45:29 +00:00
|
|
|
|
2018-12-10 13:21:42 +00:00
|
|
|
@receiver(invitation_used)
|
2019-12-31 11:45:29 +00:00
|
|
|
# pylint: disable=unused-argument
|
2020-02-21 20:27:25 +00:00
|
|
|
def on_invitation_used(sender, request: HttpRequest, invitation, **_):
|
2018-12-10 13:49:15 +00:00
|
|
|
"""Log Invitation usage"""
|
2019-12-31 11:51:16 +00:00
|
|
|
Event.new(EventAction.INVITE_USED, invitation_uuid=invitation.uuid.hex).from_http(
|
|
|
|
request
|
|
|
|
)
|