2024-10-23 11:06:57 +02:00
|
|
|
from urllib.parse import quote as urlquote
|
|
|
|
|
2017-10-14 16:22:23 +02:00
|
|
|
from django.contrib.auth import logout as auth_logout
|
|
|
|
from django.contrib.auth.decorators import login_required
|
2024-10-23 11:06:57 +02:00
|
|
|
from django.shortcuts import redirect, render
|
|
|
|
from django.urls import reverse
|
2017-10-14 16:22:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
def login(req):
|
2020-06-16 12:00:24 +02:00
|
|
|
if req.user.is_authenticated:
|
|
|
|
return redirect("homepage")
|
2017-10-14 16:22:23 +02:00
|
|
|
|
2020-06-16 12:00:24 +02:00
|
|
|
if req.method == "GET":
|
2017-10-14 16:22:23 +02:00
|
|
|
reqDict = req.GET
|
2020-06-16 12:00:24 +02:00
|
|
|
elif req.method == "POST":
|
2017-10-14 16:22:23 +02:00
|
|
|
reqDict = req.POST
|
2020-06-16 12:00:24 +02:00
|
|
|
if "next" in reqDict:
|
|
|
|
nextUrl = reqDict["next"]
|
2017-10-14 16:22:23 +02:00
|
|
|
context = {
|
2020-06-16 12:00:24 +02:00
|
|
|
"pass_url": "{}?next={}".format(
|
|
|
|
reverse("password_login"), urlquote(nextUrl, safe="")
|
|
|
|
),
|
|
|
|
"cas_url": "{}?next={}".format(
|
|
|
|
reverse("cas_ng_login"), urlquote(nextUrl, safe="")
|
|
|
|
),
|
2017-10-14 16:22:23 +02:00
|
|
|
}
|
|
|
|
else:
|
|
|
|
context = {
|
2020-06-16 12:00:24 +02:00
|
|
|
"pass_url": reverse("password_login"),
|
|
|
|
"cas_url": reverse("cas_ng_login"),
|
2017-10-14 16:22:23 +02:00
|
|
|
}
|
|
|
|
|
2020-06-16 12:00:24 +02:00
|
|
|
return render(req, "mainsite/login.html", context=context)
|
2017-10-14 16:22:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def logout(req):
|
2020-06-16 12:00:24 +02:00
|
|
|
CAS_BACKEND_NAME = "django_cas_ng.backends.CASBackend"
|
|
|
|
if req.session["_auth_user_backend"] != CAS_BACKEND_NAME:
|
2017-10-14 16:22:23 +02:00
|
|
|
auth_logout(req)
|
2020-06-16 12:00:24 +02:00
|
|
|
return redirect("homepage")
|
|
|
|
return redirect("cas_ng_logout")
|