auth.txt 26 KB

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