Skip to content

Commit

Permalink
feat(account): Add DefaultAccountAdapter.request_password_reset
Browse files Browse the repository at this point in the history
This new adapter method can be used to apply custom logic for filtering
the list of users that are to receive password reset request emails.
  • Loading branch information
mecampbellsoup committed Aug 16, 2024
1 parent cfd1455 commit 372e22a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
15 changes: 13 additions & 2 deletions allauth/account/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from django.contrib.sites.shortcuts import get_current_site
from django.core.exceptions import FieldDoesNotExist
from django.core.mail import EmailMessage, EmailMultiAlternatives
from django.http import HttpResponse, HttpResponseRedirect
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.shortcuts import resolve_url
from django.template import TemplateDoesNotExist
from django.template.loader import render_to_string
Expand Down Expand Up @@ -563,9 +563,20 @@ def is_safe_url(self, url):

return url_has_allowed_host_and_scheme(url, allowed_hosts=allowed_hosts)

def request_password_reset(self, users):
"""
Method intended to be overridden in case you need to customize the logic
used to determine whether a user is permitted to request a password reset.
For example, if you are enforcing something like "social only" authentication
in your app, you may want to intervene here. The returned list of users will
be sent password reset emails.
"""
return users

def get_reset_password_from_key_url(self, key):
"""
Method intented to be overriden in case the password reset email
Method intended to be overridden in case the password reset email
needs to be adjusted.
"""
from allauth.account.internal import flows
Expand Down
12 changes: 5 additions & 7 deletions allauth/account/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from allauth.utils import get_username_max_length, set_form_field_order

from . import app_settings
from .adapter import get_adapter
from .adapter import DefaultAccountAdapter, get_adapter
from .app_settings import AuthenticationMethod
from .models import EmailAddress, Login
from .utils import (
Expand Down Expand Up @@ -598,17 +598,14 @@ def clean_email(self):
raise get_adapter().validation_error("unknown_email")
return self.cleaned_data["email"]

def save(self, request, **kwargs):
def save(self, request, **kwargs) -> str:
email = self.cleaned_data["email"]
if not self.users:
flows.signup.send_unknown_account_mail(request, email)
else:
self._send_password_reset_mail(request, email, self.users, **kwargs)
return email

def _send_password_reset_mail(self, request, email, users, **kwargs):
adapter: DefaultAccountAdapter = get_adapter()
token_generator = kwargs.get("token_generator", default_token_generator)

users = adapter.request_password_reset(self.users)
for user in users:
temp_key = token_generator.make_token(user)

Expand All @@ -630,6 +627,7 @@ def _send_password_reset_mail(self, request, email, users, **kwargs):
if app_settings.AUTHENTICATION_METHOD != AuthenticationMethod.EMAIL:
context["username"] = user_username(user)
get_adapter().send_mail("account/email/password_reset_key", email, context)
return email


class ResetPasswordKeyForm(PasswordVerificationMixin, forms.Form):
Expand Down

0 comments on commit 372e22a

Please sign in to comment.