Ticket #20313: patch-20313-20170608.diff

File patch-20313-20170608.diff, 7.6 KB (added by Luc Saffre, 7 years ago)

A solution which works for me (using branch 1.11rc1) and maybe works for everybody but needs more work to be complete

  • django/contrib/auth/__init__.py

    diff --git a/django/contrib/auth/__init__.py b/django/contrib/auth/__init__.py
    index 426bd69..3f56386 100644
    a b def logout(request):  
    177177        request.session[LANGUAGE_SESSION_KEY] = language
    178178
    179179    if hasattr(request, 'user'):
    180         from django.contrib.auth.models import AnonymousUser
    181         request.user = AnonymousUser()
     180        # from django.contrib.auth.models import AnonymousUser
     181        # request.user = AnonymousUser()
     182        request.user = get_user_model().get_anonymous_user()
    182183
    183184
    184185def get_user_model():
    def get_user(request):  
    200201    Returns the user model instance associated with the given request session.
    201202    If no user is retrieved an instance of `AnonymousUser` is returned.
    202203    """
    203     from .models import AnonymousUser
     204    # from .models import AnonymousUser
    204205    user = None
    205206    try:
    206207        user_id = _get_user_session_key(request)
    def get_user(request):  
    222223                    request.session.flush()
    223224                    user = None
    224225
    225     return user or AnonymousUser()
     226    # return user or AnonymousUser()
     227    return user or get_user_model().get_anonymous_user()
    226228
    227229
    228230def get_permission_codename(action, opts):
  • django/contrib/auth/backends.py

    diff --git a/django/contrib/auth/backends.py b/django/contrib/auth/backends.py
    index 8df95db..f089783 100644
    a b  
    11from __future__ import unicode_literals
    22
    33from django.contrib.auth import get_user_model
    4 from django.contrib.auth.models import Permission
    54
    65UserModel = get_user_model()
    76
    class ModelBackend(object):  
    3635        return user_obj.user_permissions.all()
    3736
    3837    def _get_group_permissions(self, user_obj):
     38        from django.contrib.auth.models import Permission
    3939        user_groups_field = get_user_model()._meta.get_field('groups')
    4040        user_groups_query = 'group__%s' % user_groups_field.related_query_name()
    4141        return Permission.objects.filter(**{user_groups_query: user_obj})
    class ModelBackend(object):  
    4949        if not user_obj.is_active or user_obj.is_anonymous or obj is not None:
    5050            return set()
    5151
     52        from django.contrib.auth.models import Permission
    5253        perm_cache_name = '_%s_perm_cache' % from_name
    5354        if not hasattr(user_obj, perm_cache_name):
    5455            if user_obj.is_superuser:
  • django/contrib/auth/models.py

    diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py
    index 6055526..254fc42 100644
    a b class PermissionsMixin(models.Model):  
    296296        return _user_has_module_perms(self, app_label)
    297297
    298298
     299@python_2_unicode_compatible
     300class AnonymousUser(object):
     301    id = None
     302    pk = None
     303    username = ''
     304    is_staff = False
     305    is_active = False
     306    is_superuser = False
     307    _groups = EmptyManager(Group)
     308    _user_permissions = EmptyManager(Permission)
     309
     310    def __init__(self):
     311        pass
     312
     313    def __str__(self):
     314        return 'AnonymousUser'
     315
     316    def __eq__(self, other):
     317        return isinstance(other, self.__class__)
     318
     319    def __ne__(self, other):
     320        return not self.__eq__(other)
     321
     322    def __hash__(self):
     323        return 1  # instances always return the same hash value
     324
     325    def save(self):
     326        raise NotImplementedError("Django doesn't provide a DB representation for AnonymousUser.")
     327
     328    def delete(self):
     329        raise NotImplementedError("Django doesn't provide a DB representation for AnonymousUser.")
     330
     331    def set_password(self, raw_password):
     332        raise NotImplementedError("Django doesn't provide a DB representation for AnonymousUser.")
     333
     334    def check_password(self, raw_password):
     335        raise NotImplementedError("Django doesn't provide a DB representation for AnonymousUser.")
     336
     337    @property
     338    def groups(self):
     339        return self._groups
     340
     341    @property
     342    def user_permissions(self):
     343        return self._user_permissions
     344
     345    def get_group_permissions(self, obj=None):
     346        return set()
     347
     348    def get_all_permissions(self, obj=None):
     349        return _user_get_all_permissions(self, obj=obj)
     350
     351    def has_perm(self, perm, obj=None):
     352        return _user_has_perm(self, perm, obj=obj)
     353
     354    def has_perms(self, perm_list, obj=None):
     355        for perm in perm_list:
     356            if not self.has_perm(perm, obj):
     357                return False
     358        return True
     359
     360    def has_module_perms(self, module):
     361        return _user_has_module_perms(self, module)
     362
     363    @property
     364    def is_anonymous(self):
     365        return CallableTrue
     366
     367    @property
     368    def is_authenticated(self):
     369        return CallableFalse
     370
     371    def get_username(self):
     372        return self.username
     373
     374
    299375class AbstractUser(AbstractBaseUser, PermissionsMixin):
    300376    """
    301377    An abstract base class implementing a fully featured User model with
    class AbstractUser(AbstractBaseUser, PermissionsMixin):  
    365441        """
    366442        send_mail(subject, message, from_email, [self.email], **kwargs)
    367443
     444    @classmethod
     445    def get_anonymous_user(cls):
     446        """Return an instance of AnonymousUser. Alternative implementations
     447        for AUTH_USER_MODEL may override this to use an alternative
     448        AnonymousUser class or add custom initialization.
     449
     450        """
     451        return AnonymousUser()
     452
    368453
    369454class User(AbstractUser):
    370455    """
    class User(AbstractUser):  
    373458
    374459    Username, password and email are required. Other fields are optional.
    375460    """
     461
    376462    class Meta(AbstractUser.Meta):
    377463        swappable = 'AUTH_USER_MODEL'
    378464
    379465
    380 @python_2_unicode_compatible
    381 class AnonymousUser(object):
    382     id = None
    383     pk = None
    384     username = ''
    385     is_staff = False
    386     is_active = False
    387     is_superuser = False
    388     _groups = EmptyManager(Group)
    389     _user_permissions = EmptyManager(Permission)
    390 
    391     def __init__(self):
    392         pass
    393 
    394     def __str__(self):
    395         return 'AnonymousUser'
    396 
    397     def __eq__(self, other):
    398         return isinstance(other, self.__class__)
    399 
    400     def __ne__(self, other):
    401         return not self.__eq__(other)
    402 
    403     def __hash__(self):
    404         return 1  # instances always return the same hash value
    405 
    406     def save(self):
    407         raise NotImplementedError("Django doesn't provide a DB representation for AnonymousUser.")
    408 
    409     def delete(self):
    410         raise NotImplementedError("Django doesn't provide a DB representation for AnonymousUser.")
    411 
    412     def set_password(self, raw_password):
    413         raise NotImplementedError("Django doesn't provide a DB representation for AnonymousUser.")
    414 
    415     def check_password(self, raw_password):
    416         raise NotImplementedError("Django doesn't provide a DB representation for AnonymousUser.")
    417 
    418     @property
    419     def groups(self):
    420         return self._groups
    421 
    422     @property
    423     def user_permissions(self):
    424         return self._user_permissions
    425 
    426     def get_group_permissions(self, obj=None):
    427         return set()
    428 
    429     def get_all_permissions(self, obj=None):
    430         return _user_get_all_permissions(self, obj=obj)
    431 
    432     def has_perm(self, perm, obj=None):
    433         return _user_has_perm(self, perm, obj=obj)
    434 
    435     def has_perms(self, perm_list, obj=None):
    436         for perm in perm_list:
    437             if not self.has_perm(perm, obj):
    438                 return False
    439         return True
    440 
    441     def has_module_perms(self, module):
    442         return _user_has_module_perms(self, module)
    443 
    444     @property
    445     def is_anonymous(self):
    446         return CallableTrue
    447 
    448     @property
    449     def is_authenticated(self):
    450         return CallableFalse
    451 
    452     def get_username(self):
    453         return self.username
     466   
Back to Top