|
47 | 47 | from api.registrations.serializers import RegistrationSerializer |
48 | 48 | from api.resources import annotations as resource_annotations |
49 | 49 |
|
| 50 | +from api.users.services import send_password_reset_email |
50 | 51 | from api.users.permissions import ( |
51 | 52 | CurrentUser, ReadOnlyOrCurrentUser, |
52 | 53 | ReadOnlyOrCurrentUserRelationship, |
@@ -864,38 +865,30 @@ class ResetPassword(JSONAPIBaseView, generics.ListCreateAPIView): |
864 | 865 | throttle_classes = (NonCookieAuthThrottle, BurstRateThrottle, RootAnonThrottle, SendEmailThrottle) |
865 | 866 |
|
866 | 867 | def get(self, request, *args, **kwargs): |
| 868 | + institutional = bool(request.query_params.get('institutional', None)) |
867 | 869 | email = request.query_params.get('email', None) |
868 | 870 | if not email: |
869 | 871 | raise ValidationError('Request must include email in query params.') |
870 | 872 |
|
871 | | - institutional = bool(request.query_params.get('institutional', None)) |
872 | | - mail_template = mails.FORGOT_PASSWORD if not institutional else mails.FORGOT_PASSWORD_INSTITUTION |
873 | | - |
874 | | - status_message = language.RESET_PASSWORD_SUCCESS_STATUS_MESSAGE.format(email=email) |
875 | | - kind = 'success' |
876 | 873 | # check if the user exists |
877 | 874 | user_obj = get_user(email=email) |
878 | | - |
879 | | - if user_obj: |
| 875 | + if user_obj and user_obj.is_active: |
880 | 876 | # rate limit forgot_password_post |
881 | 877 | if not throttle_period_expired(user_obj.email_last_sent, settings.SEND_EMAIL_THROTTLE): |
882 | | - status_message = 'You have recently requested to change your password. Please wait a few minutes ' \ |
883 | | - 'before trying again.' |
884 | | - kind = 'error' |
885 | | - return Response({'message': status_message, 'kind': kind}, status=status.HTTP_429_TOO_MANY_REQUESTS) |
886 | | - elif user_obj.is_active: |
887 | | - # new random verification key (v2) |
888 | | - user_obj.verification_key_v2 = generate_verification_key(verification_type='password') |
889 | | - user_obj.email_last_sent = timezone.now() |
890 | | - user_obj.save() |
891 | | - reset_link = f'{settings.RESET_PASSWORD_URL}{user_obj._id}/{user_obj.verification_key_v2['token']}/' |
892 | | - mails.send_mail( |
893 | | - to_addr=email, |
894 | | - mail=mail_template, |
895 | | - reset_link=reset_link, |
896 | | - can_change_preferences=False, |
897 | | - ) |
898 | | - return Response(status=status.HTTP_200_OK, data={'message': status_message, 'kind': kind, 'institutional': institutional}) |
| 878 | + status_message = 'You have recently requested to change your password. ' \ |
| 879 | + 'Please wait a few minutes before trying again.' |
| 880 | + return Response({'message': status_message, 'kind': 'error'}, status=status.HTTP_429_TOO_MANY_REQUESTS) |
| 881 | + |
| 882 | + send_password_reset_email(user_obj, email, institutional=institutional) |
| 883 | + |
| 884 | + return Response( |
| 885 | + status=status.HTTP_200_OK, |
| 886 | + data={ |
| 887 | + 'message': language.RESET_PASSWORD_SUCCESS_STATUS_MESSAGE.format(email=email), |
| 888 | + 'kind': 'success', |
| 889 | + 'institutional': institutional, |
| 890 | + }, |
| 891 | + ) |
899 | 892 |
|
900 | 893 | def post(self, request, *args, **kwargs): |
901 | 894 | serializer = self.get_serializer(data=request.data) |
|
0 commit comments