Skip to content

Commit 5112d00

Browse files
committed
add email check
1 parent b44a442 commit 5112d00

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

admin/users/views.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from osf.exceptions import UserStateError
2020
from osf.models.base import Guid
21-
from osf.models.user import OSFUser
21+
from osf.models.user import OSFUser, Email
2222
from osf.models.spam import SpamStatus
2323
from framework.auth import get_user
2424
from framework.auth.core import generate_verification_key
@@ -411,6 +411,19 @@ def form_valid(self, form):
411411

412412
user = self.get_object()
413413
address = form.cleaned_data['new_email'].strip().lower()
414+
415+
existing_email = Email.objects.filter(address=address).first()
416+
if existing_email:
417+
if existing_email.user == user:
418+
messages.error(self.request, f'Email {address} is already confirmed for this user.')
419+
else:
420+
messages.error(self.request, f'Email {address} already exists in the system and is associated with another user.')
421+
return super().form_valid(form)
422+
423+
if address in user.unconfirmed_emails:
424+
messages.error(self.request, f'Email {address} is already pending confirmation for this user.')
425+
return super().form_valid(form)
426+
414427
try:
415428
user.add_unconfirmed_email(address)
416429

0 commit comments

Comments
 (0)