Ticket #19412: modelbackend-perms.diff

File modelbackend-perms.diff, 2.2 KB (added by Preston Holmes, 12 years ago)
  • django/contrib/auth/backends.py

    diff --git a/django/contrib/auth/backends.py b/django/contrib/auth/backends.py
    index db99c94..d045d6a 100644
    a b  
    11from __future__ import unicode_literals
    22from django.contrib.auth import get_user_model
    33from django.contrib.auth.models import Permission
     4from django.db.models.fields import FieldDoesNotExist
    45
    56
    67class ModelBackend(object):
    class ModelBackend(object):  
    3031            if user_obj.is_superuser:
    3132                perms = Permission.objects.all()
    3233            else:
    33                 user_groups_field = get_user_model()._meta.get_field('groups')
    34                 user_groups_query = 'group__%s' % user_groups_field.related_query_name()
    35                 perms = Permission.objects.filter(**{user_groups_query: user_obj})
     34                try:
     35                    user_groups_field = get_user_model()._meta.get_field('groups')
     36                    user_groups_query = 'group__%s' % user_groups_field.related_query_name()
     37                    perms = Permission.objects.filter(**{user_groups_query: user_obj})
     38                except FieldDoesNotExist:
     39                    # custom user object may not define groups
     40                    perms = Permission.objects.none()
    3641            perms = perms.values_list('content_type__app_label', 'codename').order_by()
    3742            user_obj._group_perm_cache = set(["%s.%s" % (ct, name) for ct, name in perms])
    3843        return user_obj._group_perm_cache
    class ModelBackend(object):  
    4146        if user_obj.is_anonymous() or obj is not None:
    4247            return set()
    4348        if not hasattr(user_obj, '_perm_cache'):
    44             user_obj._perm_cache = set(["%s.%s" % (p.content_type.app_label, p.codename) for p in user_obj.user_permissions.select_related()])
     49            if hasattr(user_obj, 'user_permissions'):
     50                user_obj._perm_cache = set(["%s.%s" % (p.content_type.app_label, p.codename) for p in user_obj.user_permissions.select_related()])
     51            else:
     52                user_obj._perm_cache = set()
    4553            user_obj._perm_cache.update(self.get_group_permissions(user_obj))
    4654        return user_obj._perm_cache
    4755
Back to Top