Created
August 21, 2023 13:15
-
-
Save brunomichetti/7a71062d654fff12618dfd7f55cdd60b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
from rest_framework.permissions import IsAuthenticated | |
... | |
# Add this to the UsersViewSet | |
def get_permissions(self): | |
if self.action == "destroy": | |
return [IsAuthenticated()] | |
return super().get_permissions() | |
# Add this to the UsersViewSet | |
def destroy(self, request, pk, *args, **kwargs): | |
""" | |
Deletes an authenticated user. Requires authentication. | |
Only a user can delete its own account. | |
""" | |
user = request.user | |
if user.id != int(pk): | |
raise ValidationError("Only a user can delete its own account.") | |
user.delete() | |
return Response(status=status.HTTP_204_NO_CONTENT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment