auth.txt 27 KB

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