Fixed #3032 -- Added some useful methods and attributes so that AnonymousUser can proxy for a User a bit more logically. Patch from semenov.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6299 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-09-15 18:01:29 +00:00
parent 4f87e34315
commit 388182b622
4 changed files with 23 additions and 3 deletions

View File

@ -1,6 +1,7 @@
from django.core import validators
from django.core.exceptions import ImproperlyConfigured
from django.db import connection, models
from django.db.models.manager import EmptyManager
from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import smart_str
from django.utils.translation import ugettext_lazy as _
@ -293,6 +294,11 @@ class Message(models.Model):
class AnonymousUser(object):
id = None
username = ''
is_staff = False
is_active = True
is_superuser = False
_groups = EmptyManager()
_user_permissions = EmptyManager()
def __init__(self):
pass
@ -325,11 +331,11 @@ class AnonymousUser(object):
raise NotImplementedError
def _get_groups(self):
raise NotImplementedError
return self._groups
groups = property(_get_groups)
def _get_user_permissions(self):
raise NotImplementedError
return self._user_permissions
user_permissions = property(_get_user_permissions)
def has_perm(self, perm):

View File

@ -1,5 +1,5 @@
"""
>>> from models import User
>>> from models import User, AnonymousUser
>>> u = User.objects.create_user('testuser', 'test@example.com', 'testpw')
>>> u.has_usable_password()
True
@ -16,4 +16,11 @@ False
>>> u2 = User.objects.create_user('testuser2', 'test2@example.com')
>>> u2.has_usable_password()
False
>>> a = AnonymousUser()
>>> a.is_staff
False
>>> a.groups.all()
[]
>>> a.user_permissions.all()
[]
"""

View File

@ -111,3 +111,7 @@ class ManagerDescriptor(object):
if instance != None:
raise AttributeError, "Manager isn't accessible via %s instances" % type.__name__
return self.manager
class EmptyManager(Manager):
def get_query_set(self):
return self.get_empty_query_set()

View File

@ -244,6 +244,9 @@ Anonymous users
the ``django.contrib.auth.models.User`` interface, with these differences:
* ``id`` is always ``None``.
* ``is_staff`` and ``is_superuser`` are always False.
* ``is_active`` is always True.
* ``groups`` and ``user_permissions`` are always empty.
* ``is_anonymous()`` returns ``True`` instead of ``False``.
* ``is_authenticated()`` returns ``False`` instead of ``True``.
* ``has_perm()`` always returns ``False``.