diff options
| author | Jeremy Lainé <jeremy.laine@m4x.org> | 2016-04-02 13:18:26 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-04-09 14:54:18 -0400 |
| commit | c1aec0feda73ede09503192a66f973598aef901d (patch) | |
| tree | f1e4c09f3e98177cfe78cc9039b300f8984e7aed /docs | |
| parent | c16b9dd8e0ae757616e9accbaffecc521191ee98 (diff) | |
Fixed #25847 -- Made User.is_(anonymous|authenticated) properties.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/howto/error-reporting.txt | 2 | ||||
| -rw-r--r-- | docs/internals/deprecation.txt | 3 | ||||
| -rw-r--r-- | docs/ref/contrib/auth.txt | 73 | ||||
| -rw-r--r-- | docs/ref/request-response.txt | 4 | ||||
| -rw-r--r-- | docs/releases/1.10.txt | 35 | ||||
| -rw-r--r-- | docs/topics/auth/customizing.txt | 38 | ||||
| -rw-r--r-- | docs/topics/auth/default.txt | 12 | ||||
| -rw-r--r-- | docs/topics/cache.txt | 2 | ||||
| -rw-r--r-- | docs/topics/class-based-views/mixins.txt | 6 |
9 files changed, 120 insertions, 55 deletions
diff --git a/docs/howto/error-reporting.txt b/docs/howto/error-reporting.txt index 9ae194d4e0..d07404a1e9 100644 --- a/docs/howto/error-reporting.txt +++ b/docs/howto/error-reporting.txt @@ -256,7 +256,7 @@ given view by setting the ``HttpRequest``’s ``exception_reporter_filter`` attribute:: def my_view(request): - if request.user.is_authenticated(): + if request.user.is_authenticated: request.exception_reporter_filter = CustomExceptionReporterFilter() ... diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt index 810f7d1a2b..d428f414f7 100644 --- a/docs/internals/deprecation.txt +++ b/docs/internals/deprecation.txt @@ -147,6 +147,9 @@ details on these changes. * The shim for supporting custom related manager classes without a ``_apply_rel_filters()`` method will be removed. +* Using ``User.is_authenticated()`` and ``User.is_anonymous()`` as methods + will no longer be supported. + .. _deprecation-removed-in-1.10: 1.10 diff --git a/docs/ref/contrib/auth.txt b/docs/ref/contrib/auth.txt index 831de3b5a6..6e3b8892cf 100644 --- a/docs/ref/contrib/auth.txt +++ b/docs/ref/contrib/auth.txt @@ -111,6 +111,41 @@ Fields A datetime designating when the account was created. Is set to the current date/time by default when the account is created. +Attributes +---------- + +.. class:: models.User + + .. attribute:: is_authenticated + + Read-only attribute which is always ``True`` (as opposed to + ``AnonymousUser.is_authenticated`` which is always ``False``). This is + a way to tell if the user has been authenticated. This does not imply + any permissions and doesn't check if the user is active or has a valid + session. Even though normally you will check this attribute on + ``request.user`` to find out whether it has been populated by the + :class:`~django.contrib.auth.middleware.AuthenticationMiddleware` + (representing the currently logged-in user), you should know this + attribute is ``True`` for any :class:`~models.User` instance. + + .. versionchanged:: 1.10 + + In older versions, this was a method. Backwards-compatibility + support for using it as a method will be removed in Django 2.0. + + .. attribute:: is_anonymous + + Read-only attribute which is always ``False``. This is a way of + differentiating :class:`~models.User` and :class:`~models.AnonymousUser` + objects. Generally, you should prefer using + :attr:`~django.contrib.auth.models.User.is_authenticated` to this + attribute. + + .. versionchanged:: 1.10 + + In older versions, this was a method. Backwards-compatibility + support for using it as a method will be removed in Django 2.0. + Methods ------- @@ -119,31 +154,9 @@ Methods .. method:: get_username() Returns the username for the user. Since the User model can be swapped - out, you should use this method instead of referencing the username + out, you should use this method instead of referencing the username attribute directly. - .. method:: is_anonymous() - - Always returns ``False``. This is a way of differentiating - :class:`~django.contrib.auth.models.User` and - :class:`~django.contrib.auth.models.AnonymousUser` objects. - Generally, you should prefer using - :meth:`~django.contrib.auth.models.User.is_authenticated()` to this - method. - - .. method:: is_authenticated() - - Always returns ``True`` (as opposed to - ``AnonymousUser.is_authenticated()`` which always returns ``False``). - This is a way to tell if the user has been authenticated. This does not - imply any permissions, and doesn't check if the user is active or has - a valid session. Even though normally you will call this method on - ``request.user`` to find out whether it has been populated by the - :class:`~django.contrib.auth.middleware.AuthenticationMiddleware` - (representing the currently logged-in user), you should know this method - returns ``True`` for any :class:`~django.contrib.auth.models.User` - instance. - .. method:: get_full_name() Returns the :attr:`~django.contrib.auth.models.User.first_name` plus @@ -287,6 +300,10 @@ Manager methods string. * :meth:`~django.contrib.auth.models.User.get_username()` always returns the empty string. + * :attr:`~django.contrib.auth.models.User.is_anonymous` is ``True`` + instead of ``False``. + * :attr:`~django.contrib.auth.models.User.is_authenticated` is + ``False`` instead of ``True``. * :attr:`~django.contrib.auth.models.User.is_staff` and :attr:`~django.contrib.auth.models.User.is_superuser` are always ``False``. @@ -294,10 +311,6 @@ Manager methods * :attr:`~django.contrib.auth.models.User.groups` and :attr:`~django.contrib.auth.models.User.user_permissions` are always empty. - * :meth:`~django.contrib.auth.models.User.is_anonymous()` returns ``True`` - instead of ``False``. - * :meth:`~django.contrib.auth.models.User.is_authenticated()` returns - ``False`` instead of ``True``. * :meth:`~django.contrib.auth.models.User.set_password()`, :meth:`~django.contrib.auth.models.User.check_password()`, :meth:`~django.db.models.Model.save` and @@ -471,21 +484,21 @@ The following backends are available in :mod:`django.contrib.auth.backends`: Returns the set of permission strings the ``user_obj`` has from their own user permissions. Returns an empty set if - :meth:`~django.contrib.auth.models.AbstractBaseUser.is_anonymous` or + :attr:`~django.contrib.auth.models.AbstractBaseUser.is_anonymous` or :attr:`~django.contrib.auth.models.CustomUser.is_active` is ``False``. .. method:: get_group_permissions(user_obj, obj=None) Returns the set of permission strings the ``user_obj`` has from the permissions of the groups they belong. Returns an empty set if - :meth:`~django.contrib.auth.models.AbstractBaseUser.is_anonymous` or + :attr:`~django.contrib.auth.models.AbstractBaseUser.is_anonymous` or :attr:`~django.contrib.auth.models.CustomUser.is_active` is ``False``. .. method:: get_all_permissions(user_obj, obj=None) Returns the set of permission strings the ``user_obj`` has, including both user permissions and group permissions. Returns an empty set if - :meth:`~django.contrib.auth.models.AbstractBaseUser.is_anonymous` or + :attr:`~django.contrib.auth.models.AbstractBaseUser.is_anonymous` or :attr:`~django.contrib.auth.models.CustomUser.is_active` is ``False``. .. method:: has_perm(user_obj, perm, obj=None) diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt index acf195803b..a1be4d64b9 100644 --- a/docs/ref/request-response.txt +++ b/docs/ref/request-response.txt @@ -233,9 +233,9 @@ middleware class is listed in :setting:`MIDDLEWARE_CLASSES`. logged-in user. If the user isn't currently logged in, ``user`` will be set to an instance of :class:`~django.contrib.auth.models.AnonymousUser`. You can tell them apart with - :meth:`~django.contrib.auth.models.User.is_authenticated`, like so:: + :attr:`~django.contrib.auth.models.User.is_authenticated`, like so:: - if request.user.is_authenticated(): + if request.user.is_authenticated: ... # Do something for logged-in users. else: ... # Do something for anonymous users. diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt index 2002905b2d..463cd4706e 100644 --- a/docs/releases/1.10.txt +++ b/docs/releases/1.10.txt @@ -730,6 +730,10 @@ Miscellaneous * Middleware classes are now initialized when the server starts rather than during the first request. +* If you override ``is_authenticated()`` or ``is_anonymous()`` in a custom user + model, you must convert them to attributes or properties as described in + :ref:`the deprecation note <user-is-auth-anon-deprecation>`. + .. _deprecated-features-1.10: Features deprecated in 1.10 @@ -857,6 +861,37 @@ features, is deprecated. Replace it with a custom lookup:: models.CharField.register_lookup(Search) models.TextField.register_lookup(Search) +.. _user-is-auth-anon-deprecation: + +Using ``User.is_authenticated()`` and ``User.is_anonymous()`` as methods +------------------------------------------------------------------------ + +The ``is_authenticated()`` and ``is_anonymous()`` methods of +:class:`~django.contrib.auth.models.AbstractBaseUser` and +:class:`~django.contrib.auth.models.AnonymousUser` classes are now +properties. They will still work as methods until Django 2.0, but all usage +in Django now uses attribute access. + +For example, if you use +:class:`~django.contrib.auth.middleware.AuthenticationMiddleware` and want +to know whether the user is currently logged-in you would use:: + + if request.user.is_authenticated: + ... # Do something for logged-in users. + else: + ... # Do something for anonymous users. + +instead of ``request.user.is_authenticated()``. + +This change avoids accidental information leakage if you forget to call the +method, e.g.:: + + if request.user.is_authenticated: + return sensitive_information + +If you override these methods in a custom user model, you must change them to +properties or attributes. + Custom manager classes available through ``prefetch_related`` must define a ``_apply_rel_filters()`` method ----------------------------------------------------------------------------------------------------------- diff --git a/docs/topics/auth/customizing.txt b/docs/topics/auth/customizing.txt index 271d7f52f7..68ca0e7f55 100644 --- a/docs/topics/auth/customizing.txt +++ b/docs/topics/auth/customizing.txt @@ -603,7 +603,7 @@ password resets. You must then provide some key implementation details: raised a deprecation warning in older versions and is no longer supported in Django 1.9). -The following methods are available on any subclass of +The following attributes and methods are available on any subclass of :class:`~django.contrib.auth.models.AbstractBaseUser`: .. class:: models.AbstractBaseUser @@ -612,20 +612,34 @@ The following methods are available on any subclass of Returns the value of the field nominated by ``USERNAME_FIELD``. - .. method:: models.AbstractBaseUser.is_anonymous() + .. attribute:: models.AbstractBaseUser.is_authenticated - Always returns ``False``. This is a way of differentiating - from :class:`~django.contrib.auth.models.AnonymousUser` objects. - Generally, you should prefer using - :meth:`~django.contrib.auth.models.AbstractBaseUser.is_authenticated()` to this - method. + Read-only attribute which is always ``True`` (as opposed to + ``AnonymousUser.is_authenticated`` which is always ``False``). + This is a way to tell if the user has been authenticated. This does not + imply any permissions and doesn't check if the user is active or has + a valid session. Even though normally you will check this attribute on + ``request.user`` to find out whether it has been populated by the + :class:`~django.contrib.auth.middleware.AuthenticationMiddleware` + (representing the currently logged-in user), you should know this + attribute is ``True`` for any :class:`~models.User` instance. - .. method:: models.AbstractBaseUser.is_authenticated() + .. versionchanged:: 1.10 - Always returns ``True``. This is a way to tell if the user has been - authenticated. This does not imply any permissions, and doesn't check - if the user is active - it only indicates that the user has provided a - valid username and password. + In older versions, this was a method. Backwards-compatibility + support for using it as a method will be removed in Django 2.0. + + .. attribute:: models.AbstractBaseUser.is_anonymous + + Read-only attribute which is always ``False``. This is a way of + differentiating :class:`~models.User` and :class:`~models.AnonymousUser` + objects. Generally, you should prefer using + :attr:`~models.User.is_authenticated` to this attribute. + + .. versionchanged:: 1.10 + + In older versions, this was a method. Backwards-compatibility + support for using it as a method will be removed in Django 2.0. .. method:: models.AbstractBaseUser.set_password(raw_password) diff --git a/docs/topics/auth/default.txt b/docs/topics/auth/default.txt index 72205740d5..4c8d44a0cd 100644 --- a/docs/topics/auth/default.txt +++ b/docs/topics/auth/default.txt @@ -306,9 +306,9 @@ of :class:`~django.contrib.auth.models.AnonymousUser`, otherwise it will be an instance of :class:`~django.contrib.auth.models.User`. You can tell them apart with -:meth:`~django.contrib.auth.models.User.is_authenticated()`, like so:: +:attr:`~django.contrib.auth.models.User.is_authenticated`, like so:: - if request.user.is_authenticated(): + if request.user.is_authenticated: # Do something for authenticated users. ... else: @@ -421,15 +421,15 @@ The raw way ~~~~~~~~~~~ The simple, raw way to limit access to pages is to check -:meth:`request.user.is_authenticated() -<django.contrib.auth.models.User.is_authenticated()>` and either redirect to a +:attr:`request.user.is_authenticated +<django.contrib.auth.models.User.is_authenticated>` and either redirect to a login page:: from django.conf import settings from django.shortcuts import redirect def my_view(request): - if not request.user.is_authenticated(): + if not request.user.is_authenticated: return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path)) # ... @@ -438,7 +438,7 @@ login page:: from django.shortcuts import render def my_view(request): - if not request.user.is_authenticated(): + if not request.user.is_authenticated: return render(request, 'myapp/login_error.html') # ... diff --git a/docs/topics/cache.txt b/docs/topics/cache.txt index 4cfffeeecf..b0bf37c94c 100644 --- a/docs/topics/cache.txt +++ b/docs/topics/cache.txt @@ -1170,7 +1170,7 @@ decorator):: @vary_on_cookie def list_blog_entries_view(request): - if request.user.is_anonymous(): + if request.user.is_anonymous: response = render_only_public_entries() patch_cache_control(response, public=True) else: diff --git a/docs/topics/class-based-views/mixins.txt b/docs/topics/class-based-views/mixins.txt index e53a8599e4..a5bb290d2b 100644 --- a/docs/topics/class-based-views/mixins.txt +++ b/docs/topics/class-based-views/mixins.txt @@ -235,7 +235,7 @@ We'll demonstrate this with the ``Author`` model we used in the model = Author def post(self, request, *args, **kwargs): - if not request.user.is_authenticated(): + if not request.user.is_authenticated: return HttpResponseForbidden() # Look up the author we're interested in. @@ -466,7 +466,7 @@ Our new ``AuthorDetail`` looks like this:: return context def post(self, request, *args, **kwargs): - if not request.user.is_authenticated(): + if not request.user.is_authenticated: return HttpResponseForbidden() self.object = self.get_object() form = self.get_form() @@ -552,7 +552,7 @@ template as ``AuthorDisplay`` is using on ``GET``:: model = Author def post(self, request, *args, **kwargs): - if not request.user.is_authenticated(): + if not request.user.is_authenticated: return HttpResponseForbidden() self.object = self.get_object() return super(AuthorInterest, self).post(request, *args, **kwargs) |
