2020-10-17 21:31:55 +02:00
|
|
|
from django.shortcuts import render, redirect
|
|
|
|
from django.urls import reverse
|
|
|
|
from django.contrib.auth import logout as auth_logout
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
|
|
|
|
from urllib.parse import quote as urlquote
|
|
|
|
|
|
|
|
|
|
|
|
def login(req):
|
|
|
|
if req.user.is_authenticated:
|
|
|
|
return redirect("mainsite:home")
|
|
|
|
|
|
|
|
if req.method == "GET":
|
|
|
|
reqDict = req.GET
|
|
|
|
elif req.method == "POST":
|
|
|
|
reqDict = req.POST
|
|
|
|
if "next" in reqDict:
|
|
|
|
nextUrl = reqDict["next"]
|
|
|
|
context = {
|
|
|
|
"pass_url": "{}?next={}".format(
|
|
|
|
reverse("gestiojeux_auth:password_login"), urlquote(nextUrl, safe="")
|
|
|
|
),
|
|
|
|
"cas_url": "{}?next={}".format(
|
|
|
|
reverse("gestiojeux_auth:cas_ng_login"), urlquote(nextUrl, safe="")
|
|
|
|
),
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
context = {
|
|
|
|
"pass_url": reverse("gestiojeux_auth:password_login"),
|
|
|
|
"cas_url": reverse("gestiojeux_auth:cas_ng_login"),
|
|
|
|
}
|
|
|
|
|
2020-11-22 13:34:01 +01:00
|
|
|
return render(req, "registration/login_switch.html", context=context)
|
2020-10-17 21:31:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def logout(req):
|
|
|
|
CAS_BACKEND_NAME = "django_cas_ng.backends.CASBackend"
|
|
|
|
if req.session["_auth_user_backend"] != CAS_BACKEND_NAME:
|
|
|
|
auth_logout(req)
|
2020-11-22 13:34:01 +01:00
|
|
|
if "next" in req.GET:
|
|
|
|
return redirect(req.GET["next"])
|
2020-10-17 21:31:55 +02:00
|
|
|
return redirect("mainsite:home")
|
|
|
|
|
2020-11-22 13:34:01 +01:00
|
|
|
if "next" in req.GET:
|
|
|
|
return redirect(
|
|
|
|
"{}?next={}".format(
|
|
|
|
reverse("gestiojeux_auth:cas_ng_logout"),
|
|
|
|
urlquote(req.GET["next"], safe=""),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return redirect("gestiojeux_auth:cas_ng_logout")
|