|
4 | 4 |
|
5 | 5 | import logging |
6 | 6 |
|
| 7 | +from django.db import transaction |
7 | 8 | from django.http import Http404, HttpResponseBadRequest |
8 | 9 | from edx_rest_framework_extensions import permissions |
9 | 10 | from edx_rest_framework_extensions.auth.jwt.authentication import JwtAuthentication |
|
18 | 19 | from rest_framework import status |
19 | 20 | from rest_framework.response import Response |
20 | 21 | from rest_framework.views import APIView |
| 22 | +from social_django.models import UserSocialAuth |
21 | 23 |
|
22 | 24 | log = logging.getLogger(__name__) |
23 | 25 |
|
@@ -85,25 +87,34 @@ def post(self, request): |
85 | 87 |
|
86 | 88 | # A user that is created by LTI will always have the same username as |
87 | 89 | # lti_user_id in LtiUser table. |
88 | | - lti_user = LtiUser.objects.filter(edx_user__email=user_email).first() |
89 | | - if not lti_user: |
90 | | - log.error("No user was found against the given email (%s)", user_email) |
91 | | - raise Http404 |
92 | | - if lti_user.lti_user_id != lti_user.edx_user.username: |
93 | | - log.error( |
94 | | - "User with email (%s) does not appear to be an LTI-created user", |
95 | | - user_email, |
96 | | - ) |
97 | | - return HttpResponseBadRequest( |
98 | | - "User with the given email does not appear to be an LTI-created user." |
99 | | - ) |
100 | | - |
101 | | - user = lti_user.edx_user |
102 | | - user.email = user.email.split("@")[0] + "@" + PLACEHOLDER_EMAIL_DOMAIN |
103 | | - user.save() |
| 90 | + with transaction.atomic(): |
| 91 | + lti_user = LtiUser.objects.filter(edx_user__email=user_email).first() |
| 92 | + if not lti_user: |
| 93 | + log.error("No user was found against the given email (%s)", user_email) |
| 94 | + raise Http404 |
| 95 | + if lti_user.lti_user_id != lti_user.edx_user.username: |
| 96 | + log.error( |
| 97 | + "User with email (%s) does not appear to be an LTI-created user", |
| 98 | + user_email, |
| 99 | + ) |
| 100 | + return HttpResponseBadRequest( |
| 101 | + "User with the given email does not appear to be an " |
| 102 | + "LTI-created user." |
| 103 | + ) |
| 104 | + |
| 105 | + user = lti_user.edx_user |
| 106 | + user.email = user.email.split("@")[0] + "@" + PLACEHOLDER_EMAIL_DOMAIN |
| 107 | + user.save() |
| 108 | + # Remove social auth records for this user |
| 109 | + UserSocialAuth.objects.filter(user=user).delete() |
| 110 | + # Remove the old LTI mapping so that a new one gets created the next time |
| 111 | + # users access edX via LTI |
| 112 | + lti_user.delete() |
| 113 | + |
104 | 114 | # Send the user for retirement and deactivate the account |
105 | 115 | try: |
106 | 116 | create_retirement_request_and_deactivate_account(user) |
107 | 117 | except Exception as e: # noqa: BLE001 |
108 | 118 | log.error("Error retiring and deactivating user: %s", e) # noqa: TRY400 |
| 119 | + |
109 | 120 | return Response(status=status.HTTP_200_OK) |
0 commit comments