From 5b8ef18dcc006c0fa53b356efbb7cf2c691463d5 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sat, 4 Dec 2010 05:59:56 +0000 Subject: [PATCH] Fixed #14795 -- Ensure that get_all_permissions() returns the right result (i.e., all permissions) for superusers. Thanks to jay.halleaux@gmail.com for the report, and Brett Haydon for the patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14797 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/auth/backends.py | 8 +++++--- django/contrib/auth/tests/auth_backends.py | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/django/contrib/auth/backends.py b/django/contrib/auth/backends.py index 9d674664ebc..2f608043cb7 100644 --- a/django/contrib/auth/backends.py +++ b/django/contrib/auth/backends.py @@ -25,9 +25,11 @@ class ModelBackend(object): groups. """ if not hasattr(user_obj, '_group_perm_cache'): - perms = Permission.objects.filter(group__user=user_obj - ).values_list('content_type__app_label', 'codename' - ).order_by() + if user_obj.is_superuser: + perms = Permission.objects.all() + else: + perms = Permission.objects.filter(group__user=user_obj) + perms = perms.values_list('content_type__app_label', 'codename').order_by() user_obj._group_perm_cache = set(["%s.%s" % (ct, name) for ct, name in perms]) return user_obj._group_perm_cache diff --git a/django/contrib/auth/tests/auth_backends.py b/django/contrib/auth/tests/auth_backends.py index 490861cb436..e931152f77a 100644 --- a/django/contrib/auth/tests/auth_backends.py +++ b/django/contrib/auth/tests/auth_backends.py @@ -15,6 +15,7 @@ class BackendTest(TestCase): self.curr_auth = settings.AUTHENTICATION_BACKENDS settings.AUTHENTICATION_BACKENDS = (self.backend,) User.objects.create_user('test', 'test@example.com', 'test') + User.objects.create_superuser('test2', 'test2@example.com', 'test') def tearDown(self): settings.AUTHENTICATION_BACKENDS = self.curr_auth @@ -90,6 +91,10 @@ class BackendTest(TestCase): self.assertEqual(user.has_perm('auth.test'), True) self.assertEqual(user.get_all_permissions(), set(['auth.test'])) + def test_get_all_superuser_permissions(self): + "A superuser has all permissions. Refs #14795" + user = User.objects.get(username='test2') + self.assertEqual(len(user.get_all_permissions()), len(Permission.objects.all())) class TestObj(object): pass