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.

182 lines
5.3 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. import uuid
  2. from django.contrib import messages
  3. from django.contrib.auth import login as auth_login
  4. from django.contrib.auth import logout as auth_logout
  5. from django.contrib.auth import authenticate
  6. from django.contrib.auth.decorators import login_required
  7. from django.contrib.auth.hashers import check_password
  8. from django.contrib.auth.models import User
  9. from django.core import signing
  10. from django.http import HttpResponseBadRequest
  11. from django.shortcuts import redirect, render
  12. from hc.accounts.forms import (EmailPasswordForm, ReportSettingsForm,
  13. SetPasswordForm)
  14. from hc.accounts.models import Profile
  15. from hc.api.models import Channel, Check
  16. def _make_user(email):
  17. username = str(uuid.uuid4())[:30]
  18. user = User(username=username, email=email)
  19. user.set_unusable_password()
  20. user.save()
  21. channel = Channel()
  22. channel.user = user
  23. channel.kind = "email"
  24. channel.value = email
  25. channel.email_verified = True
  26. channel.save()
  27. return user
  28. def _associate_demo_check(request, user):
  29. if "welcome_code" in request.session:
  30. check = Check.objects.get(code=request.session["welcome_code"])
  31. # Only associate demo check if it doesn't have an owner already.
  32. if check.user is None:
  33. check.user = user
  34. check.save()
  35. check.assign_all_channels()
  36. del request.session["welcome_code"]
  37. def login(request):
  38. bad_credentials = False
  39. if request.method == 'POST':
  40. form = EmailPasswordForm(request.POST)
  41. if form.is_valid():
  42. email = form.cleaned_data["email"]
  43. password = form.cleaned_data["password"]
  44. if len(password):
  45. user = authenticate(username=email, password=password)
  46. if user is not None and user.is_active:
  47. auth_login(request, user)
  48. return redirect("hc-checks")
  49. bad_credentials = True
  50. else:
  51. try:
  52. user = User.objects.get(email=email)
  53. except User.DoesNotExist:
  54. user = _make_user(email)
  55. _associate_demo_check(request, user)
  56. profile = Profile.objects.for_user(user)
  57. profile.send_instant_login_link()
  58. return redirect("hc-login-link-sent")
  59. else:
  60. form = EmailPasswordForm()
  61. bad_link = request.session.pop("bad_link", None)
  62. ctx = {
  63. "form": form,
  64. "bad_credentials": bad_credentials,
  65. "bad_link": bad_link
  66. }
  67. return render(request, "accounts/login.html", ctx)
  68. def logout(request):
  69. auth_logout(request)
  70. return redirect("hc-index")
  71. def login_link_sent(request):
  72. return render(request, "accounts/login_link_sent.html")
  73. def set_password_link_sent(request):
  74. return render(request, "accounts/set_password_link_sent.html")
  75. def check_token(request, username, token):
  76. if request.user.is_authenticated() and request.user.username == username:
  77. # User is already logged in
  78. return redirect("hc-checks")
  79. user = authenticate(username=username, token=token)
  80. if user is not None and user.is_active:
  81. # This should get rid of "welcome_code" in session
  82. request.session.flush()
  83. profile = Profile.objects.for_user(user)
  84. profile.token = ""
  85. profile.save()
  86. auth_login(request, user)
  87. return redirect("hc-checks")
  88. request.session["bad_link"] = True
  89. return redirect("hc-login")
  90. @login_required
  91. def profile(request):
  92. profile = Profile.objects.for_user(request.user)
  93. if request.method == "POST":
  94. if "set_password" in request.POST:
  95. profile.send_set_password_link()
  96. return redirect("hc-set-password-link-sent")
  97. form = ReportSettingsForm(request.POST)
  98. if form.is_valid():
  99. profile.reports_allowed = form.cleaned_data["reports_allowed"]
  100. profile.save()
  101. messages.info(request, "Your settings have been updated!")
  102. ctx = {
  103. "profile": profile
  104. }
  105. return render(request, "accounts/profile.html", ctx)
  106. @login_required
  107. def set_password(request, token):
  108. profile = Profile.objects.for_user(request.user)
  109. if not check_password(token, profile.token):
  110. return HttpResponseBadRequest()
  111. if request.method == "POST":
  112. form = SetPasswordForm(request.POST)
  113. if form.is_valid():
  114. password = form.cleaned_data["password"]
  115. request.user.set_password(password)
  116. request.user.save()
  117. profile.token = ""
  118. profile.save()
  119. # Setting a password logs the user out, so here we
  120. # log them back in.
  121. u = authenticate(username=request.user.email, password=password)
  122. auth_login(request, u)
  123. messages.info(request, "Your password has been set!")
  124. return redirect("hc-profile")
  125. ctx = {
  126. }
  127. return render(request, "accounts/set_password.html", ctx)
  128. def unsubscribe_reports(request, username):
  129. try:
  130. signing.Signer().unsign(request.GET.get("token"))
  131. except signing.BadSignature:
  132. return HttpResponseBadRequest()
  133. user = User.objects.get(username=username)
  134. profile = Profile.objects.for_user(user)
  135. profile.reports_allowed = False
  136. profile.save()
  137. return render(request, "accounts/unsubscribed.html")