Ticket #1488: auth_middleware_fix.3.diff

File auth_middleware_fix.3.diff, 1.8 KB (added by django@…, 19 years ago)

_actual_ patch, ignore first two files

  • django/contrib/auth/middleware.py

    === django/contrib/auth/middleware.py
    ==================================================================
     
    1 class UserWrapper(object):
     1class LazyUser(object):
    22    """
    3     Proxy to lazily load a user object.
     3    Descriptor to lazily load a user object.
    44    """
    5     def __init__(self, login):
    6         self._login = login
    7         self._cached_user = None
     5    def __init__(self, user_id):
     6        self.user_id = user_id
    87
    9     def _get_user(self):
     8    def __get__(self, request, obj_type=None):
    109        from django.contrib.auth.models import User
    11         if not self._cached_user:
    12             self._cached_user = User.objects.get(pk=self._login)
    13         return self._cached_user
     10        request.user = User.objects.get(pk=self.user_id)
     11        return request.user
    1412
    15     _user = property(_get_user)
    16 
    17     def __getattr__(self, name):
    18         return getattr(self._user, name)
    19 
    2013class AuthenticationMiddleware:
    2114    def process_request(self, request):
    2215        from django.contrib.auth.models import SESSION_KEY
    2316        assert hasattr(request, 'session'), "The Django authentication middleware requires session middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.sessions.middleware.SessionMiddleware'."
    2417        try:
    2518            user_id = request.session[SESSION_KEY]
    26             request.user = UserWrapper(user_id)
    2719        except KeyError:
    2820            from django.contrib.auth.models import AnonymousUser
    2921            request.user = AnonymousUser()
     22        else:
     23            class UserRequest(request.__class__):
     24                user = LazyUser(user_id)
     25            request.__class__ = UserRequest
    3026        return None
Back to Top