diff --git a/allauth/account/adapter.py b/allauth/account/adapter.py index d2533dd7be..4184212dd5 100644 --- a/allauth/account/adapter.py +++ b/allauth/account/adapter.py @@ -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 @@ -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 @@ -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 diff --git a/allauth/account/forms.py b/allauth/account/forms.py index 637f15a1ea..d1596fce8d 100644 --- a/allauth/account/forms.py +++ b/allauth/account/forms.py @@ -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 ( @@ -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)