Skip to content

Commit 7d33e39

Browse files
committed
Do full email validation in oauth signup form
These fields aren't editable anyway, but if we don't do the full validation we will instead crash if for example the same account creation form is submitted twice (happens surprisingly often). Now we will instead show a validation error message.
1 parent 0c8f2d7 commit 7d33e39

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

pgweb/account/forms.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ def _clean_username(username):
2828
raise forms.ValidationError("This username is already in use")
2929

3030

31+
def _clean_email(email):
32+
email = email.lower()
33+
34+
if User.objects.filter(email=email).exists():
35+
raise forms.ValidationError("A user with this email address is already registered")
36+
37+
if SecondaryEmail.objects.filter(email=email).exists():
38+
raise forms.ValidationError("This email address is already attached to a different user")
39+
40+
return email
41+
42+
3143
# Override some error handling only in the default authentication form
3244
class PgwebAuthenticationForm(AuthenticationForm):
3345
def clean(self):
@@ -91,15 +103,7 @@ def clean_username(self):
91103
return _clean_username(self.cleaned_data['username'])
92104

93105
def clean_email(self):
94-
email = self.cleaned_data['email'].lower()
95-
96-
if User.objects.filter(email=email).exists():
97-
raise forms.ValidationError("A user with this email address is already registered")
98-
99-
if SecondaryEmail.objects.filter(email=email).exists():
100-
raise forms.ValidationError("This email address is already attached to a different user")
101-
102-
return email
106+
return _clean_email(self.cleaned_data['email'])
103107

104108

105109
class SignupOauthForm(forms.Form):
@@ -122,7 +126,7 @@ def clean_username(self):
122126
return _clean_username(self.cleaned_data['username'])
123127

124128
def clean_email(self):
125-
return self.cleaned_data['email'].lower()
129+
return _clean_email(self.cleaned_data['email'])
126130

127131

128132
class UserProfileForm(forms.ModelForm):

0 commit comments

Comments
 (0)