Skip to content

Commit 77367b6

Browse files
committed
Send a notification to the user on each succesful login.
1 parent f705a99 commit 77367b6

4 files changed

Lines changed: 55 additions & 1 deletion

File tree

hypha/apply/users/apps.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.apps import AppConfig
2+
3+
4+
class UsersConfig(AppConfig):
5+
name = "hypha.apply.users"
6+
7+
def ready(self):
8+
from . import signals # NOQA

hypha/apply/users/signals.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from django.conf import settings
2+
from django.contrib.auth.signals import user_logged_in
3+
from django.dispatch import receiver
4+
from django.utils import formats, timezone
5+
from django.utils.translation import gettext_lazy as _
6+
from wagtail.models import Site
7+
8+
from hypha.core.mail import MarkdownMail
9+
10+
11+
@receiver(user_logged_in)
12+
def send_login_notification(sender, request, user, **kwargs):
13+
if not settings.SEND_MESSAGES or not user.email:
14+
return
15+
16+
email = MarkdownMail("users/emails/login_notification.md")
17+
email.send(
18+
to=user.email,
19+
subject=_("Successful login to %(org)s") % {"org": settings.ORG_LONG_NAME},
20+
from_email=settings.DEFAULT_FROM_EMAIL,
21+
context={
22+
"user": user,
23+
"login_time": formats.date_format(timezone.now(), "SHORT_DATETIME_FORMAT"),
24+
"site": Site.find_for_request(request) if request else None,
25+
"ORG_EMAIL": settings.ORG_EMAIL,
26+
},
27+
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{% load i18n wagtailadmin_tags %}{% base_url_setting as base_url %}
2+
{% blocktrans %}Dear {{ user }},{% endblocktrans %}
3+
4+
{% blocktrans %}This is to notify you that your account was successfully logged in to {{ ORG_LONG_NAME }}.{% endblocktrans %}
5+
6+
{% blocktrans with login_time=login_time %}Login time: {{ login_time }}{% endblocktrans %}
7+
8+
{% blocktrans %}If you did not log in, please contact us immediately and consider changing your password.{% endblocktrans %}
9+
10+
{% if ORG_EMAIL %}
11+
{% blocktrans %}If you have any questions, please contact us at {{ ORG_EMAIL }}.{% endblocktrans %}
12+
{% endif %}
13+
14+
{% blocktrans %}Kind Regards,
15+
The {{ ORG_SHORT_NAME }} Team{% endblocktrans %}
16+
17+
--
18+
{{ ORG_LONG_NAME }}
19+
{% if site %}{{ site.root_url }}{% else %}{{ base_url }}{% endif %}

hypha/settings/django.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"hypha.apply.dashboard",
1717
"hypha.apply.flags",
1818
"hypha.home",
19-
"hypha.apply.users",
19+
"hypha.apply.users.apps.UsersConfig",
2020
"hypha.apply.review",
2121
"hypha.apply.determinations",
2222
"hypha.apply.stream_forms",

0 commit comments

Comments
 (0)