Ticket #12148: auth_get_user_patch_v2.diff
File auth_get_user_patch_v2.diff, 4.2 KB (added by , 15 years ago) |
---|
-
django/contrib/auth/__init__.py
1 1 import datetime 2 2 from warnings import warn 3 from django.conf import settings 3 4 from django.core.exceptions import ImproperlyConfigured 4 5 from django.utils.importlib import import_module 5 6 … … 35 36 return cls() 36 37 37 38 def get_backends(): 38 from django.conf import settings39 39 backends = [] 40 40 for backend_path in settings.AUTHENTICATION_BACKENDS: 41 41 backends.append(load_backend(backend_path)) … … 96 96 try: 97 97 user_id = request.session[SESSION_KEY] 98 98 backend_path = request.session[BACKEND_SESSION_KEY] 99 backend = load_backend(backend_path) 100 user = backend.get_user(user_id) or AnonymousUser() 99 if backend_path in settings.AUTHENTICATION_BACKENDS: 100 backend = load_backend(backend_path) 101 user = backend.get_user(user_id) or AnonymousUser() 102 else: 103 user = AnonymousUser() 101 104 except KeyError: 102 105 user = AnonymousUser() 103 106 return user -
django/contrib/auth/tests/auth_backends.py
1 1 from django.conf import settings 2 from django.contrib import auth 2 3 from django.contrib.auth.models import User, Group, Permission, AnonymousUser 3 4 from django.contrib.contenttypes.models import ContentType 5 from django.http import HttpRequest 4 6 from django.test import TestCase 5 7 6 8 … … 245 247 246 248 def test_get_all_permissions(self): 247 249 self.assertEqual(self.user1.get_all_permissions(TestObj()), set()) 250 251 252 class StubBackend(object): 253 USER_1 = User() 254 def get_user(self, id): 255 if id == 1: return self.USER_1 256 return None 257 258 class BackendCachedTest(TestCase): 259 """ 260 Tests that User is loaded with the backend cached in the session. 261 """ 262 263 backend = 'django.contrib.auth.tests.auth_backends.StubBackend' 264 valid_uid = 1 265 invalid_uid = -1 266 267 def setUp(self): 268 self.curr_auth = settings.AUTHENTICATION_BACKENDS 269 settings.AUTHENTICATION_BACKENDS = (self.backend,) 270 self.request = HttpRequest() 271 self.request.session = {} 272 273 def tearDown(self): 274 settings.AUTHENTICATION_BACKENDS = self.curr_auth 275 276 def set_up_session(self, uid, backend): 277 self.request.session[auth.SESSION_KEY] = uid 278 self.request.session[auth.BACKEND_SESSION_KEY] = backend 279 280 def test_valid_user_valid_backend(self): 281 self.set_up_session(uid=self.valid_uid, backend=self.backend) 282 user = auth.get_user(self.request) 283 self.assert_(user is StubBackend.USER_1) 284 285 def test_invalid_user_valid_backend(self): 286 self.set_up_session(uid=self.invalid_uid, backend=self.backend) 287 user = auth.get_user(self.request) 288 self.assert_(isinstance(user,AnonymousUser)) 289 290 def test_valid_user_backend_not_in_settings(self): 291 self.set_up_session(uid=self.invalid_uid, backend=self.backend) 292 settings.AUTHENTICATION_BACKENDS = tuple() 293 user = auth.get_user(self.request) 294 self.assert_(isinstance(user,AnonymousUser)) 295 -
django/contrib/auth/tests/__init__.py
4 4 from django.contrib.auth.tests.forms import FORM_TESTS 5 5 from django.contrib.auth.tests.remote_user \ 6 6 import RemoteUserTest, RemoteUserNoCreateTest, RemoteUserCustomTest 7 from django.contrib.auth.tests.auth_backends import BackendTest, RowlevelBackendTest, AnonymousUserBackendTest, NoAnonymousUserBackendTest 7 from django.contrib.auth.tests.auth_backends import BackendTest, RowlevelBackendTest, AnonymousUserBackendTest, NoAnonymousUserBackendTest, BackendCachedTest 8 8 from django.contrib.auth.tests.tokens import TOKEN_GENERATOR_TESTS 9 9 10 10 # The password for the fixture data users is 'password'