From 388182b62299e063f843b0157e510a4021f7acfd Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sat, 15 Sep 2007 18:01:29 +0000 Subject: [PATCH] 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 --- django/contrib/auth/models.py | 10 ++++++++-- django/contrib/auth/tests.py | 9 ++++++++- django/db/models/manager.py | 4 ++++ docs/authentication.txt | 3 +++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py index 3584cea533d..7cbeb26af6f 100644 --- a/django/contrib/auth/models.py +++ b/django/contrib/auth/models.py @@ -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): diff --git a/django/contrib/auth/tests.py b/django/contrib/auth/tests.py index ed768aa429a..329049c546d 100644 --- a/django/contrib/auth/tests.py +++ b/django/contrib/auth/tests.py @@ -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() +[] """ \ No newline at end of file diff --git a/django/db/models/manager.py b/django/db/models/manager.py index 040b86656a5..7b2e9167387 100644 --- a/django/db/models/manager.py +++ b/django/db/models/manager.py @@ -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() diff --git a/docs/authentication.txt b/docs/authentication.txt index 907b72e44a0..713e86c140f 100644 --- a/docs/authentication.txt +++ b/docs/authentication.txt @@ -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``.