gestiojeux/gestiojeux_auth/views.py

53 lines
1.6 KiB
Python
Raw Normal View History

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"),
}
return render(req, "registration/login_switch.html", context=context)
@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)
if "next" in req.GET:
return redirect(req.GET["next"])
return redirect("mainsite:home")
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")