Ticket #12462: 12462.diff

File 12462.diff, 3.6 KB (added by Florian Apolloner, 15 years ago)
  • django/contrib/auth/models.py

    diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py
    index 8148d8a..ceab7ba 100644
    a b class User(models.Model):  
    218218        permissions = set()
    219219        for backend in auth.get_backends():
    220220            if hasattr(backend, "get_group_permissions"):
    221                 if obj is not None and backend.supports_object_permissions:
    222                     group_permissions = backend.get_group_permissions(self, obj)
     221                if obj is not None:
     222                    if backend.supports_object_permissions:
     223                        permissions.update(
     224                            backend.get_group_permissions(self, obj)
     225                        )
    223226                else:
    224                     group_permissions = backend.get_group_permissions(self)
    225                 permissions.update(group_permissions)
     227                    permissions.update(backend.get_group_permissions(self))
    226228        return permissions
    227229
    228230    def get_all_permissions(self, obj=None):
    229231        permissions = set()
    230232        for backend in auth.get_backends():
    231233            if hasattr(backend, "get_all_permissions"):
    232                 if obj is not None and backend.supports_object_permissions:
    233                     all_permissions = backend.get_all_permissions(self, obj)
     234                if obj is not None:
     235                    if backend.supports_object_permissions:
     236                        permissions.update(
     237                            backend.get_all_permissions(self, obj)
     238                        )
    234239                else:
    235                     all_permissions = backend.get_all_permissions(self)
    236                 permissions.update(all_permissions)
     240                    permissions.update(backend.get_all_permissions(self))
    237241        return permissions
    238242
    239243    def has_perm(self, perm, obj=None):
    class User(models.Model):  
    255259        # Otherwise we need to check the backends.
    256260        for backend in auth.get_backends():
    257261            if hasattr(backend, "has_perm"):
    258                 if obj is not None and backend.supports_object_permissions:
    259                     if backend.has_perm(self, perm, obj):
    260                         return True
     262                if obj is not None:
     263                    if (backend.supports_object_permissions and
     264                        backend.has_perm(self, perm, obj)):
     265                            return True
    261266                else:
    262267                    if backend.has_perm(self, perm):
    263268                        return True
  • 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 bf5611a..af15d0b 100644
    a b class BackendTest(TestCase):  
    6969        self.assertEqual(user.has_perm('test'), False)
    7070        self.assertEqual(user.has_perms(['auth.test2', 'auth.test3']), False)
    7171
     72    def test_has_no_object_perm(self):
     73        """Regressiontest for #12462"""
     74        user = User.objects.get(username='test')
     75        content_type=ContentType.objects.get_for_model(Group)
     76        perm = Permission.objects.create(name='test', content_type=content_type, codename='test')
     77        user.user_permissions.add(perm)
     78        user.save()
     79
     80        self.assertEqual(user.has_perm('auth.test', 'object'), False)
     81        self.assertEqual(user.get_all_permissions('object'), set([]))
     82        self.assertEqual(user.has_perm('auth.test'), True)
     83        self.assertEqual(user.get_all_permissions(), set(['auth.test']))
     84
     85
     86
    7287
    7388class TestObj(object):
    7489    pass
Back to Top