views.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # GNU MediaGoblin -- federated, autonomous media hosting
  2. # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import six
  17. from itsdangerous import BadSignature
  18. from mediagoblin import messages, mg_globals
  19. from mediagoblin.db.models import User, Privilege
  20. from mediagoblin.tools.crypto import get_timed_signer_url
  21. from mediagoblin.decorators import auth_enabled, allow_registration
  22. from mediagoblin.tools.response import render_to_response, redirect, render_404
  23. from mediagoblin.tools.translate import pass_to_ugettext as _
  24. from mediagoblin.tools.mail import email_debug_message
  25. from mediagoblin.tools.pluginapi import hook_handle
  26. from mediagoblin.auth.tools import (send_verification_email, register_user,
  27. check_login_simple)
  28. @allow_registration
  29. @auth_enabled
  30. def register(request):
  31. """The registration view.
  32. Note that usernames will always be lowercased. Email domains are lowercased while
  33. the first part remains case-sensitive.
  34. """
  35. if 'pass_auth' not in request.template_env.globals:
  36. redirect_name = hook_handle('auth_no_pass_redirect')
  37. if redirect_name:
  38. return redirect(request, 'mediagoblin.plugins.{0}.register'.format(
  39. redirect_name))
  40. else:
  41. return redirect(request, 'index')
  42. register_form = hook_handle("auth_get_registration_form", request)
  43. if request.method == 'POST' and register_form.validate():
  44. # TODO: Make sure the user doesn't exist already
  45. user = register_user(request, register_form)
  46. if user:
  47. # redirect the user to their homepage... there will be a
  48. # message waiting for them to verify their email
  49. return redirect(
  50. request, 'mediagoblin.user_pages.user_home',
  51. user=user.username)
  52. return render_to_response(
  53. request,
  54. 'mediagoblin/auth/register.html',
  55. {'register_form': register_form,
  56. 'post_url': request.urlgen('mediagoblin.auth.register')})
  57. @auth_enabled
  58. def login(request):
  59. """
  60. MediaGoblin login view.
  61. If you provide the POST with 'next', it'll redirect to that view.
  62. """
  63. if 'pass_auth' not in request.template_env.globals:
  64. redirect_name = hook_handle('auth_no_pass_redirect')
  65. if redirect_name:
  66. return redirect(request, 'mediagoblin.plugins.{0}.login'.format(
  67. redirect_name))
  68. else:
  69. return redirect(request, 'index')
  70. login_form = hook_handle("auth_get_login_form", request)
  71. login_failed = False
  72. if request.method == 'POST':
  73. if login_form.validate():
  74. user = check_login_simple(
  75. login_form.username.data,
  76. login_form.password.data)
  77. if user:
  78. # set up login in session
  79. if login_form.stay_logged_in.data:
  80. request.session['stay_logged_in'] = True
  81. request.session['user_id'] = six.text_type(user.id)
  82. request.session.save()
  83. if request.form.get('next'):
  84. return redirect(request, location=request.form['next'])
  85. else:
  86. return redirect(request, "index")
  87. login_failed = True
  88. return render_to_response(
  89. request,
  90. 'mediagoblin/auth/login.html',
  91. {'login_form': login_form,
  92. 'next': request.GET.get('next') or request.form.get('next'),
  93. 'login_failed': login_failed,
  94. 'post_url': request.urlgen('mediagoblin.auth.login'),
  95. 'allow_registration': mg_globals.app_config["allow_registration"]})
  96. def logout(request):
  97. # Maybe deleting the user_id parameter would be enough?
  98. request.session.delete()
  99. return redirect(request, "index")
  100. def verify_email(request):
  101. """
  102. Email verification view
  103. validates GET parameters against database and unlocks the user account, if
  104. you are lucky :)
  105. """
  106. # If we don't have userid and token parameters, we can't do anything; 404
  107. if not 'token' in request.GET:
  108. return render_404(request)
  109. # Catch error if token is faked or expired
  110. try:
  111. token = get_timed_signer_url("mail_verification_token") \
  112. .loads(request.GET['token'], max_age=10*24*3600)
  113. except BadSignature:
  114. messages.add_message(
  115. request,
  116. messages.ERROR,
  117. _('The verification key or user id is incorrect.'))
  118. return redirect(
  119. request,
  120. 'index')
  121. user = User.query.filter_by(id=int(token)).first()
  122. if user and user.has_privilege(u'active') is False:
  123. user.verification_key = None
  124. user.all_privileges.append(
  125. Privilege.query.filter(
  126. Privilege.privilege_name==u'active').first())
  127. user.save()
  128. messages.add_message(
  129. request,
  130. messages.SUCCESS,
  131. _("Your email address has been verified. "
  132. "You may now login, edit your profile, and submit images!"))
  133. else:
  134. messages.add_message(
  135. request,
  136. messages.ERROR,
  137. _('The verification key or user id is incorrect'))
  138. return redirect(
  139. request, 'mediagoblin.user_pages.user_home',
  140. user=user.username)
  141. def resend_activation(request):
  142. """
  143. The reactivation view
  144. Resend the activation email.
  145. """
  146. if request.user is None:
  147. messages.add_message(
  148. request,
  149. messages.ERROR,
  150. _('You must be logged in so we know who to send the email to!'))
  151. return redirect(request, 'mediagoblin.auth.login')
  152. if request.user.has_privilege(u'active'):
  153. messages.add_message(
  154. request,
  155. messages.ERROR,
  156. _("You've already verified your email address!"))
  157. return redirect(request, "mediagoblin.user_pages.user_home", user=request.user['username'])
  158. email_debug_message(request)
  159. send_verification_email(request.user, request)
  160. messages.add_message(
  161. request,
  162. messages.INFO,
  163. _('Resent your verification email.'))
  164. return redirect(
  165. request, 'mediagoblin.user_pages.user_home',
  166. user=request.user.username)