|
43 | 43 | EmailResetForm,
|
44 | 44 | UserSearchForm,
|
45 | 45 | MergeUserForm,
|
46 |
| - AddSystemTagForm |
| 46 | + AddSystemTagForm, |
| 47 | + AddEmailForm |
47 | 48 | )
|
48 | 49 | from admin.base.views import GuidView
|
49 | 50 | from api.users.services import send_password_reset_email
|
@@ -399,6 +400,35 @@ def form_valid(self, form):
|
399 | 400 | return super().form_valid(form)
|
400 | 401 |
|
401 | 402 |
|
| 403 | +class UserAddEmail(UserMixin, FormView): |
| 404 | + """Allows authorized users to add an email to a user's account and trigger confirmation.""" |
| 405 | + permission_required = 'osf.change_osfuser' |
| 406 | + raise_exception = True |
| 407 | + form_class = AddEmailForm |
| 408 | + |
| 409 | + def form_valid(self, form): |
| 410 | + from osf.exceptions import BlockedEmailError |
| 411 | + from django.core.exceptions import ValidationError as DjangoValidationError |
| 412 | + from framework.auth.views import send_confirm_email_async |
| 413 | + from django.utils import timezone |
| 414 | + |
| 415 | + user = self.get_object() |
| 416 | + address = form.cleaned_data['new_email'].strip().lower() |
| 417 | + try: |
| 418 | + user.add_unconfirmed_email(address) |
| 419 | + |
| 420 | + send_confirm_email_async(user, email=address) |
| 421 | + user.email_last_sent = timezone.now() |
| 422 | + user.save() |
| 423 | + messages.success(self.request, f'Added unconfirmed email {address} and sent confirmation email.') |
| 424 | + except (DjangoValidationError, ValueError) as e: |
| 425 | + messages.error(self.request, f'Invalid email: {getattr(e, "message", str(e))}') |
| 426 | + except BlockedEmailError: |
| 427 | + messages.error(self.request, 'This email address domain is blocked.') |
| 428 | + |
| 429 | + return super().form_valid(form) |
| 430 | + |
| 431 | + |
402 | 432 | class UserMergeAccounts(UserMixin, FormView):
|
403 | 433 | """ Allows authorized users to merge a user's accounts using their guid.
|
404 | 434 | """
|
|
0 commit comments