auth.txt 26 KB

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