PageRenderTime 211ms CodeModel.GetById 27ms RepoModel.GetById 2ms app.codeStats 0ms

/python/lib/django-1.2/django/contrib/auth/views.py

https://gitlab.com/gregtyka/frankenserver
Python | 188 lines | 170 code | 11 blank | 7 comment | 7 complexity | af8e3d3d3ebebc64b87955c6f6138f98 MD5 | raw file
  1. import re
  2. from django.conf import settings
  3. from django.contrib.auth import REDIRECT_FIELD_NAME
  4. # Avoid shadowing the login() view below.
  5. from django.contrib.auth import login as auth_login
  6. from django.contrib.auth.decorators import login_required
  7. from django.contrib.auth.forms import AuthenticationForm
  8. from django.contrib.auth.forms import PasswordResetForm, SetPasswordForm, PasswordChangeForm
  9. from django.contrib.auth.tokens import default_token_generator
  10. from django.views.decorators.csrf import csrf_protect
  11. from django.core.urlresolvers import reverse
  12. from django.shortcuts import render_to_response, get_object_or_404
  13. from django.contrib.sites.models import get_current_site
  14. from django.http import HttpResponseRedirect, Http404
  15. from django.template import RequestContext
  16. from django.utils.http import urlquote, base36_to_int
  17. from django.utils.translation import ugettext as _
  18. from django.contrib.auth.models import User
  19. from django.views.decorators.cache import never_cache
  20. @csrf_protect
  21. @never_cache
  22. def login(request, template_name='registration/login.html',
  23. redirect_field_name=REDIRECT_FIELD_NAME,
  24. authentication_form=AuthenticationForm):
  25. """Displays the login form and handles the login action."""
  26. redirect_to = request.REQUEST.get(redirect_field_name, '')
  27. if request.method == "POST":
  28. form = authentication_form(data=request.POST)
  29. if form.is_valid():
  30. # Light security check -- make sure redirect_to isn't garbage.
  31. if not redirect_to or ' ' in redirect_to:
  32. redirect_to = settings.LOGIN_REDIRECT_URL
  33. # Heavier security check -- redirects to http://example.com should
  34. # not be allowed, but things like /view/?param=http://example.com
  35. # should be allowed. This regex checks if there is a '//' *before* a
  36. # question mark.
  37. elif '//' in redirect_to and re.match(r'[^\?]*//', redirect_to):
  38. redirect_to = settings.LOGIN_REDIRECT_URL
  39. # Okay, security checks complete. Log the user in.
  40. auth_login(request, form.get_user())
  41. if request.session.test_cookie_worked():
  42. request.session.delete_test_cookie()
  43. return HttpResponseRedirect(redirect_to)
  44. else:
  45. form = authentication_form(request)
  46. request.session.set_test_cookie()
  47. current_site = get_current_site(request)
  48. return render_to_response(template_name, {
  49. 'form': form,
  50. redirect_field_name: redirect_to,
  51. 'site': current_site,
  52. 'site_name': current_site.name,
  53. }, context_instance=RequestContext(request))
  54. def logout(request, next_page=None, template_name='registration/logged_out.html', redirect_field_name=REDIRECT_FIELD_NAME):
  55. "Logs out the user and displays 'You are logged out' message."
  56. from django.contrib.auth import logout
  57. logout(request)
  58. if next_page is None:
  59. redirect_to = request.REQUEST.get(redirect_field_name, '')
  60. if redirect_to:
  61. return HttpResponseRedirect(redirect_to)
  62. else:
  63. current_site = get_current_site(request)
  64. return render_to_response(template_name, {
  65. 'site': current_site,
  66. 'site_name': current_site.name,
  67. 'title': _('Logged out')
  68. }, context_instance=RequestContext(request))
  69. else:
  70. # Redirect to this page until the session has been cleared.
  71. return HttpResponseRedirect(next_page or request.path)
  72. def logout_then_login(request, login_url=None):
  73. "Logs out the user if he is logged in. Then redirects to the log-in page."
  74. if not login_url:
  75. login_url = settings.LOGIN_URL
  76. return logout(request, login_url)
  77. def redirect_to_login(next, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
  78. "Redirects the user to the login page, passing the given 'next' page"
  79. if not login_url:
  80. login_url = settings.LOGIN_URL
  81. return HttpResponseRedirect('%s?%s=%s' % (login_url, urlquote(redirect_field_name), urlquote(next)))
  82. # 4 views for password reset:
  83. # - password_reset sends the mail
  84. # - password_reset_done shows a success message for the above
  85. # - password_reset_confirm checks the link the user clicked and
  86. # prompts for a new password
  87. # - password_reset_complete shows a success message for the above
  88. @csrf_protect
  89. def password_reset(request, is_admin_site=False, template_name='registration/password_reset_form.html',
  90. email_template_name='registration/password_reset_email.html',
  91. password_reset_form=PasswordResetForm, token_generator=default_token_generator,
  92. post_reset_redirect=None):
  93. if post_reset_redirect is None:
  94. post_reset_redirect = reverse('django.contrib.auth.views.password_reset_done')
  95. if request.method == "POST":
  96. form = password_reset_form(request.POST)
  97. if form.is_valid():
  98. opts = {}
  99. opts['use_https'] = request.is_secure()
  100. opts['token_generator'] = token_generator
  101. opts['email_template_name'] = email_template_name
  102. opts['request'] = request
  103. if is_admin_site:
  104. opts['domain_override'] = request.META['HTTP_HOST']
  105. form.save(**opts)
  106. return HttpResponseRedirect(post_reset_redirect)
  107. else:
  108. form = password_reset_form()
  109. return render_to_response(template_name, {
  110. 'form': form,
  111. }, context_instance=RequestContext(request))
  112. def password_reset_done(request, template_name='registration/password_reset_done.html'):
  113. return render_to_response(template_name, context_instance=RequestContext(request))
  114. @never_cache
  115. def password_reset_confirm(request, uidb36=None, token=None, template_name='registration/password_reset_confirm.html',
  116. token_generator=default_token_generator, set_password_form=SetPasswordForm,
  117. post_reset_redirect=None):
  118. """
  119. View that checks the hash in a password reset link and presents a
  120. form for entering a new password.
  121. """
  122. assert uidb36 is not None and token is not None # checked by URLconf
  123. if post_reset_redirect is None:
  124. post_reset_redirect = reverse('django.contrib.auth.views.password_reset_complete')
  125. try:
  126. uid_int = base36_to_int(uidb36)
  127. user = User.objects.get(id=uid_int)
  128. except (ValueError, User.DoesNotExist):
  129. user = None
  130. context_instance = RequestContext(request)
  131. if user is not None and token_generator.check_token(user, token):
  132. context_instance['validlink'] = True
  133. if request.method == 'POST':
  134. form = set_password_form(user, request.POST)
  135. if form.is_valid():
  136. form.save()
  137. return HttpResponseRedirect(post_reset_redirect)
  138. else:
  139. form = set_password_form(None)
  140. else:
  141. context_instance['validlink'] = False
  142. form = None
  143. context_instance['form'] = form
  144. return render_to_response(template_name, context_instance=context_instance)
  145. def password_reset_complete(request, template_name='registration/password_reset_complete.html'):
  146. return render_to_response(template_name, context_instance=RequestContext(request,
  147. {'login_url': settings.LOGIN_URL}))
  148. @csrf_protect
  149. @login_required
  150. def password_change(request, template_name='registration/password_change_form.html',
  151. post_change_redirect=None, password_change_form=PasswordChangeForm):
  152. if post_change_redirect is None:
  153. post_change_redirect = reverse('django.contrib.auth.views.password_change_done')
  154. if request.method == "POST":
  155. form = password_change_form(user=request.user, data=request.POST)
  156. if form.is_valid():
  157. form.save()
  158. return HttpResponseRedirect(post_change_redirect)
  159. else:
  160. form = password_change_form(user=request.user)
  161. return render_to_response(template_name, {
  162. 'form': form,
  163. }, context_instance=RequestContext(request))
  164. def password_change_done(request, template_name='registration/password_change_done.html'):
  165. return render_to_response(template_name, context_instance=RequestContext(request))