Skip to content

Commit 78e968c

Browse files
Upgrade User Confirmation Registrations
1 parent 0471b76 commit 78e968c

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

api/users/views.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
OSFUser,
100100
Email,
101101
Tag,
102+
NotificationType
102103
)
103104
from osf.utils.tokens import TokenHandler
104105
from osf.utils.tokens.handlers import sanction_handler
@@ -822,7 +823,7 @@ def get(self, request, *args, **kwargs):
822823
raise ValidationError('Request must include email in query params.')
823824

824825
institutional = bool(request.query_params.get('institutional', None))
825-
mail_template = mails.FORGOT_PASSWORD if not institutional else mails.FORGOT_PASSWORD_INSTITUTION
826+
mail_template = 'forgot_password' if not institutional else 'forgot_password_institution'
826827

827828
status_message = language.RESET_PASSWORD_SUCCESS_STATUS_MESSAGE.format(email=email)
828829
kind = 'success'
@@ -842,12 +843,15 @@ def get(self, request, *args, **kwargs):
842843
user_obj.email_last_sent = timezone.now()
843844
user_obj.save()
844845
reset_link = f'{settings.RESET_PASSWORD_URL}{user_obj._id}/{user_obj.verification_key_v2['token']}/'
845-
mails.send_mail(
846-
to_addr=email,
847-
mail=mail_template,
848-
reset_link=reset_link,
849-
can_change_preferences=False,
850-
)
846+
847+
notification_type = NotificationType.objects.filter(name=mail_template)
848+
if not notification_type.exists():
849+
raise NotificationType.DoesNotExist(
850+
f'NotificationType with name {mail_template} does not exist.'
851+
)
852+
notification_type = notification_type.first()
853+
notification_type.emit(user=user_obj, event_context={'can_change_preferences': False, 'reset_link': reset_link})
854+
851855
return Response(status=status.HTTP_200_OK, data={'message': status_message, 'kind': kind, 'institutional': institutional})
852856

853857
@method_decorator(csrf_protect)
@@ -1059,13 +1063,13 @@ def _process_external_identity(self, user, external_identity, service_url):
10591063
if external_status == 'CREATE':
10601064
service_url += '&' + urlencode({'new': 'true'})
10611065
elif external_status == 'LINK':
1062-
mails.send_mail(
1063-
user=user,
1064-
to_addr=user.username,
1065-
mail=mails.EXTERNAL_LOGIN_LINK_SUCCESS,
1066-
external_id_provider=provider,
1067-
can_change_preferences=False,
1068-
)
1066+
notification_type = NotificationType.objects.filter(name='external_confirm_success')
1067+
if not notification_type.exists():
1068+
raise NotificationType.DoesNotExist(
1069+
'NotificationType with name external_confirm_success does not exist.'
1070+
)
1071+
notification_type = notification_type.first()
1072+
notification_type.emit(user=user, event_context={'can_change_preferences': False, 'external_id_provider': provider})
10691073

10701074
enqueue_task(update_affiliation_for_orcid_sso_users.s(user._id, provider_id))
10711075

@@ -1380,13 +1384,13 @@ def post(self, request, *args, **kwargs):
13801384
if external_status == 'CREATE':
13811385
service_url += '&{}'.format(urlencode({'new': 'true'}))
13821386
elif external_status == 'LINK':
1383-
mails.send_mail(
1384-
user=user,
1385-
to_addr=user.username,
1386-
mail=mails.EXTERNAL_LOGIN_LINK_SUCCESS,
1387-
external_id_provider=provider,
1388-
can_change_preferences=False,
1389-
)
1387+
notification_type = NotificationType.objects.filter(name='external_confirm_success')
1388+
if not notification_type.exists():
1389+
raise NotificationType.DoesNotExist(
1390+
'NotificationType with name external_confirm_success does not exist.'
1391+
)
1392+
notification_type = notification_type.first()
1393+
notification_type.emit(user=user, event_context={'can_change_preferences': False, 'external_id_provider': provider})
13901394

13911395
enqueue_task(update_affiliation_for_orcid_sso_users.s(user._id, provider_id))
13921396

framework/auth/views.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from osf.exceptions import ValidationValueError, BlockedEmailError
3434
from osf.models.provider import PreprintProvider
3535
from osf.models.tag import Tag
36+
from osf.models.notification import NotificationType
3637
from osf.utils.requests import check_select_for_update
3738
from website.util.metrics import CampaignClaimedTags, CampaignSourceTags
3839
from website.ember_osf_web.decorators import ember_flag_is_active
@@ -207,14 +208,14 @@ def redirect_unsupported_institution(auth):
207208
def forgot_password_post():
208209
"""Dispatches to ``_forgot_password_post`` passing non-institutional user mail template
209210
and reset action."""
210-
return _forgot_password_post(mail_template=mails.FORGOT_PASSWORD,
211+
return _forgot_password_post(mail_template='forgot_password',
211212
reset_route='reset_password_get')
212213

213214

214215
def forgot_password_institution_post():
215216
"""Dispatches to `_forgot_password_post` passing institutional user mail template, reset
216217
action, and setting the ``institutional`` flag."""
217-
return _forgot_password_post(mail_template=mails.FORGOT_PASSWORD_INSTITUTION,
218+
return _forgot_password_post(mail_template='forgot_password_institution',
218219
reset_route='reset_password_institution_get',
219220
institutional=True)
220221

@@ -272,12 +273,13 @@ def _forgot_password_post(mail_template, reset_route, institutional=False):
272273
token=user_obj.verification_key_v2['token']
273274
)
274275
)
275-
mails.send_mail(
276-
to_addr=email,
277-
mail=mail_template,
278-
reset_link=reset_link,
279-
can_change_preferences=False,
280-
)
276+
notification_type = NotificationType.objects.filter(name=mail_template)
277+
if not notification_type.exists():
278+
raise NotificationType.DoesNotExist(
279+
f'NotificationType with name {mail_template} does not exist.'
280+
)
281+
notification_type = notification_type.first()
282+
notification_type.emit(user=user_obj, event_context={'can_change_preferences': False, 'reset_link': reset_link})
281283

282284
# institutional forgot password page displays the message as main text, not as an alert
283285
if institutional:

0 commit comments

Comments
 (0)