La version 5 de Planète Casio. Regroupe le forum, les programmes, les tutoriel, les sprites et tous les autres outils développés par nos soins.
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.

69 lines
2.3KB

  1. from flask import redirect, url_for, request, flash
  2. from flask_login import login_user, logout_user, login_required, current_user
  3. from app import app
  4. from app.forms.login import LoginForm
  5. from app.models.users import Member
  6. from app.models.privs import Group
  7. from app.utils.render import render
  8. from config import V5Config
  9. @app.route('/login', methods=['GET', 'POST'])
  10. def login():
  11. if current_user.is_authenticated:
  12. return redirect(url_for('index'))
  13. form = LoginForm()
  14. if form.validate_on_submit():
  15. member = Member.query.filter_by(name=form.username.data).first()
  16. # Check if member can login
  17. if "No login" in [g.name for g in member.groups]:
  18. flash('Cet utilisateur ne peut pas se connecter', 'error')
  19. if request.referrer:
  20. return redirect(request.referrer)
  21. return redirect(url_for('index'))
  22. # Check if password is ok
  23. if member is None or not member.check_password(form.password.data):
  24. flash('Pseudo ou mot de passe invalide', 'error')
  25. if request.referrer:
  26. return redirect(request.referrer)
  27. return redirect(url_for('index'))
  28. # Login & update time-based trophies
  29. login_user(member, remember=form.remember_me.data,
  30. duration=V5Config.REMEMBER_COOKIE_DURATION)
  31. member.update_trophies("on-login")
  32. # Redirect safely (https://huit.re/open-redirect)
  33. def is_safe_url(target):
  34. ref_url = urlparse(request.host_url)
  35. test_url = urlparse(urljoin(request.host_url, target))
  36. return test_url.scheme in ('http', 'https') and \
  37. ref_url.netloc == test_url.netloc
  38. next = request.args.get('next')
  39. if next and is_safe_url(next):
  40. return redirect(next)
  41. if request.referrer:
  42. return redirect(request.referrer)
  43. return redirect(url_for('index'))
  44. return render('login.html', form=form)
  45. @app.route('/logout')
  46. @login_required
  47. def logout():
  48. try:
  49. print(request.referrer)
  50. except Exception as e:
  51. print('No referrer:', e)
  52. logout_user()
  53. flash('Déconnexion réussie', 'info')
  54. if request.referrer:
  55. return redirect(request.referrer)
  56. return redirect(url_for('index'))