1
0
mirror of https://github.com/django/django.git synced 2025-10-24 22:26:08 +00:00

Fixed #31920 -- Made AuthenticationMiddleware add request.auser().

This commit is contained in:
Jon Janzen
2023-02-13 18:24:35 -05:00
committed by Mariusz Felisiak
parent e83a88566a
commit e846c5e724
5 changed files with 50 additions and 0 deletions

View File

@@ -1,3 +1,7 @@
from functools import partial
from asgiref.sync import sync_to_async
from django.contrib import auth
from django.contrib.auth import load_backend
from django.contrib.auth.backends import RemoteUserBackend
@@ -12,6 +16,12 @@ def get_user(request):
return request._cached_user
async def auser(request):
if not hasattr(request, "_acached_user"):
request._acached_user = await sync_to_async(auth.get_user)(request)
return request._acached_user
class AuthenticationMiddleware(MiddlewareMixin):
def process_request(self, request):
if not hasattr(request, "session"):
@@ -23,6 +33,7 @@ class AuthenticationMiddleware(MiddlewareMixin):
"'django.contrib.auth.middleware.AuthenticationMiddleware'."
)
request.user = SimpleLazyObject(lambda: get_user(request))
request.auser = partial(auser, request)
class RemoteUserMiddleware(MiddlewareMixin):