From 1853ca96bbfec6eba331a7c555961a5faa06f787 Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Fri, 8 Oct 2021 13:04:06 +0200 Subject: [PATCH] Check trusted email domains --- accounts/forms.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/accounts/forms.py b/accounts/forms.py index c5efdcd..d11c888 100644 --- a/accounts/forms.py +++ b/accounts/forms.py @@ -9,9 +9,23 @@ class RegistrationForm(UserCreationForm): label=_("Email address"), widget=forms.TextInput(), required=True, - help_text=_("Please enter a valid email address ending with `@crans.org` or `@ens-paris-saclay.fr`."), + help_text=_( + "Please enter a valid email address ending with `@crans.org` or " + "`@ens-paris-saclay.fr`." + ), ) + def clean_email(self): + """ + Check that the email address ends with a trusted domain. + """ + email = self.cleaned_data.get("email") + if not email.endswith("@crans.org") and not email.endswith("@ens-paris-saclay.fr"): + raise forms.ValidationError( + _("Must end with `@crans.org` or `@ens-paris-saclay.fr`.") + ) + return email + class Meta: model = User fields = ["username", "password1", "password2", "email"]