Skip to content

Commit

Permalink
feat(accounts): Add notifications for account events
Browse files Browse the repository at this point in the history
  • Loading branch information
varun kumar committed Sep 30, 2023
1 parent 701ef63 commit 3440e5c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions allauth/account/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,9 @@ def get_client_ip(self, request):
ip = request.META.get("REMOTE_ADDR")
return ip

def get_browser_user_agent(self, request):
return request.META["HTTP_USER_AGENT"]

def generate_emailconfirmation_key(self, email):
key = get_random_string(64).lower()
return key
Expand All @@ -676,6 +679,9 @@ def get_login_stages(self):
ret.append("allauth.mfa.stages.AuthenticateStage")
return ret

def send_notification_mail(self, template_prefix, user, context):
self.send_mail(template_prefix, user.email, context)


def get_adapter(request=None):
return import_attribute(app_settings.ADAPTER)(request)
4 changes: 4 additions & 0 deletions allauth/account/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ def ready(self):
raise ImproperlyConfigured(
f"{required_mw} must be added to settings.MIDDLEWARE"
)
from .receivers import send_password_change_notification
from .signals import password_changed

password_changed.connect(send_password_change_notification)
18 changes: 18 additions & 0 deletions allauth/account/receivers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from datetime import datetime

from django.dispatch import receiver

from .adapter import get_adapter
from .signals import *


def send_password_change_notification(sender, request, user, **kwargs):
adapter = get_adapter()
template_prefix = "account/email/password_changed_notification"
context = {
"timestamp": datetime.now(),
"username": user.username,
"ip": adapter.get_client_ip(request),
"browser_agent": adapter.get_browser_user_agent(request),
}
adapter.send_notification_mail(template_prefix, user, context)
19 changes: 19 additions & 0 deletions allauth/templates/account/email/base_notification.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% load i18n %}

Hello {{ username }},

We wanted to let you know that there has been a recent change to your account information.

Here are the details of the change:

{% block content %}
{% endblock %}

Change details
IP address : {{ip}}
Browser agent : {{browser_agent}}


If this change was not made by you, please contact us immediately at support of {{site_name}}.


Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends "account/email/base_notification.txt" %}
{% load i18n %}

{% block content %}{% blocktrans %}Your Password has been changed

Changes made by {{ username }}.{% endblocktrans %}{% endblock content %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% load i18n %}
{% autoescape off %}
{% blocktrans %}Your Password has been changed{% endblocktrans %}
{% endautoescape %}

0 comments on commit 3440e5c

Please sign in to comment.