Ticket #2550: ticket2550b.diff

File ticket2550b.diff, 4.4 KB (added by dan.julius@…, 4 years ago)

Fixed value of supports_anonymous_user and supports_object_permissions

  • docs/topics/auth.txt

     
    14491449    you need to force users to re-authenticate using different methods. A simple
    14501450    way to do that is simply to execute ``Session.objects.all().delete()``.
    14511451
     1452.. versionadded:: 1.4
     1453
     1454If a backend raises a :class:`~django.core.exceptions.PermissionDenied`
     1455exception, authentication will immediately fail. Django won't check the backends that follow.
     1456
     1457
    14521458Writing an authentication backend
    14531459---------------------------------
    14541460
  • django/contrib/auth/__init__.py

     
    11import datetime
    22from warnings import warn
    3 from django.core.exceptions import ImproperlyConfigured
     3from django.core.exceptions import ImproperlyConfigured, PermissionDenied
    44from django.utils.importlib import import_module
    55from django.contrib.auth.signals import user_logged_in, user_logged_out
    66
     
    4747        except TypeError:
    4848            # This backend doesn't accept these credentials as arguments. Try the next one.
    4949            continue
     50        except PermissionDenied:
     51            # This backend says to stop in our tracks - this user should not be allowed in at all.
     52            return None
    5053        if user is None:
    5154            continue
    5255        # Annotate the user object with the path of the backend.
  • django/contrib/auth/tests/auth_backends.py

     
    33from django.conf import settings
    44from django.contrib.auth.models import User, Group, Permission, AnonymousUser
    55from django.contrib.contenttypes.models import ContentType
    6 from django.core.exceptions import ImproperlyConfigured
     6from django.core.exceptions import ImproperlyConfigured, PermissionDenied
     7from django.contrib.auth import authenticate
    78from django.test import TestCase
    89
    910
     
    350351        self.assertEqual(self.user1.has_module_perms("app1"), False)
    351352        self.assertEqual(self.user1.has_module_perms("app2"), False)
    352353
     354class PermissionDeniedBackend(object):
     355    """
     356    always raises PermissionDenied
     357    """
     358    supports_object_permissions = True
     359    supports_anonymous_user = True
     360    supports_inactive_user = True
     361
     362    def authenticate(self, username=None, password=None):
     363        raise PermissionDenied
     364
     365
     366class PermissionDeniedBackendTest(TestCase):
     367    """
     368        Tests that other backends are not checked once a backend raises PermissionDenied
     369    """
     370    backend = 'django.contrib.auth.tests.auth_backends.PermissionDeniedBackend'
     371
     372    def setUp(self):
     373        self.curr_auth = settings.AUTHENTICATION_BACKENDS
     374        self.user1 = User.objects.create_user('test', 'test@example.com', 'test')
     375        self.user1.save()
     376
     377    def tearDown(self):
     378        settings.AUTHENTICATION_BACKENDS = self.curr_auth
     379
     380    def test_permission_denied(self):
     381        "user is not authenticated after a backend raises permission denied #2550"
     382        settings.AUTHENTICATION_BACKENDS = (self.backend,) + tuple(self.curr_auth)
     383        self.assertEqual(authenticate(username='test', password='test'), None)
     384
     385    def test_authenticates(self):
     386        settings.AUTHENTICATION_BACKENDS = tuple(self.curr_auth) + (self.backend,)
     387        self.assertEqual(authenticate(username='test', password='test'), self.user1)
  • django/contrib/auth/tests/__init__.py

     
    11from django.contrib.auth.tests.auth_backends import (BackendTest,
    22    RowlevelBackendTest, AnonymousUserBackendTest, NoAnonymousUserBackendTest,
    3     NoBackendsTest, InActiveUserBackendTest, NoInActiveUserBackendTest)
     3    NoBackendsTest, InActiveUserBackendTest, NoInActiveUserBackendTest,
     4    PermissionDeniedBackendTest)
    45from django.contrib.auth.tests.basic import BasicTestCase
    56from django.contrib.auth.tests.context_processors import AuthContextProcessorTests
    67from django.contrib.auth.tests.decorators import LoginRequiredTestCase
Back to Top