Skip to content

Commit

Permalink
feat(account): Move RequestPasswordReset.save impl into account adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
mecampbellsoup committed Aug 15, 2024
1 parent 4303cbe commit e1258a0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
26 changes: 24 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 All @@ -41,6 +41,9 @@

from . import app_settings

if TYPE_CHECKING:
from allauth.account.forms import ResetPasswordForm


class DefaultAccountAdapter(BaseAdapter):
"""The adapter class allows you to override various functionality of the
Expand Down Expand Up @@ -565,9 +568,28 @@ def is_safe_url(self, url):

return url_has_allowed_host_and_scheme(url, allowed_hosts=allowed_hosts)

def request_password_reset(
self, form: ResetPasswordForm, request: HttpRequest, **kwargs
) -> str:
"""
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.
"""
from allauth.account.internal import flows

email = form.cleaned_data["email"]
if not form.users:
flows.signup.send_unknown_account_mail(request, email)
else:
form._send_password_reset_mail(request, email, form.users, **kwargs)
return email

def get_reset_password_from_key_url(self, key):
"""
Method intented to be overriden in case the password reset email
Method intented to be overridden in case the password reset email
needs to be adjusted.
"""
from allauth.account.internal import flows
Expand Down
10 changes: 3 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 @@ -590,12 +590,8 @@ def clean_email(self):
return self.cleaned_data["email"]

def save(self, request, **kwargs):
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
adapter: DefaultAccountAdapter = get_adapter()
return adapter.request_password_reset(self, request, **kwargs)

def _send_password_reset_mail(self, request, email, users, **kwargs):
token_generator = kwargs.get("token_generator", default_token_generator)
Expand Down

0 comments on commit e1258a0

Please sign in to comment.