You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

345 lines
12 KiB

  1. import functools
  2. import warnings
  3. from django.conf import settings
  4. # Avoid shadowing the login() and logout() views below.
  5. from django.contrib.auth import (
  6. REDIRECT_FIELD_NAME, get_user_model, login as auth_login,
  7. logout as auth_logout, update_session_auth_hash,
  8. )
  9. from django.contrib.auth.decorators import login_required
  10. from django.contrib.auth.forms import (
  11. AuthenticationForm, PasswordChangeForm, PasswordResetForm, SetPasswordForm,
  12. )
  13. from django.contrib.auth.tokens import default_token_generator
  14. from django.contrib.sites.shortcuts import get_current_site
  15. from django.core.urlresolvers import reverse
  16. from django.http import HttpResponseRedirect, QueryDict
  17. from django.shortcuts import resolve_url
  18. from django.template.response import TemplateResponse
  19. from django.utils.deprecation import (
  20. RemovedInDjango20Warning, RemovedInDjango110Warning,
  21. )
  22. from django.utils.encoding import force_text
  23. from django.utils.http import is_safe_url, urlsafe_base64_decode
  24. from django.utils.six.moves.urllib.parse import urlparse, urlunparse
  25. from django.utils.translation import ugettext as _
  26. from django.views.decorators.cache import never_cache
  27. from django.views.decorators.csrf import csrf_protect
  28. from django.views.decorators.debug import sensitive_post_parameters
  29. def deprecate_current_app(func):
  30. """
  31. Handle deprecation of the current_app parameter of the views.
  32. """
  33. @functools.wraps(func)
  34. def inner(*args, **kwargs):
  35. if 'current_app' in kwargs:
  36. warnings.warn(
  37. "Passing `current_app` as a keyword argument is deprecated. "
  38. "Instead the caller of `{0}` should set "
  39. "`request.current_app`.".format(func.__name__),
  40. RemovedInDjango20Warning
  41. )
  42. current_app = kwargs.pop('current_app')
  43. request = kwargs.get('request', None)
  44. if request and current_app is not None:
  45. request.current_app = current_app
  46. return func(*args, **kwargs)
  47. return inner
  48. @deprecate_current_app
  49. @sensitive_post_parameters()
  50. @csrf_protect
  51. @never_cache
  52. def login(request, template_name='registration/login.html',
  53. redirect_field_name=REDIRECT_FIELD_NAME,
  54. authentication_form=AuthenticationForm,
  55. extra_context=None):
  56. """
  57. Displays the login form and handles the login action.
  58. """
  59. redirect_to = request.POST.get(redirect_field_name,
  60. request.GET.get(redirect_field_name, ''))
  61. if request.method == "POST":
  62. form = authentication_form(request, data=request.POST)
  63. if form.is_valid():
  64. # Ensure the user-originating redirection url is safe.
  65. if not is_safe_url(url=redirect_to, host=request.get_host()):
  66. redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)
  67. # Okay, security check complete. Log the user in.
  68. auth_login(request, form.get_user())
  69. return HttpResponseRedirect(redirect_to)
  70. else:
  71. form = authentication_form(request)
  72. current_site = get_current_site(request)
  73. context = {
  74. 'form': form,
  75. redirect_field_name: redirect_to,
  76. 'site': current_site,
  77. 'site_name': current_site.name,
  78. }
  79. if extra_context is not None:
  80. context.update(extra_context)
  81. return TemplateResponse(request, template_name, context)
  82. @deprecate_current_app
  83. def logout(request, next_page=None,
  84. template_name='registration/logged_out.html',
  85. redirect_field_name=REDIRECT_FIELD_NAME,
  86. extra_context=None):
  87. """
  88. Logs out the user and displays 'You are logged out' message.
  89. """
  90. auth_logout(request)
  91. if next_page is not None:
  92. next_page = resolve_url(next_page)
  93. if (redirect_field_name in request.POST or
  94. redirect_field_name in request.GET):
  95. next_page = request.POST.get(redirect_field_name,
  96. request.GET.get(redirect_field_name))
  97. # Security check -- don't allow redirection to a different host.
  98. if not is_safe_url(url=next_page, host=request.get_host()):
  99. next_page = request.path
  100. if next_page:
  101. # Redirect to this page until the session has been cleared.
  102. return HttpResponseRedirect(next_page)
  103. current_site = get_current_site(request)
  104. context = {
  105. 'site': current_site,
  106. 'site_name': current_site.name,
  107. 'title': _('Logged out')
  108. }
  109. if extra_context is not None:
  110. context.update(extra_context)
  111. return TemplateResponse(request, template_name, context)
  112. @deprecate_current_app
  113. def logout_then_login(request, login_url=None, extra_context=None):
  114. """
  115. Logs out the user if they are logged in. Then redirects to the log-in page.
  116. """
  117. if not login_url:
  118. login_url = settings.LOGIN_URL
  119. login_url = resolve_url(login_url)
  120. return logout(request, login_url, extra_context=extra_context)
  121. def redirect_to_login(next, login_url=None,
  122. redirect_field_name=REDIRECT_FIELD_NAME):
  123. """
  124. Redirects the user to the login page, passing the given 'next' page
  125. """
  126. resolved_url = resolve_url(login_url or settings.LOGIN_URL)
  127. login_url_parts = list(urlparse(resolved_url))
  128. if redirect_field_name:
  129. querystring = QueryDict(login_url_parts[4], mutable=True)
  130. querystring[redirect_field_name] = next
  131. login_url_parts[4] = querystring.urlencode(safe='/')
  132. return HttpResponseRedirect(urlunparse(login_url_parts))
  133. # 4 views for password reset:
  134. # - password_reset sends the mail
  135. # - password_reset_done shows a success message for the above
  136. # - password_reset_confirm checks the link the user clicked and
  137. # prompts for a new password
  138. # - password_reset_complete shows a success message for the above
  139. @deprecate_current_app
  140. @csrf_protect
  141. def password_reset(request, is_admin_site=False,
  142. template_name='registration/password_reset_form.html',
  143. email_template_name='registration/password_reset_email.html',
  144. subject_template_name='registration/password_reset_subject.txt',
  145. password_reset_form=PasswordResetForm,
  146. token_generator=default_token_generator,
  147. post_reset_redirect=None,
  148. from_email=None,
  149. extra_context=None,
  150. html_email_template_name=None,
  151. extra_email_context=None):
  152. if post_reset_redirect is None:
  153. post_reset_redirect = reverse('password_reset_done')
  154. else:
  155. post_reset_redirect = resolve_url(post_reset_redirect)
  156. if request.method == "POST":
  157. form = password_reset_form(request.POST)
  158. if form.is_valid():
  159. opts = {
  160. 'use_https': request.is_secure(),
  161. 'token_generator': token_generator,
  162. 'from_email': from_email,
  163. 'email_template_name': email_template_name,
  164. 'subject_template_name': subject_template_name,
  165. 'request': request,
  166. 'html_email_template_name': html_email_template_name,
  167. 'extra_email_context': extra_email_context,
  168. }
  169. if is_admin_site:
  170. warnings.warn(
  171. "The is_admin_site argument to "
  172. "django.contrib.auth.views.password_reset() is deprecated "
  173. "and will be removed in Django 1.10.",
  174. RemovedInDjango110Warning, 3
  175. )
  176. opts = dict(opts, domain_override=request.get_host())
  177. form.save(**opts)
  178. return HttpResponseRedirect(post_reset_redirect)
  179. else:
  180. form = password_reset_form()
  181. context = {
  182. 'form': form,
  183. 'title': _('Password reset'),
  184. }
  185. if extra_context is not None:
  186. context.update(extra_context)
  187. return TemplateResponse(request, template_name, context)
  188. @deprecate_current_app
  189. def password_reset_done(request,
  190. template_name='registration/password_reset_done.html',
  191. extra_context=None):
  192. context = {
  193. 'title': _('Password reset sent'),
  194. }
  195. if extra_context is not None:
  196. context.update(extra_context)
  197. return TemplateResponse(request, template_name, context)
  198. # Doesn't need csrf_protect since no-one can guess the URL
  199. @sensitive_post_parameters()
  200. @never_cache
  201. @deprecate_current_app
  202. def password_reset_confirm(request, uidb64=None, token=None,
  203. template_name='registration/password_reset_confirm.html',
  204. token_generator=default_token_generator,
  205. set_password_form=SetPasswordForm,
  206. post_reset_redirect=None,
  207. extra_context=None):
  208. """
  209. View that checks the hash in a password reset link and presents a
  210. form for entering a new password.
  211. """
  212. UserModel = get_user_model()
  213. assert uidb64 is not None and token is not None # checked by URLconf
  214. if post_reset_redirect is None:
  215. post_reset_redirect = reverse('password_reset_complete')
  216. else:
  217. post_reset_redirect = resolve_url(post_reset_redirect)
  218. try:
  219. # urlsafe_base64_decode() decodes to bytestring on Python 3
  220. uid = force_text(urlsafe_base64_decode(uidb64))
  221. user = UserModel._default_manager.get(pk=uid)
  222. except (TypeError, ValueError, OverflowError, UserModel.DoesNotExist):
  223. user = None
  224. if user is not None and token_generator.check_token(user, token):
  225. validlink = True
  226. title = _('Enter new password')
  227. if request.method == 'POST':
  228. form = set_password_form(user, request.POST)
  229. if form.is_valid():
  230. form.save()
  231. return HttpResponseRedirect(post_reset_redirect)
  232. else:
  233. form = set_password_form(user)
  234. else:
  235. validlink = False
  236. form = None
  237. title = _('Password reset unsuccessful')
  238. context = {
  239. 'form': form,
  240. 'title': title,
  241. 'validlink': validlink,
  242. }
  243. if extra_context is not None:
  244. context.update(extra_context)
  245. return TemplateResponse(request, template_name, context)
  246. @deprecate_current_app
  247. def password_reset_complete(request,
  248. template_name='registration/password_reset_complete.html',
  249. extra_context=None):
  250. context = {
  251. 'login_url': resolve_url(settings.LOGIN_URL),
  252. 'title': _('Password reset complete'),
  253. }
  254. if extra_context is not None:
  255. context.update(extra_context)
  256. return TemplateResponse(request, template_name, context)
  257. @sensitive_post_parameters()
  258. @csrf_protect
  259. @login_required
  260. @deprecate_current_app
  261. def password_change(request,
  262. template_name='registration/password_change_form.html',
  263. post_change_redirect=None,
  264. password_change_form=PasswordChangeForm,
  265. extra_context=None):
  266. if post_change_redirect is None:
  267. post_change_redirect = reverse('password_change_done')
  268. else:
  269. post_change_redirect = resolve_url(post_change_redirect)
  270. if request.method == "POST":
  271. form = password_change_form(user=request.user, data=request.POST)
  272. if form.is_valid():
  273. form.save()
  274. # Updating the password logs out all other sessions for the user
  275. # except the current one if
  276. # django.contrib.auth.middleware.SessionAuthenticationMiddleware
  277. # is enabled.
  278. update_session_auth_hash(request, form.user)
  279. return HttpResponseRedirect(post_change_redirect)
  280. else:
  281. form = password_change_form(user=request.user)
  282. context = {
  283. 'form': form,
  284. 'title': _('Password change'),
  285. }
  286. if extra_context is not None:
  287. context.update(extra_context)
  288. return TemplateResponse(request, template_name, context)
  289. @login_required
  290. @deprecate_current_app
  291. def password_change_done(request,
  292. template_name='registration/password_change_done.html',
  293. extra_context=None):
  294. context = {
  295. 'title': _('Password change successful'),
  296. }
  297. if extra_context is not None:
  298. context.update(extra_context)
  299. return TemplateResponse(request, template_name, context)