With this implementation, the entire Userlist is visible for anyone in the API endpoint. If I change the permission classes to IsAdmin for instance, users does not have access to their own user. What would be a secure way to only return the current user? Is it possible to filter out just that user within the get_queryset() function?
class UserList(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView):
serializer_class = UserSerializer
permission_classes = [IsAuthenticatedOrReadOnly]
def get(self, request, *args, **kwargs):
self.serializer_class = UserGetSerializer
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
def get_queryset(self):
qs = get_user_model().objects.all()
if self.request.user:
# Return the currently logged in user
status = self.request.query_params.get("user", None)
if status and status == "current":
qs = get_user_model().objects.filter(pk=self.request.user.pk)
return qs