auth.txt 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. =======================
  2. ``django.contrib.auth``
  3. =======================
  4. This document provides API reference material for the components of Django's
  5. authentication system. For more details on the usage of these components or
  6. how to customize authentication and authorization see the :doc:`authentication
  7. topic guide </topics/auth/index>`.
  8. .. currentmodule:: django.contrib.auth
  9. ``User`` model
  10. ==============
  11. .. class:: models.User
  12. Fields
  13. ------
  14. .. class:: models.User
  15. :noindex:
  16. :class:`~django.contrib.auth.models.User` objects have the following
  17. fields:
  18. .. attribute:: username
  19. Required. 150 characters or fewer. Usernames may contain alphanumeric,
  20. ``_``, ``@``, ``+``, ``.`` and ``-`` characters.
  21. The ``max_length`` should be sufficient for many use cases. If you need
  22. a longer length, please use a :ref:`custom user model
  23. <specifying-custom-user-model>`.
  24. .. attribute:: first_name
  25. Optional (:attr:`blank=True <django.db.models.Field.blank>`). 150
  26. characters or fewer.
  27. .. attribute:: last_name
  28. Optional (:attr:`blank=True <django.db.models.Field.blank>`). 150
  29. characters or fewer.
  30. .. attribute:: email
  31. Optional (:attr:`blank=True <django.db.models.Field.blank>`). Email
  32. address.
  33. .. attribute:: password
  34. Required. A hash of, and metadata about, the password. (Django doesn't
  35. store the raw password.) Raw passwords can be arbitrarily long and can
  36. contain any character. The metadata in this field may mark the password
  37. as unusable. See the :doc:`password documentation
  38. </topics/auth/passwords>`.
  39. .. attribute:: groups
  40. Many-to-many relationship to :class:`~django.contrib.auth.models.Group`
  41. .. attribute:: user_permissions
  42. Many-to-many relationship to :class:`~django.contrib.auth.models.Permission`
  43. .. attribute:: is_staff
  44. Boolean. Allows this user to access the admin site.
  45. .. attribute:: is_active
  46. Boolean. Marks this user account as active. We recommend that you set
  47. this flag to ``False`` instead of deleting accounts. That way, if your
  48. applications have any foreign keys to users, the foreign keys won't
  49. break.
  50. This doesn't necessarily control whether or not the user can log in.
  51. Authentication backends aren't required to check for the ``is_active``
  52. flag but the default backend
  53. (:class:`~django.contrib.auth.backends.ModelBackend`) and the
  54. :class:`~django.contrib.auth.backends.RemoteUserBackend` do. You can
  55. use :class:`~django.contrib.auth.backends.AllowAllUsersModelBackend`
  56. or :class:`~django.contrib.auth.backends.AllowAllUsersRemoteUserBackend`
  57. if you want to allow inactive users to login. In this case, you'll also
  58. want to customize the
  59. :class:`~django.contrib.auth.forms.AuthenticationForm` used by the
  60. :class:`~django.contrib.auth.views.LoginView` as it rejects inactive
  61. users. Be aware that the permission-checking methods such as
  62. :meth:`~django.contrib.auth.models.User.has_perm` and the
  63. authentication in the Django admin all return ``False`` for inactive
  64. users.
  65. .. attribute:: is_superuser
  66. Boolean. Treats this user as having all permissions without assigning
  67. any permission to it in particular.
  68. .. attribute:: last_login
  69. A datetime of the user's last login.
  70. .. attribute:: date_joined
  71. The date/time when the account was created.
  72. Attributes
  73. ----------
  74. .. class:: models.User
  75. :noindex:
  76. .. attribute:: is_authenticated
  77. Read-only attribute which is always ``True`` (as opposed to
  78. ``AnonymousUser.is_authenticated`` which is always ``False``). This is
  79. a way to tell if the user has been authenticated. This does not imply
  80. any permissions and doesn't check if the user is active or has a valid
  81. session. Even though normally you will check this attribute on
  82. ``request.user`` to find out whether it has been populated by the
  83. :class:`~django.contrib.auth.middleware.AuthenticationMiddleware`
  84. (representing the currently logged-in user), you should know this
  85. attribute is ``True`` for any :class:`~models.User` instance.
  86. .. attribute:: is_anonymous
  87. Read-only attribute which is always ``False``. This is a way of
  88. differentiating :class:`~models.User` and :class:`~models.AnonymousUser`
  89. objects. Generally, you should prefer using
  90. :attr:`~django.contrib.auth.models.User.is_authenticated` to this
  91. attribute.
  92. Methods
  93. -------
  94. .. class:: models.User
  95. :noindex:
  96. .. method:: get_username()
  97. Returns the username for the user. Since the ``User`` model can be
  98. swapped out, you should use this method instead of referencing the
  99. username attribute directly.
  100. .. method:: get_full_name()
  101. Returns the :attr:`~django.contrib.auth.models.User.first_name` plus
  102. the :attr:`~django.contrib.auth.models.User.last_name`, with a space in
  103. between.
  104. .. method:: get_short_name()
  105. Returns the :attr:`~django.contrib.auth.models.User.first_name`.
  106. .. method:: set_password(raw_password)
  107. Sets the user's password to the given raw string, taking care of the
  108. password hashing. Doesn't save the
  109. :class:`~django.contrib.auth.models.User` object.
  110. When the ``raw_password`` is ``None``, the password will be set to an
  111. unusable password, as if
  112. :meth:`~django.contrib.auth.models.User.set_unusable_password()`
  113. were used.
  114. .. method:: check_password(raw_password)
  115. .. method:: acheck_password(raw_password)
  116. *Asynchronous version*: ``acheck_password()``
  117. Returns ``True`` if the given raw string is the correct password for
  118. the user. (This takes care of the password hashing in making the
  119. comparison.)
  120. .. method:: set_unusable_password()
  121. Marks the user as having no password set by updating the metadata in
  122. the :attr:`~django.contrib.auth.models.User.password` field. This isn't
  123. the same as having a blank string for a password.
  124. :meth:`~django.contrib.auth.models.User.check_password()` for this user
  125. will never return ``True``. Doesn't save the
  126. :class:`~django.contrib.auth.models.User` object.
  127. You may need this if authentication for your application takes place
  128. against an existing external source such as an LDAP directory.
  129. .. admonition:: Password reset restriction
  130. Users having an unusable password will not able to request a
  131. password reset email via
  132. :class:`~django.contrib.auth.views.PasswordResetView`.
  133. .. method:: has_usable_password()
  134. Returns ``False`` if
  135. :meth:`~django.contrib.auth.models.User.set_unusable_password()` has
  136. been called for this user.
  137. .. method:: get_user_permissions(obj=None)
  138. .. method:: aget_user_permissions(obj=None)
  139. *Asynchronous version*: ``aget_user_permissions()``
  140. Returns a set of permission strings that the user has directly.
  141. If ``obj`` is passed in, only returns the user permissions for this
  142. specific object.
  143. .. versionchanged:: 5.2
  144. ``aget_user_permissions()`` method was added.
  145. .. method:: get_group_permissions(obj=None)
  146. .. method:: aget_group_permissions(obj=None)
  147. *Asynchronous version*: ``aget_group_permissions()``
  148. Returns a set of permission strings that the user has, through their
  149. groups.
  150. If ``obj`` is passed in, only returns the group permissions for
  151. this specific object.
  152. .. versionchanged:: 5.2
  153. ``aget_group_permissions()`` method was added.
  154. .. method:: get_all_permissions(obj=None)
  155. .. method:: aget_all_permissions(obj=None)
  156. *Asynchronous version*: ``aget_all_permissions()``
  157. Returns a set of permission strings that the user has, both through
  158. group and user permissions.
  159. If ``obj`` is passed in, only returns the permissions for this
  160. specific object.
  161. .. versionchanged:: 5.2
  162. ``aget_all_permissions()`` method was added.
  163. .. method:: has_perm(perm, obj=None)
  164. .. method:: ahas_perm(perm, obj=None)
  165. *Asynchronous version*: ``ahas_perm()``
  166. Returns ``True`` if the user has the specified permission, where perm
  167. is in the format ``"<app label>.<permission codename>"``. (see
  168. documentation on :ref:`permissions <topic-authorization>`). If the user is
  169. inactive, this method will always return ``False``. For an active
  170. superuser, this method will always return ``True``.
  171. If ``obj`` is passed in, this method won't check for a permission for
  172. the model, but for this specific object.
  173. .. versionchanged:: 5.2
  174. ``ahas_perm()`` method was added.
  175. .. method:: has_perms(perm_list, obj=None)
  176. .. method:: ahas_perms(perm_list, obj=None)
  177. *Asynchronous version*: ``ahas_perms()``
  178. Returns ``True`` if the user has each of the specified permissions,
  179. where each perm is in the format
  180. ``"<app label>.<permission codename>"``. If the user is inactive,
  181. this method will always return ``False``. For an active superuser, this
  182. method will always return ``True``.
  183. If ``obj`` is passed in, this method won't check for permissions for
  184. the model, but for the specific object.
  185. .. versionchanged:: 5.2
  186. ``ahas_perms()`` method was added.
  187. .. method:: has_module_perms(package_name)
  188. .. method:: ahas_module_perms(package_name)
  189. *Asynchronous version*: ``ahas_module_perms()``
  190. Returns ``True`` if the user has any permissions in the given package
  191. (the Django app label). If the user is inactive, this method will
  192. always return ``False``. For an active superuser, this method will
  193. always return ``True``.
  194. .. versionchanged:: 5.2
  195. ``ahas_module_perms()`` method was added.
  196. .. method:: email_user(subject, message, from_email=None, **kwargs)
  197. Sends an email to the user. If ``from_email`` is ``None``, Django uses
  198. the :setting:`DEFAULT_FROM_EMAIL`. Any ``**kwargs`` are passed to the
  199. underlying :meth:`~django.core.mail.send_mail()` call.
  200. Manager methods
  201. ---------------
  202. .. class:: models.UserManager
  203. The :class:`~django.contrib.auth.models.User` model has a custom manager
  204. that has the following helper methods (in addition to the methods provided
  205. by :class:`~django.contrib.auth.models.BaseUserManager`):
  206. .. method:: create_user(username, email=None, password=None, **extra_fields)
  207. .. method:: acreate_user(username, email=None, password=None, **extra_fields)
  208. *Asynchronous version*: ``acreate_user()``
  209. Creates, saves and returns a :class:`~django.contrib.auth.models.User`.
  210. The :attr:`~django.contrib.auth.models.User.username` and
  211. :attr:`~django.contrib.auth.models.User.password` are set as given. The
  212. domain portion of :attr:`~django.contrib.auth.models.User.email` is
  213. automatically converted to lowercase, and the returned
  214. :class:`~django.contrib.auth.models.User` object will have
  215. :attr:`~django.contrib.auth.models.User.is_active` set to ``True``.
  216. If no password is provided,
  217. :meth:`~django.contrib.auth.models.User.set_unusable_password()` will
  218. be called.
  219. If no email is provided, :attr:`~django.contrib.auth.models.User.email`
  220. will be set to an empty string.
  221. The ``extra_fields`` keyword arguments are passed through to the
  222. :class:`~django.contrib.auth.models.User`’s ``__init__`` method to
  223. allow setting arbitrary fields on a :ref:`custom user model
  224. <auth-custom-user>`.
  225. See :ref:`Creating users <topics-auth-creating-users>` for example usage.
  226. .. versionchanged:: 5.2
  227. ``acreate_user()`` method was added.
  228. .. method:: create_superuser(username, email=None, password=None, **extra_fields)
  229. .. method:: acreate_superuser(username, email=None, password=None, **extra_fields)
  230. *Asynchronous version*: ``acreate_superuser()``
  231. Same as :meth:`create_user`, but sets :attr:`~models.User.is_staff` and
  232. :attr:`~models.User.is_superuser` to ``True``.
  233. .. versionchanged:: 5.2
  234. ``acreate_superuser()`` method was added.
  235. .. method:: with_perm(perm, is_active=True, include_superusers=True, backend=None, obj=None)
  236. Returns users that have the given permission ``perm`` either in the
  237. ``"<app label>.<permission codename>"`` format or as a
  238. :class:`~django.contrib.auth.models.Permission` instance. Returns an
  239. empty queryset if no users who have the ``perm`` found.
  240. If ``is_active`` is ``True`` (default), returns only active users, or
  241. if ``False``, returns only inactive users. Use ``None`` to return all
  242. users irrespective of active state.
  243. If ``include_superusers`` is ``True`` (default), the result will
  244. include superusers.
  245. If ``backend`` is passed in and it's defined in
  246. :setting:`AUTHENTICATION_BACKENDS`, then this method will use it.
  247. Otherwise, it will use the ``backend`` in
  248. :setting:`AUTHENTICATION_BACKENDS`, if there is only one, or raise an
  249. exception.
  250. ``AnonymousUser`` object
  251. ========================
  252. .. class:: models.AnonymousUser
  253. :class:`django.contrib.auth.models.AnonymousUser` is a class that
  254. implements the :class:`django.contrib.auth.models.User` interface, with
  255. these differences:
  256. * :ref:`id <automatic-primary-key-fields>` is always ``None``.
  257. * :attr:`~django.contrib.auth.models.User.username` is always the empty
  258. string.
  259. * :meth:`~django.contrib.auth.models.User.get_username()` always returns
  260. the empty string.
  261. * :attr:`~django.contrib.auth.models.User.is_anonymous` is ``True``
  262. instead of ``False``.
  263. * :attr:`~django.contrib.auth.models.User.is_authenticated` is
  264. ``False`` instead of ``True``.
  265. * :attr:`~django.contrib.auth.models.User.is_staff` and
  266. :attr:`~django.contrib.auth.models.User.is_superuser` are always
  267. ``False``.
  268. * :attr:`~django.contrib.auth.models.User.is_active` is always ``False``.
  269. * :attr:`~django.contrib.auth.models.User.groups` and
  270. :attr:`~django.contrib.auth.models.User.user_permissions` are always
  271. empty.
  272. * :meth:`~django.contrib.auth.models.User.set_password()`,
  273. :meth:`~django.contrib.auth.models.User.check_password()`,
  274. :meth:`~django.db.models.Model.save` and
  275. :meth:`~django.db.models.Model.delete()` raise :exc:`NotImplementedError`.
  276. In practice, you probably won't need to use
  277. :class:`~django.contrib.auth.models.AnonymousUser` objects on your own, but
  278. they're used by web requests, as explained in the next section.
  279. ``Permission`` model
  280. ====================
  281. .. class:: models.Permission
  282. Fields
  283. ------
  284. :class:`~django.contrib.auth.models.Permission` objects have the following
  285. fields:
  286. .. class:: models.Permission
  287. :noindex:
  288. .. attribute:: name
  289. Required. 255 characters or fewer. Example: ``'Can vote'``.
  290. .. attribute:: content_type
  291. Required. A foreign key to the
  292. :class:`~django.contrib.contenttypes.models.ContentType` model.
  293. .. attribute:: codename
  294. Required. 100 characters or fewer. Example: ``'can_vote'``.
  295. Methods
  296. -------
  297. :class:`~django.contrib.auth.models.Permission` objects have the standard
  298. data-access methods like any other :doc:`Django model </ref/models/instances>`.
  299. ``Group`` model
  300. ===============
  301. .. class:: models.Group
  302. Fields
  303. ------
  304. :class:`~django.contrib.auth.models.Group` objects have the following fields:
  305. .. class:: models.Group
  306. :noindex:
  307. .. attribute:: name
  308. Required. 150 characters or fewer. Any characters are permitted.
  309. Example: ``'Awesome Users'``.
  310. .. attribute:: permissions
  311. Many-to-many field to :class:`~django.contrib.auth.models.Permission`::
  312. group.permissions.set([permission_list])
  313. group.permissions.add(permission, permission, ...)
  314. group.permissions.remove(permission, permission, ...)
  315. group.permissions.clear()
  316. Validators
  317. ==========
  318. .. class:: validators.ASCIIUsernameValidator
  319. A field validator allowing only ASCII letters and numbers, in addition to
  320. ``@``, ``.``, ``+``, ``-``, and ``_``.
  321. .. class:: validators.UnicodeUsernameValidator
  322. A field validator allowing Unicode characters, in addition to ``@``, ``.``,
  323. ``+``, ``-``, and ``_``. The default validator for ``User.username``.
  324. .. _topics-auth-signals:
  325. Login and logout signals
  326. ========================
  327. .. module:: django.contrib.auth.signals
  328. The auth framework uses the following :doc:`signals </topics/signals>` that
  329. can be used for notification when a user logs in or out.
  330. .. data:: user_logged_in
  331. Sent when a user logs in successfully.
  332. Arguments sent with this signal:
  333. ``sender``
  334. The class of the user that just logged in.
  335. ``request``
  336. The current :class:`~django.http.HttpRequest` instance.
  337. ``user``
  338. The user instance that just logged in.
  339. .. data:: user_logged_out
  340. Sent when the logout method is called.
  341. ``sender``
  342. As above: the class of the user that just logged out or ``None``
  343. if the user was not authenticated.
  344. ``request``
  345. The current :class:`~django.http.HttpRequest` instance.
  346. ``user``
  347. The user instance that just logged out or ``None`` if the
  348. user was not authenticated.
  349. .. data:: user_login_failed
  350. Sent when the user failed to login successfully
  351. ``sender``
  352. The name of the module used for authentication.
  353. ``credentials``
  354. A dictionary of keyword arguments containing the user credentials that were
  355. passed to :func:`~django.contrib.auth.authenticate()` or your own custom
  356. authentication backend. Credentials matching a set of 'sensitive' patterns,
  357. (including password) will not be sent in the clear as part of the signal.
  358. ``request``
  359. The :class:`~django.http.HttpRequest` object, if one was provided to
  360. :func:`~django.contrib.auth.authenticate`.
  361. .. _authentication-backends-reference:
  362. Authentication backends
  363. =======================
  364. .. module:: django.contrib.auth.backends
  365. :synopsis: Django's built-in authentication backend classes.
  366. This section details the authentication backends that come with Django. For
  367. information on how to use them and how to write your own authentication
  368. backends, see the :ref:`Other authentication sources section
  369. <authentication-backends>` of the :doc:`User authentication guide
  370. </topics/auth/index>`.
  371. Available authentication backends
  372. ---------------------------------
  373. The following backends are available in :mod:`django.contrib.auth.backends`:
  374. .. class:: BaseBackend
  375. A base class that provides default implementations for all required
  376. methods. By default, it will reject any user and provide no permissions.
  377. .. method:: get_user_permissions(user_obj, obj=None)
  378. .. method:: aget_user_permissions(user_obj, obj=None)
  379. *Asynchronous version*: ``aget_user_permissions()``
  380. Returns an empty set.
  381. .. versionchanged:: 5.2
  382. ``aget_user_permissions()`` function was added.
  383. .. method:: get_group_permissions(user_obj, obj=None)
  384. .. method:: aget_group_permissions(user_obj, obj=None)
  385. *Asynchronous version*: ``aget_group_permissions()``
  386. Returns an empty set.
  387. .. versionchanged:: 5.2
  388. ``aget_group_permissions()`` function was added.
  389. .. method:: get_all_permissions(user_obj, obj=None)
  390. .. method:: aget_all_permissions(user_obj, obj=None)
  391. *Asynchronous version*: ``aget_all_permissions()``
  392. Uses :meth:`get_user_permissions` and :meth:`get_group_permissions` to
  393. get the set of permission strings the ``user_obj`` has.
  394. .. versionchanged:: 5.2
  395. ``aget_all_permissions()`` function was added.
  396. .. method:: has_perm(user_obj, perm, obj=None)
  397. .. method:: ahas_perm(user_obj, perm, obj=None)
  398. *Asynchronous version*: ``ahas_perm()``
  399. Uses :meth:`get_all_permissions` to check if ``user_obj`` has the
  400. permission string ``perm``.
  401. .. versionchanged:: 5.2
  402. ``ahas_perm()`` function was added.
  403. .. class:: ModelBackend
  404. This is the default authentication backend used by Django. It
  405. authenticates using credentials consisting of a user identifier and
  406. password. For Django's default user model, the user identifier is the
  407. username, for custom user models it is the field specified by
  408. USERNAME_FIELD (see :doc:`Customizing Users and authentication
  409. </topics/auth/customizing>`).
  410. It also handles the default permissions model as defined for
  411. :class:`~django.contrib.auth.models.User` and
  412. :class:`~django.contrib.auth.models.PermissionsMixin`.
  413. :meth:`has_perm`, :meth:`get_all_permissions`, :meth:`get_user_permissions`,
  414. and :meth:`get_group_permissions` allow an object to be passed as a
  415. parameter for object-specific permissions, but this backend does not
  416. implement them other than returning an empty set of permissions if
  417. ``obj is not None``.
  418. :meth:`with_perm` also allows an object to be passed as a parameter, but
  419. unlike others methods it returns an empty queryset if ``obj is not None``.
  420. .. method:: authenticate(request, username=None, password=None, **kwargs)
  421. .. method:: aauthenticate(request, username=None, password=None, **kwargs)
  422. *Asynchronous version*: ``aauthenticate()``
  423. Tries to authenticate ``username`` with ``password`` by calling
  424. :meth:`User.check_password
  425. <django.contrib.auth.models.User.check_password>`. If no ``username``
  426. is provided, it tries to fetch a username from ``kwargs`` using the
  427. key :attr:`CustomUser.USERNAME_FIELD
  428. <django.contrib.auth.models.CustomUser.USERNAME_FIELD>`. Returns an
  429. authenticated user or ``None``.
  430. ``request`` is an :class:`~django.http.HttpRequest` and may be ``None``
  431. if it wasn't provided to :func:`~django.contrib.auth.authenticate`
  432. (which passes it on to the backend).
  433. .. versionchanged:: 5.2
  434. ``aauthenticate()`` function was added.
  435. .. method:: get_user_permissions(user_obj, obj=None)
  436. .. method:: aget_user_permissions(user_obj, obj=None)
  437. *Asynchronous version*: ``aget_user_permissions()``
  438. Returns the set of permission strings the ``user_obj`` has from their
  439. own user permissions. Returns an empty set if
  440. :attr:`~django.contrib.auth.models.AbstractBaseUser.is_anonymous` or
  441. :attr:`~django.contrib.auth.models.CustomUser.is_active` is ``False``.
  442. .. versionchanged:: 5.2
  443. ``aget_user_permissions()`` function was added.
  444. .. method:: get_group_permissions(user_obj, obj=None)
  445. .. method:: aget_group_permissions(user_obj, obj=None)
  446. *Asynchronous version*: ``aget_group_permissions()``
  447. Returns the set of permission strings the ``user_obj`` has from the
  448. permissions of the groups they belong. Returns an empty set if
  449. :attr:`~django.contrib.auth.models.AbstractBaseUser.is_anonymous` or
  450. :attr:`~django.contrib.auth.models.CustomUser.is_active` is ``False``.
  451. .. versionchanged:: 5.2
  452. ``aget_group_permissions()`` function was added.
  453. .. method:: get_all_permissions(user_obj, obj=None)
  454. .. method:: aget_all_permissions(user_obj, obj=None)
  455. *Asynchronous version*: ``aget_all_permissions()``
  456. Returns the set of permission strings the ``user_obj`` has, including both
  457. user permissions and group permissions. Returns an empty set if
  458. :attr:`~django.contrib.auth.models.AbstractBaseUser.is_anonymous` or
  459. :attr:`~django.contrib.auth.models.CustomUser.is_active` is ``False``.
  460. .. versionchanged:: 5.2
  461. ``aget_all_permissions()`` function was added.
  462. .. method:: has_perm(user_obj, perm, obj=None)
  463. .. method:: ahas_perm(user_obj, perm, obj=None)
  464. *Asynchronous version*: ``ahas_perm()``
  465. Uses :meth:`get_all_permissions` to check if ``user_obj`` has the
  466. permission string ``perm``. Returns ``False`` if the user is not
  467. :attr:`~django.contrib.auth.models.CustomUser.is_active`.
  468. .. versionchanged:: 5.2
  469. ``ahas_perm()`` function was added.
  470. .. method:: has_module_perms(user_obj, app_label)
  471. .. method:: ahas_module_perms(user_obj, app_label)
  472. *Asynchronous version*: ``ahas_module_perms()``
  473. Returns whether the ``user_obj`` has any permissions on the app
  474. ``app_label``.
  475. .. versionchanged:: 5.2
  476. ``ahas_module_perms()`` function was added.
  477. .. method:: user_can_authenticate()
  478. Returns whether the user is allowed to authenticate. To match the
  479. behavior of :class:`~django.contrib.auth.forms.AuthenticationForm`
  480. which :meth:`prohibits inactive users from logging in
  481. <django.contrib.auth.forms.AuthenticationForm.confirm_login_allowed>`,
  482. this method returns ``False`` for users with :attr:`is_active=False
  483. <django.contrib.auth.models.User.is_active>`. Custom user models that
  484. don't have an :attr:`~django.contrib.auth.models.CustomUser.is_active`
  485. field are allowed.
  486. .. method:: with_perm(perm, is_active=True, include_superusers=True, obj=None)
  487. Returns all active users who have the permission ``perm`` either in
  488. the form of ``"<app label>.<permission codename>"`` or a
  489. :class:`~django.contrib.auth.models.Permission` instance. Returns an
  490. empty queryset if no users who have the ``perm`` found.
  491. If ``is_active`` is ``True`` (default), returns only active users, or
  492. if ``False``, returns only inactive users. Use ``None`` to return all
  493. users irrespective of active state.
  494. If ``include_superusers`` is ``True`` (default), the result will
  495. include superusers.
  496. .. class:: AllowAllUsersModelBackend
  497. Same as :class:`ModelBackend` except that it doesn't reject inactive users
  498. because :meth:`~ModelBackend.user_can_authenticate` always returns ``True``.
  499. When using this backend, you'll likely want to customize the
  500. :class:`~django.contrib.auth.forms.AuthenticationForm` used by the
  501. :class:`~django.contrib.auth.views.LoginView` by overriding the
  502. :meth:`~django.contrib.auth.forms.AuthenticationForm.confirm_login_allowed`
  503. method as it rejects inactive users.
  504. .. class:: RemoteUserBackend
  505. Use this backend to take advantage of external-to-Django-handled
  506. authentication. It authenticates using usernames passed in
  507. :attr:`request.META['REMOTE_USER'] <django.http.HttpRequest.META>`. See
  508. the :doc:`Authenticating against REMOTE_USER </howto/auth-remote-user>`
  509. documentation.
  510. If you need more control, you can create your own authentication backend
  511. that inherits from this class and override these attributes or methods:
  512. .. attribute:: create_unknown_user
  513. ``True`` or ``False``. Determines whether or not a user object is
  514. created if not already in the database Defaults to ``True``.
  515. .. method:: authenticate(request, remote_user)
  516. .. method:: aauthenticate(request, remote_user)
  517. *Asynchronous version*: ``aauthenticate()``
  518. The username passed as ``remote_user`` is considered trusted. This
  519. method returns the user object with the given username, creating a new
  520. user object if :attr:`~RemoteUserBackend.create_unknown_user` is
  521. ``True``.
  522. Returns ``None`` if :attr:`~RemoteUserBackend.create_unknown_user` is
  523. ``False`` and a ``User`` object with the given username is not found in
  524. the database.
  525. ``request`` is an :class:`~django.http.HttpRequest` and may be ``None``
  526. if it wasn't provided to :func:`~django.contrib.auth.authenticate`
  527. (which passes it on to the backend).
  528. .. versionchanged:: 5.2
  529. ``aauthenticate()`` function was added.
  530. .. method:: clean_username(username)
  531. Performs any cleaning on the ``username`` (e.g. stripping LDAP DN
  532. information) prior to using it to get or create a user object. Returns
  533. the cleaned username.
  534. .. method:: configure_user(request, user, created=True)
  535. .. method:: aconfigure_user(request, user, created=True)
  536. *Asynchronous version*: ``aconfigure_user()``
  537. Configures the user on each authentication attempt. This method is
  538. called immediately after fetching or creating the user being
  539. authenticated, and can be used to perform custom setup actions, such as
  540. setting the user's groups based on attributes in an LDAP directory.
  541. Returns the user object. When fetching or creating an user is called
  542. from a synchronous context, ``configure_user`` is called,
  543. ``aconfigure_user`` is called from async contexts.
  544. The setup can be performed either once when the user is created
  545. (``created`` is ``True``) or on existing users (``created`` is
  546. ``False``) as a way of synchronizing attributes between the remote and
  547. the local systems.
  548. ``request`` is an :class:`~django.http.HttpRequest` and may be ``None``
  549. if it wasn't provided to :func:`~django.contrib.auth.authenticate`
  550. (which passes it on to the backend).
  551. .. versionchanged:: 5.2
  552. ``aconfigure_user()`` function was added.
  553. .. method:: user_can_authenticate()
  554. Returns whether the user is allowed to authenticate. This method
  555. returns ``False`` for users with :attr:`is_active=False
  556. <django.contrib.auth.models.User.is_active>`. Custom user models that
  557. don't have an :attr:`~django.contrib.auth.models.CustomUser.is_active`
  558. field are allowed.
  559. .. class:: AllowAllUsersRemoteUserBackend
  560. Same as :class:`RemoteUserBackend` except that it doesn't reject inactive
  561. users because :attr:`~RemoteUserBackend.user_can_authenticate` always
  562. returns ``True``.
  563. Utility functions
  564. =================
  565. .. currentmodule:: django.contrib.auth
  566. .. function:: get_user(request)
  567. .. function:: aget_user(request)
  568. *Asynchronous version*: ``aget_user()``
  569. Returns the user model instance associated with the given ``request``’s
  570. session.
  571. It checks if the authentication backend stored in the session is present in
  572. :setting:`AUTHENTICATION_BACKENDS`. If so, it uses the backend's
  573. ``get_user()`` method to retrieve the user model instance and then verifies
  574. the session by calling the user model's
  575. :meth:`~django.contrib.auth.models.AbstractBaseUser.get_session_auth_hash`
  576. method. If the verification fails and :setting:`SECRET_KEY_FALLBACKS` are
  577. provided, it verifies the session against each fallback key using
  578. :meth:`~django.contrib.auth.models.AbstractBaseUser.\
  579. get_session_auth_fallback_hash`.
  580. Returns an instance of :class:`~django.contrib.auth.models.AnonymousUser`
  581. if the authentication backend stored in the session is no longer in
  582. :setting:`AUTHENTICATION_BACKENDS`, if a user isn't returned by the
  583. backend's ``get_user()`` method, or if the session auth hash doesn't
  584. validate.