Ticket #2550: patch.diff

File patch.diff, 3.3 KB (added by aashu_dwivedi, 4 years ago)

the implementation along with test cases submitted by aashu_dwivedi

  • 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
     
    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/auth_backends.py

     
    354354        self.assertEqual(self.user1.has_module_perms("app1"), False)
    355355        self.assertEqual(self.user1.has_module_perms("app2"), False)
    356356
     357class PermissionDeniedBackend(object):
     358    """
     359    always raises PermissionDenied
     360    """
     361    supports_object_permissions = False
     362    supports_anonymous_user = False
     363
     364    def authenticate(self,username=None,password=None):
     365        raise PermissionDenied
     366 
     367class PermissionDeniedBackendTest(TestCase):
     368    """
     369    Tests that other backends are not checked once a backend raises PermissionDenied
     370    """
     371    backend = 'django.contrib.auth.tests.auth_backends.PermissionDeniedBackend'
     372
     373    def setUp(self):
     374        self.curr_auth = settings.AUTHENTICATION_BACKENDS
     375        self.user1 = User.objects.create_user('test', 'test@example.com', 'test')
     376        self.user1.save()
     377
     378    def tearDown(self):
     379        settings.AUTHENTICATION_BACKENDS = self.curr_auth
     380
     381    def test_permission_denied(self):
     382        "user is not authenticated after a backend raises permission denied #2550"
     383        settings.AUTHENTICATION_BACKENDS = (self.backend,)+tuple(self.curr_auth)
     384        self.assertEqual(authenticate(username='test',password='test'), None)
     385
     386    def test_authenticates(self):
     387        settings.AUTHENTICATION_BACKENDS = tuple(self.curr_auth) + (self.backend,)
     388        self.assertEqual(authenticate(username='test',password='test'),self.user1)
     389
  • 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,PermissionDeniedBackendTest)
    44from django.contrib.auth.tests.basic import BasicTestCase
    55from django.contrib.auth.tests.decorators import LoginRequiredTestCase
    66from django.contrib.auth.tests.forms import (UserCreationFormTest,
Back to Top