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.

105 lines
2.7 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
9 years ago
  1. import uuid
  2. from django.conf import settings
  3. from django.contrib.auth import authenticate
  4. from django.contrib.auth import login as auth_login, logout as auth_logout
  5. from django.contrib.auth.models import User
  6. from django.core.urlresolvers import reverse
  7. from django.http import HttpResponseBadRequest
  8. from django.shortcuts import redirect, render
  9. from hc.accounts.forms import EmailForm
  10. from hc.api.models import Channel, Check
  11. from hc.lib import emails
  12. def _make_user(email):
  13. username = str(uuid.uuid4())[:30]
  14. user = User(username=username, email=email)
  15. user.save()
  16. channel = Channel()
  17. channel.user = user
  18. channel.kind = "email"
  19. channel.value = email
  20. channel.email_verified = True
  21. channel.save()
  22. return user
  23. def _associate_demo_check(request, user):
  24. if "welcome_code" in request.session:
  25. check = Check.objects.get(code=request.session["welcome_code"])
  26. # Only associate demo check if it doesn't have an owner already.
  27. if check.user is None:
  28. check.user = user
  29. check.save()
  30. check.assign_all_channels()
  31. del request.session["welcome_code"]
  32. def _send_login_link(user):
  33. token = str(uuid.uuid4())
  34. user.set_password(token)
  35. user.save()
  36. login_link = reverse("hc-check-token", args=[user.username, token])
  37. login_link = settings.SITE_ROOT + login_link
  38. ctx = {"login_link": login_link}
  39. emails.login(user.email, ctx)
  40. def login(request):
  41. if request.method == 'POST':
  42. form = EmailForm(request.POST)
  43. if form.is_valid():
  44. email = form.cleaned_data["email"]
  45. try:
  46. user = User.objects.get(email=email)
  47. except User.DoesNotExist:
  48. user = _make_user(email)
  49. _associate_demo_check(request, user)
  50. # We don't want to reset passwords of staff users :-)
  51. if user.is_staff:
  52. return HttpResponseBadRequest()
  53. _send_login_link(user)
  54. return redirect("hc-login-link-sent")
  55. else:
  56. form = EmailForm()
  57. ctx = {"form": form}
  58. return render(request, "accounts/login.html", ctx)
  59. def logout(request):
  60. auth_logout(request)
  61. return redirect("hc-index")
  62. def login_link_sent(request):
  63. return render(request, "accounts/login_link_sent.html")
  64. def check_token(request, username, token):
  65. user = authenticate(username=username, password=token)
  66. if user is not None:
  67. if user.is_active:
  68. # This should get rid of "welcome_code" in session
  69. request.session.flush()
  70. user.set_unusable_password()
  71. user.save()
  72. auth_login(request, user)
  73. return redirect("hc-checks")
  74. return render(request, "bad_link.html")