Ticket #25057: 25057_inactive_user.diff

File 25057_inactive_user.diff, 3.3 KB (added by Grigoriy Kramarenko, 9 years ago)
  • django/contrib/auth/models.py

    diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py
    index ad46c5c..44170c7 100644
    a b class Permission(models.Model):  
    5959    created for each Django model.
    6060    """
    6161    name = models.CharField(_('name'), max_length=255)
    62     content_type = models.ForeignKey(ContentType)
     62    content_type = models.ForeignKey(ContentType, verbose_name=_('content type'))
    6363    codename = models.CharField(_('codename'), max_length=100)
    6464    objects = PermissionManager()
    6565
    class PermissionsMixin(models.Model):  
    251251    def get_all_permissions(self, obj=None):
    252252        return _user_get_all_permissions(self, obj)
    253253
    254     def has_perm(self, perm, obj=None):
     254    def has_perm(self, perm, obj=None, check_active=True):
    255255        """
    256256        Returns True if the user has the specified permission. This method
    257257        queries all available auth backends, but returns immediately if any
    class PermissionsMixin(models.Model):  
    260260        provided, permissions for this specific object are checked.
    261261        """
    262262
    263         # Active superusers have all permissions.
    264         if self.is_active and self.is_superuser:
     263        if check_active and not self.is_active:
     264            return False
     265
     266        # Superusers have all permissions.
     267        if self.is_superuser:
    265268            return True
    266269
    267270        # Otherwise we need to check the backends.
    268271        return _user_has_perm(self, perm, obj)
    269272
    270     def has_perms(self, perm_list, obj=None):
     273    def has_perms(self, perm_list, obj=None, check_active=True):
    271274        """
    272275        Returns True if the user has each of the specified permissions. If
    273276        object is passed, it checks if the user has all required perms for this
    274277        object.
    275278        """
    276279        for perm in perm_list:
    277             if not self.has_perm(perm, obj):
     280            if not self.has_perm(perm, obj, check_active):
    278281                return False
    279282        return True
    280283
    281     def has_module_perms(self, app_label):
     284    def has_module_perms(self, app_label, check_active=True):
    282285        """
    283286        Returns True if the user has any permissions in the given app label.
    284287        Uses pretty much the same logic as has_perm, above.
    285288        """
    286         # Active superusers have all permissions.
    287         if self.is_active and self.is_superuser:
     289        if check_active and not self.is_active:
     290            return False
     291
     292        # Superusers have all permissions.
     293        if self.is_superuser:
    288294            return True
    289295
    290296        return _user_has_module_perms(self, app_label)
  • tests/auth_tests/test_auth_backends.py

    diff --git a/tests/auth_tests/test_auth_backends.py b/tests/auth_tests/test_auth_backends.py
    index 6a642aa..da2f18d 100644
    a b class InActiveUserBackendTest(TestCase):  
    445445
    446446    def test_has_perm(self):
    447447        self.assertEqual(self.user1.has_perm('perm', TestObj()), False)
    448         self.assertEqual(self.user1.has_perm('inactive', TestObj()), True)
     448        #self.assertEqual(self.user1.has_perm('inactive', TestObj()), True) # Why?
     449        # Inactive user should not have any permissions!
     450        self.assertEqual(self.user1.has_perm('inactive', TestObj()), False)
    449451
    450452    def test_has_module_perms(self):
    451453        self.assertEqual(self.user1.has_module_perms("app1"), False)
Back to Top