Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions django/contrib/auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class Meta:
def __str__(self):
return "%s | %s" % (self.content_type, self.name)

@property
def user_perm_str(self):
"""String representation for the user permission check."""
return f"{self.content_type.app_label}.{self.codename}"

def natural_key(self):
return (self.codename, *self.content_type.natural_key())

Expand Down
6 changes: 3 additions & 3 deletions docs/howto/deployment/wsgi/apache-auth.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ How to authenticate against Django's user database from Apache
Since keeping multiple authentication databases in sync is a common problem
when dealing with Apache, you can configure Apache to authenticate against
Django's :doc:`authentication system </topics/auth/index>` directly. This
requires Apache version >= 2.2 and mod_wsgi >= 2.0. For example, you could:
requires Apache version >= 2.2 and ``mod_wsgi`` >= 2.0. For example, you could:

* Serve static/media files directly from Apache only to authenticated users.

Expand All @@ -23,7 +23,7 @@ requires Apache version >= 2.2 and mod_wsgi >= 2.0. For example, you could:
auth handler if your custom cannot conform to these requirements.

.. _Subversion: https://subversion.apache.org/
.. _mod_dav: https://httpd.apache.org/docs/2.2/mod/mod_dav.html
.. _mod_dav: https://httpd.apache.org/docs/current/mod/mod_dav.html

Authentication with ``mod_wsgi``
================================
Expand Down Expand Up @@ -66,7 +66,7 @@ password that it receives from the prompt. In this example, the
application :doc:`that is created by django-admin startproject
</howto/deployment/wsgi/index>`.

.. admonition:: Using Apache 2.2 with authentication
.. admonition:: Using Apache 2.2+ with authentication

Make sure that ``mod_auth_basic`` and ``mod_authz_user`` are loaded.

Expand Down
3 changes: 0 additions & 3 deletions docs/howto/deployment/wsgi/uwsgi.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ command. For example:
# Install current stable version.
$ python -m pip install uwsgi

# Or install LTS (long term support).
$ python -m pip install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz

.. _installation procedures: https://uwsgi-docs.readthedocs.io/en/latest/Install.html

uWSGI model
Expand Down
10 changes: 10 additions & 0 deletions docs/ref/contrib/auth.txt
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,16 @@ Methods
:class:`~django.contrib.auth.models.Permission` objects have the standard
data-access methods like any other :doc:`Django model </ref/models/instances>`.

.. class:: models.Permission
:noindex:

.. attribute:: user_perm_str

.. versionadded:: 6.1

Returns the string representation for use in :meth:`has_perm
<django.contrib.auth.models.User.has_perm>`.

``Group`` model
===============

Expand Down
3 changes: 3 additions & 0 deletions docs/releases/6.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ Minor features
* :attr:`.Permission.name` and :attr:`.Permission.codename` values are now
renamed when renaming models via a migration.

* The new :attr:`.Permission.user_perm_str` property returns the string
suitable to use with :meth:`.User.has_perm`.

:mod:`django.contrib.contenttypes`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
9 changes: 9 additions & 0 deletions docs/topics/auth/default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ to test for basic permissions you should use:
The :class:`~django.contrib.auth.models.Permission` model is rarely accessed
directly.

The :class:`~django.contrib.auth.models.Permission` model provides a helper
property, :attr:`~django.contrib.auth.models.Permission.user_perm_str`,
that returns the string representation that can be used to check permission
using the :meth:`~django.contrib.auth.models.User.has_perm` method.

.. versionchanged:: 6.1

The ``user_perm_str`` property was added.

Groups
------

Expand Down
4 changes: 4 additions & 0 deletions tests/auth_tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,7 @@ def test_str(self):
self.assertEqual(
str(p), "Auth_Tests | custom email field | Can view custom email field"
)

def test_user_perm_str(self):
p = Permission.objects.get(codename="view_customemailfield")
self.assertEqual(p.user_perm_str, "auth_tests.view_customemailfield")
Loading