auth.txt 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. The ``extra_fields`` keyword arguments are passed through to the
  220. :class:`~django.contrib.auth.models.User`’s ``__init__`` method to
  221. allow setting arbitrary fields on a :ref:`custom user model
  222. <auth-custom-user>`.
  223. See :ref:`Creating users <topics-auth-creating-users>` for example usage.
  224. .. versionchanged:: 5.2
  225. ``acreate_user()`` method was added.
  226. .. method:: create_superuser(username, email=None, password=None, **extra_fields)
  227. .. method:: acreate_superuser(username, email=None, password=None, **extra_fields)
  228. *Asynchronous version*: ``acreate_superuser()``
  229. Same as :meth:`create_user`, but sets :attr:`~models.User.is_staff` and
  230. :attr:`~models.User.is_superuser` to ``True``.
  231. .. versionchanged:: 5.2
  232. ``acreate_superuser()`` method was added.
  233. .. method:: with_perm(perm, is_active=True, include_superusers=True, backend=None, obj=None)
  234. Returns users that have the given permission ``perm`` either in the
  235. ``"<app label>.<permission codename>"`` format or as a
  236. :class:`~django.contrib.auth.models.Permission` instance. Returns an
  237. empty queryset if no users who have the ``perm`` found.
  238. If ``is_active`` is ``True`` (default), returns only active users, or
  239. if ``False``, returns only inactive users. Use ``None`` to return all
  240. users irrespective of active state.
  241. If ``include_superusers`` is ``True`` (default), the result will
  242. include superusers.
  243. If ``backend`` is passed in and it's defined in
  244. :setting:`AUTHENTICATION_BACKENDS`, then this method will use it.
  245. Otherwise, it will use the ``backend`` in
  246. :setting:`AUTHENTICATION_BACKENDS`, if there is only one, or raise an
  247. exception.
  248. ``AnonymousUser`` object
  249. ========================
  250. .. class:: models.AnonymousUser
  251. :class:`django.contrib.auth.models.AnonymousUser` is a class that
  252. implements the :class:`django.contrib.auth.models.User` interface, with
  253. these differences:
  254. * :ref:`id <automatic-primary-key-fields>` is always ``None``.
  255. * :attr:`~django.contrib.auth.models.User.username` is always the empty
  256. string.
  257. * :meth:`~django.contrib.auth.models.User.get_username()` always returns
  258. the empty string.
  259. * :attr:`~django.contrib.auth.models.User.is_anonymous` is ``True``
  260. instead of ``False``.
  261. * :attr:`~django.contrib.auth.models.User.is_authenticated` is
  262. ``False`` instead of ``True``.
  263. * :attr:`~django.contrib.auth.models.User.is_staff` and
  264. :attr:`~django.contrib.auth.models.User.is_superuser` are always
  265. ``False``.
  266. * :attr:`~django.contrib.auth.models.User.is_active` is always ``False``.
  267. * :attr:`~django.contrib.auth.models.User.groups` and
  268. :attr:`~django.contrib.auth.models.User.user_permissions` are always
  269. empty.
  270. * :meth:`~django.contrib.auth.models.User.set_password()`,
  271. :meth:`~django.contrib.auth.models.User.check_password()`,
  272. :meth:`~django.db.models.Model.save` and
  273. :meth:`~django.db.models.Model.delete()` raise :exc:`NotImplementedError`.
  274. In practice, you probably won't need to use
  275. :class:`~django.contrib.auth.models.AnonymousUser` objects on your own, but
  276. they're used by web requests, as explained in the next section.
  277. ``Permission`` model
  278. ====================
  279. .. class:: models.Permission
  280. Fields
  281. ------
  282. :class:`~django.contrib.auth.models.Permission` objects have the following
  283. fields:
  284. .. class:: models.Permission
  285. :noindex:
  286. .. attribute:: name
  287. Required. 255 characters or fewer. Example: ``'Can vote'``.
  288. .. attribute:: content_type
  289. Required. A reference to the ``django_content_type`` database table,
  290. which contains a record for each installed model.
  291. .. attribute:: codename
  292. Required. 100 characters or fewer. Example: ``'can_vote'``.
  293. Methods
  294. -------
  295. :class:`~django.contrib.auth.models.Permission` objects have the standard
  296. data-access methods like any other :doc:`Django model </ref/models/instances>`.
  297. ``Group`` model
  298. ===============
  299. .. class:: models.Group
  300. Fields
  301. ------
  302. :class:`~django.contrib.auth.models.Group` objects have the following fields:
  303. .. class:: models.Group
  304. :noindex:
  305. .. attribute:: name
  306. Required. 150 characters or fewer. Any characters are permitted.
  307. Example: ``'Awesome Users'``.
  308. .. attribute:: permissions
  309. Many-to-many field to :class:`~django.contrib.auth.models.Permission`::
  310. group.permissions.set([permission_list])
  311. group.permissions.add(permission, permission, ...)
  312. group.permissions.remove(permission, permission, ...)
  313. group.permissions.clear()
  314. Validators
  315. ==========
  316. .. class:: validators.ASCIIUsernameValidator
  317. A field validator allowing only ASCII letters and numbers, in addition to
  318. ``@``, ``.``, ``+``, ``-``, and ``_``.
  319. .. class:: validators.UnicodeUsernameValidator
  320. A field validator allowing Unicode characters, in addition to ``@``, ``.``,
  321. ``+``, ``-``, and ``_``. The default validator for ``User.username``.
  322. .. _topics-auth-signals:
  323. Login and logout signals
  324. ========================
  325. .. module:: django.contrib.auth.signals
  326. The auth framework uses the following :doc:`signals </topics/signals>` that
  327. can be used for notification when a user logs in or out.
  328. .. data:: user_logged_in
  329. Sent when a user logs in successfully.
  330. Arguments sent with this signal:
  331. ``sender``
  332. The class of the user that just logged in.
  333. ``request``
  334. The current :class:`~django.http.HttpRequest` instance.
  335. ``user``
  336. The user instance that just logged in.
  337. .. data:: user_logged_out
  338. Sent when the logout method is called.
  339. ``sender``
  340. As above: the class of the user that just logged out or ``None``
  341. if the user was not authenticated.
  342. ``request``
  343. The current :class:`~django.http.HttpRequest` instance.
  344. ``user``
  345. The user instance that just logged out or ``None`` if the
  346. user was not authenticated.
  347. .. data:: user_login_failed
  348. Sent when the user failed to login successfully
  349. ``sender``
  350. The name of the module used for authentication.
  351. ``credentials``
  352. A dictionary of keyword arguments containing the user credentials that were
  353. passed to :func:`~django.contrib.auth.authenticate()` or your own custom
  354. authentication backend. Credentials matching a set of 'sensitive' patterns,
  355. (including password) will not be sent in the clear as part of the signal.
  356. ``request``
  357. The :class:`~django.http.HttpRequest` object, if one was provided to
  358. :func:`~django.contrib.auth.authenticate`.
  359. .. _authentication-backends-reference:
  360. Authentication backends
  361. =======================
  362. .. module:: django.contrib.auth.backends
  363. :synopsis: Django's built-in authentication backend classes.
  364. This section details the authentication backends that come with Django. For
  365. information on how to use them and how to write your own authentication
  366. backends, see the :ref:`Other authentication sources section
  367. <authentication-backends>` of the :doc:`User authentication guide
  368. </topics/auth/index>`.
  369. Available authentication backends
  370. ---------------------------------
  371. The following backends are available in :mod:`django.contrib.auth.backends`:
  372. .. class:: BaseBackend
  373. A base class that provides default implementations for all required
  374. methods. By default, it will reject any user and provide no permissions.
  375. .. method:: get_user_permissions(user_obj, obj=None)
  376. .. method:: aget_user_permissions(user_obj, obj=None)
  377. *Asynchronous version*: ``aget_user_permissions()``
  378. Returns an empty set.
  379. .. versionchanged:: 5.2
  380. ``aget_user_permissions()`` function was added.
  381. .. method:: get_group_permissions(user_obj, obj=None)
  382. .. method:: aget_group_permissions(user_obj, obj=None)
  383. *Asynchronous version*: ``aget_group_permissions()``
  384. Returns an empty set.
  385. .. versionchanged:: 5.2
  386. ``aget_group_permissions()`` function was added.
  387. .. method:: get_all_permissions(user_obj, obj=None)
  388. .. method:: aget_all_permissions(user_obj, obj=None)
  389. *Asynchronous version*: ``aget_all_permissions()``
  390. Uses :meth:`get_user_permissions` and :meth:`get_group_permissions` to
  391. get the set of permission strings the ``user_obj`` has.
  392. .. versionchanged:: 5.2
  393. ``aget_all_permissions()`` function was added.
  394. .. method:: has_perm(user_obj, perm, obj=None)
  395. .. method:: ahas_perm(user_obj, perm, obj=None)
  396. *Asynchronous version*: ``ahas_perm()``
  397. Uses :meth:`get_all_permissions` to check if ``user_obj`` has the
  398. permission string ``perm``.
  399. .. versionchanged:: 5.2
  400. ``ahas_perm()`` function was added.
  401. .. class:: ModelBackend
  402. This is the default authentication backend used by Django. It
  403. authenticates using credentials consisting of a user identifier and
  404. password. For Django's default user model, the user identifier is the
  405. username, for custom user models it is the field specified by
  406. USERNAME_FIELD (see :doc:`Customizing Users and authentication
  407. </topics/auth/customizing>`).
  408. It also handles the default permissions model as defined for
  409. :class:`~django.contrib.auth.models.User` and
  410. :class:`~django.contrib.auth.models.PermissionsMixin`.
  411. :meth:`has_perm`, :meth:`get_all_permissions`, :meth:`get_user_permissions`,
  412. and :meth:`get_group_permissions` allow an object to be passed as a
  413. parameter for object-specific permissions, but this backend does not
  414. implement them other than returning an empty set of permissions if
  415. ``obj is not None``.
  416. :meth:`with_perm` also allows an object to be passed as a parameter, but
  417. unlike others methods it returns an empty queryset if ``obj is not None``.
  418. .. method:: authenticate(request, username=None, password=None, **kwargs)
  419. .. method:: aauthenticate(request, username=None, password=None, **kwargs)
  420. *Asynchronous version*: ``aauthenticate()``
  421. Tries to authenticate ``username`` with ``password`` by calling
  422. :meth:`User.check_password
  423. <django.contrib.auth.models.User.check_password>`. If no ``username``
  424. is provided, it tries to fetch a username from ``kwargs`` using the
  425. key :attr:`CustomUser.USERNAME_FIELD
  426. <django.contrib.auth.models.CustomUser.USERNAME_FIELD>`. Returns an
  427. authenticated user or ``None``.
  428. ``request`` is an :class:`~django.http.HttpRequest` and may be ``None``
  429. if it wasn't provided to :func:`~django.contrib.auth.authenticate`
  430. (which passes it on to the backend).
  431. .. versionchanged:: 5.2
  432. ``aauthenticate()`` function was added.
  433. .. method:: get_user_permissions(user_obj, obj=None)
  434. .. method:: aget_user_permissions(user_obj, obj=None)
  435. *Asynchronous version*: ``aget_user_permissions()``
  436. Returns the set of permission strings the ``user_obj`` has from their
  437. own user permissions. Returns an empty set if
  438. :attr:`~django.contrib.auth.models.AbstractBaseUser.is_anonymous` or
  439. :attr:`~django.contrib.auth.models.CustomUser.is_active` is ``False``.
  440. .. versionchanged:: 5.2
  441. ``aget_user_permissions()`` function was added.
  442. .. method:: get_group_permissions(user_obj, obj=None)
  443. .. method:: aget_group_permissions(user_obj, obj=None)
  444. *Asynchronous version*: ``aget_group_permissions()``
  445. Returns the set of permission strings the ``user_obj`` has from the
  446. permissions of the groups they belong. Returns an empty set if
  447. :attr:`~django.contrib.auth.models.AbstractBaseUser.is_anonymous` or
  448. :attr:`~django.contrib.auth.models.CustomUser.is_active` is ``False``.
  449. .. versionchanged:: 5.2
  450. ``aget_group_permissions()`` function was added.
  451. .. method:: get_all_permissions(user_obj, obj=None)
  452. .. method:: aget_all_permissions(user_obj, obj=None)
  453. *Asynchronous version*: ``aget_all_permissions()``
  454. Returns the set of permission strings the ``user_obj`` has, including both
  455. user permissions and group permissions. Returns an empty set if
  456. :attr:`~django.contrib.auth.models.AbstractBaseUser.is_anonymous` or
  457. :attr:`~django.contrib.auth.models.CustomUser.is_active` is ``False``.
  458. .. versionchanged:: 5.2
  459. ``aget_all_permissions()`` function was added.
  460. .. method:: has_perm(user_obj, perm, obj=None)
  461. .. method:: ahas_perm(user_obj, perm, obj=None)
  462. *Asynchronous version*: ``ahas_perm()``
  463. Uses :meth:`get_all_permissions` to check if ``user_obj`` has the
  464. permission string ``perm``. Returns ``False`` if the user is not
  465. :attr:`~django.contrib.auth.models.CustomUser.is_active`.
  466. .. versionchanged:: 5.2
  467. ``ahas_perm()`` function was added.
  468. .. method:: has_module_perms(user_obj, app_label)
  469. .. method:: ahas_module_perms(user_obj, app_label)
  470. *Asynchronous version*: ``ahas_module_perms()``
  471. Returns whether the ``user_obj`` has any permissions on the app
  472. ``app_label``.
  473. .. versionchanged:: 5.2
  474. ``ahas_module_perms()`` function was added.
  475. .. method:: user_can_authenticate()
  476. Returns whether the user is allowed to authenticate. To match the
  477. behavior of :class:`~django.contrib.auth.forms.AuthenticationForm`
  478. which :meth:`prohibits inactive users from logging in
  479. <django.contrib.auth.forms.AuthenticationForm.confirm_login_allowed>`,
  480. this method returns ``False`` for users with :attr:`is_active=False
  481. <django.contrib.auth.models.User.is_active>`. Custom user models that
  482. don't have an :attr:`~django.contrib.auth.models.CustomUser.is_active`
  483. field are allowed.
  484. .. method:: with_perm(perm, is_active=True, include_superusers=True, obj=None)
  485. Returns all active users who have the permission ``perm`` either in
  486. the form of ``"<app label>.<permission codename>"`` or a
  487. :class:`~django.contrib.auth.models.Permission` instance. Returns an
  488. empty queryset if no users who have the ``perm`` found.
  489. If ``is_active`` is ``True`` (default), returns only active users, or
  490. if ``False``, returns only inactive users. Use ``None`` to return all
  491. users irrespective of active state.
  492. If ``include_superusers`` is ``True`` (default), the result will
  493. include superusers.
  494. .. class:: AllowAllUsersModelBackend
  495. Same as :class:`ModelBackend` except that it doesn't reject inactive users
  496. because :meth:`~ModelBackend.user_can_authenticate` always returns ``True``.
  497. When using this backend, you'll likely want to customize the
  498. :class:`~django.contrib.auth.forms.AuthenticationForm` used by the
  499. :class:`~django.contrib.auth.views.LoginView` by overriding the
  500. :meth:`~django.contrib.auth.forms.AuthenticationForm.confirm_login_allowed`
  501. method as it rejects inactive users.
  502. .. class:: RemoteUserBackend
  503. Use this backend to take advantage of external-to-Django-handled
  504. authentication. It authenticates using usernames passed in
  505. :attr:`request.META['REMOTE_USER'] <django.http.HttpRequest.META>`. See
  506. the :doc:`Authenticating against REMOTE_USER </howto/auth-remote-user>`
  507. documentation.
  508. If you need more control, you can create your own authentication backend
  509. that inherits from this class and override these attributes or methods:
  510. .. attribute:: create_unknown_user
  511. ``True`` or ``False``. Determines whether or not a user object is
  512. created if not already in the database Defaults to ``True``.
  513. .. method:: authenticate(request, remote_user)
  514. .. method:: aauthenticate(request, remote_user)
  515. *Asynchronous version*: ``aauthenticate()``
  516. The username passed as ``remote_user`` is considered trusted. This
  517. method returns the user object with the given username, creating a new
  518. user object if :attr:`~RemoteUserBackend.create_unknown_user` is
  519. ``True``.
  520. Returns ``None`` if :attr:`~RemoteUserBackend.create_unknown_user` is
  521. ``False`` and a ``User`` object with the given username is not found in
  522. the database.
  523. ``request`` is an :class:`~django.http.HttpRequest` and may be ``None``
  524. if it wasn't provided to :func:`~django.contrib.auth.authenticate`
  525. (which passes it on to the backend).
  526. .. versionchanged:: 5.2
  527. ``aauthenticate()`` function was added.
  528. .. method:: clean_username(username)
  529. Performs any cleaning on the ``username`` (e.g. stripping LDAP DN
  530. information) prior to using it to get or create a user object. Returns
  531. the cleaned username.
  532. .. method:: configure_user(request, user, created=True)
  533. .. method:: aconfigure_user(request, user, created=True)
  534. *Asynchronous version*: ``aconfigure_user()``
  535. Configures the user on each authentication attempt. This method is
  536. called immediately after fetching or creating the user being
  537. authenticated, and can be used to perform custom setup actions, such as
  538. setting the user's groups based on attributes in an LDAP directory.
  539. Returns the user object. When fetching or creating an user is called
  540. from a synchronous context, ``configure_user`` is called,
  541. ``aconfigure_user`` is called from async contexts.
  542. The setup can be performed either once when the user is created
  543. (``created`` is ``True``) or on existing users (``created`` is
  544. ``False``) as a way of synchronizing attributes between the remote and
  545. the local systems.
  546. ``request`` is an :class:`~django.http.HttpRequest` and may be ``None``
  547. if it wasn't provided to :func:`~django.contrib.auth.authenticate`
  548. (which passes it on to the backend).
  549. .. versionchanged:: 5.2
  550. ``aconfigure_user()`` function was added.
  551. .. method:: user_can_authenticate()
  552. Returns whether the user is allowed to authenticate. This method
  553. returns ``False`` for users with :attr:`is_active=False
  554. <django.contrib.auth.models.User.is_active>`. Custom user models that
  555. don't have an :attr:`~django.contrib.auth.models.CustomUser.is_active`
  556. field are allowed.
  557. .. class:: AllowAllUsersRemoteUserBackend
  558. Same as :class:`RemoteUserBackend` except that it doesn't reject inactive
  559. users because :attr:`~RemoteUserBackend.user_can_authenticate` always
  560. returns ``True``.
  561. Utility functions
  562. =================
  563. .. currentmodule:: django.contrib.auth
  564. .. function:: get_user(request)
  565. .. function:: aget_user(request)
  566. *Asynchronous version*: ``aget_user()``
  567. Returns the user model instance associated with the given ``request``’s
  568. session.
  569. It checks if the authentication backend stored in the session is present in
  570. :setting:`AUTHENTICATION_BACKENDS`. If so, it uses the backend's
  571. ``get_user()`` method to retrieve the user model instance and then verifies
  572. the session by calling the user model's
  573. :meth:`~django.contrib.auth.models.AbstractBaseUser.get_session_auth_hash`
  574. method. If the verification fails and :setting:`SECRET_KEY_FALLBACKS` are
  575. provided, it verifies the session against each fallback key using
  576. :meth:`~django.contrib.auth.models.AbstractBaseUser.\
  577. get_session_auth_fallback_hash`.
  578. Returns an instance of :class:`~django.contrib.auth.models.AnonymousUser`
  579. if the authentication backend stored in the session is no longer in
  580. :setting:`AUTHENTICATION_BACKENDS`, if a user isn't returned by the
  581. backend's ``get_user()`` method, or if the session auth hash doesn't
  582. validate.