I'm using a Django package named django-safedelete that allows to delete users without removing them from the database.
Basically, it adds a delete
attribute to the model, and the queries like User.objects.all()
won't return the deleted models.
You can still query all objects using a special manager. For example User.objects.all_with_deleted()
will return all users , including the deleted ones. User.objects.deleted_only()
will return the deleted ones.
This works as expected, except in one case.
I'm using Token Authentication for my users with Django Rest Framework 3.9, and in my DRF views, I'm using the built-in permission IsAuthenticated
.
Code of a basic CBV I'm using:
class MyView(APIView):
permission_classes = (IsAuthenticated,)
def get(self, request):
return Response(status=HTTP_200_OK)
Code of the DRF implementation of IsAuthenticated
permission:
class IsAuthenticated(BasePermission):
"""
Allows access only to authenticated users.
"""
def has_permission(self, request, view):
return bool(request.user and request.user.is_authenticated)
The problem
When a user is soft deleted, he's still able to authenticate using its token.
I'm expecting the user to have a 401 Unauthorized error when he's soft deleted. What's wrong?