Ticket #2550: ticket2550.diff

File ticket2550.diff, 4.4 KB (added by Łukasz Rekucki, 14 years ago)

Fixed indentation in the previous patch. Tweaked the docs a bit.

  • django/contrib/auth/__init__.py

    diff --git a/django/contrib/auth/__init__.py b/django/contrib/auth/__init__.py
    index 232a822..2151a7a 100644
    a b  
    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
    def authenticate(**credentials):  
    5656        except TypeError:
    5757            # This backend doesn't accept these credentials as arguments. Try the next one.
    5858            continue
     59        except PermissionDenied:
     60            # This backend says to stop in our tracks - this user should not be allowed in at all.
     61            return None
    5962        if user is None:
    6063            continue
    6164        # Annotate the user object with the path of the backend.
  • django/contrib/auth/tests/__init__.py

    diff --git a/django/contrib/auth/tests/__init__.py b/django/contrib/auth/tests/__init__.py
    index 3a8f55b..fde134d 100644
    a b  
    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.decorators import LoginRequiredTestCase
    67from django.contrib.auth.tests.forms import (UserCreationFormTest,
  • django/contrib/auth/tests/auth_backends.py

    diff --git a/django/contrib/auth/tests/auth_backends.py b/django/contrib/auth/tests/auth_backends.py
    index 256357a..51fd069 100644
    a b import warnings  
    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
    class NoInActiveUserBackendTest(TestCase):  
    354355        self.assertEqual(self.user1.has_module_perms("app1"), False)
    355356        self.assertEqual(self.user1.has_module_perms("app2"), False)
    356357
     358class PermissionDeniedBackend(object):
     359    """
     360    always raises PermissionDenied
     361    """
     362    supports_object_permissions = False
     363    supports_anonymous_user = False
     364
     365    def authenticate(self, username=None, password=None):
     366        raise PermissionDenied
     367
     368class PermissionDeniedBackendTest(TestCase):
     369    """
     370        Tests that other backends are not checked once a backend raises PermissionDenied
     371    """
     372    backend = 'django.contrib.auth.tests.auth_backends.PermissionDeniedBackend'
     373
     374    def setUp(self):
     375        self.curr_auth = settings.AUTHENTICATION_BACKENDS
     376        self.user1 = User.objects.create_user('test', 'test@example.com', 'test')
     377        self.user1.save()
     378
     379    def tearDown(self):
     380        settings.AUTHENTICATION_BACKENDS = self.curr_auth
     381
     382    def test_permission_denied(self):
     383        "user is not authenticated after a backend raises permission denied #2550"
     384        settings.AUTHENTICATION_BACKENDS = (self.backend,) + tuple(self.curr_auth)
     385        self.assertEqual(authenticate(username='test', password='test'), None)
     386
     387    def test_authenticates(self):
     388        settings.AUTHENTICATION_BACKENDS = tuple(self.curr_auth) + (self.backend,)
     389        self.assertEqual(authenticate(username='test', password='test'), self.user1)
  • docs/topics/auth.txt

    diff --git a/docs/topics/auth.txt b/docs/topics/auth.txt
    index 52ddf22..96cf0d8 100644
    a b processing at the first positive match.  
    15081508    you need to force users to re-authenticate using different methods. A simple
    15091509    way to do that is simply to execute ``Session.objects.all().delete()``.
    15101510
     1511.. versionadded:: 1.4
     1512
     1513If a backend raises a :class:`~django.core.exceptions.PermissionDenied`
     1514exception, authentication will immediately fail. Django won't check the backends that follow.
     1515
     1516
    15111517Writing an authentication backend
    15121518---------------------------------
    15131519
Back to Top