2020-11-29 22:35:42 +01:00
|
|
|
from django.views.generic import TemplateView, RedirectView
|
|
|
|
from django.shortcuts import redirect
|
2020-10-17 21:31:55 +02:00
|
|
|
from django.urls import reverse
|
2020-11-29 22:35:42 +01:00
|
|
|
from django.dispatch import receiver
|
2020-10-17 21:31:55 +02:00
|
|
|
from django.contrib.auth import logout as auth_logout
|
2020-11-29 22:35:42 +01:00
|
|
|
from django.contrib.auth import user_logged_in, user_logged_out, user_login_failed
|
|
|
|
from django.contrib import messages
|
2020-10-17 21:31:55 +02:00
|
|
|
|
|
|
|
from urllib.parse import quote as urlquote
|
|
|
|
|
|
|
|
|
2020-11-29 22:35:42 +01:00
|
|
|
class LoginView(TemplateView):
|
|
|
|
template_name = "registration/login_switch.html"
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
if request.user.is_authenticated:
|
|
|
|
messages.warning(
|
|
|
|
request,
|
|
|
|
"Vous êtes déjà connecté·e en tant que {}.".format(
|
|
|
|
request.user.username
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return redirect(self.get_next_url() or "/")
|
|
|
|
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_next_url(self):
|
|
|
|
if self.request.method == "GET":
|
|
|
|
req_dict = self.request.GET
|
|
|
|
elif self.request.method == "POST":
|
|
|
|
req_dict = self.request.POST
|
|
|
|
return req_dict.get("next")
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
next_url = self.get_next_url()
|
|
|
|
if next_url:
|
|
|
|
context["pass_url"] = "{}?next={}".format(
|
|
|
|
reverse("gestiojeux_auth:password_login"), urlquote(next_url, safe="")
|
|
|
|
)
|
|
|
|
context["cas_url"] = "{}?next={}".format(
|
|
|
|
reverse("gestiojeux_auth:cas_ng_login"), urlquote(next_url, safe="")
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
context["pass_url"] = reverse("gestiojeux_auth:password_login")
|
|
|
|
context["cas_url"] = reverse("gestiojeux_auth:cas_ng_login")
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class LogoutView(RedirectView):
|
|
|
|
permanent = False
|
|
|
|
|
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
|
|
|
CAS_BACKEND_NAME = "django_cas_ng.backends.CASBackend"
|
|
|
|
if self.request.session["_auth_user_backend"] != CAS_BACKEND_NAME:
|
|
|
|
auth_logout(self.request)
|
|
|
|
if "next" in self.request.GET:
|
|
|
|
return self.request.GET["next"]
|
|
|
|
return reverse("mainsite:home")
|
|
|
|
|
|
|
|
if "next" in self.request.GET:
|
|
|
|
return "{}?next={}".format(
|
2020-11-22 13:34:01 +01:00
|
|
|
reverse("gestiojeux_auth:cas_ng_logout"),
|
2020-11-29 22:35:42 +01:00
|
|
|
urlquote(self.request.GET["next"], safe=""),
|
2020-11-22 13:34:01 +01:00
|
|
|
)
|
2020-11-29 22:35:42 +01:00
|
|
|
return reverse("gestiojeux_auth:cas_ng_logout")
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(user_logged_in)
|
|
|
|
def on_login(request, user, **kwargs):
|
|
|
|
messages.success(request, "Connexion réussie. Bienvenue, {}.".format(user))
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(user_logged_out)
|
|
|
|
def on_logout(request, **kwargs):
|
|
|
|
messages.info(request, "Vous avez bien été déconnecté·e.")
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(user_login_failed)
|
|
|
|
def on_login_failed(request, **kwargs):
|
|
|
|
messages.error(request, "Connexion échouée.")
|