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 13:21:36 +02:00
|
|
|
from django.http import HttpRequest
|
|
|
|
from django.http.response import Http404
|
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
|
|
|
|
|
|
|
|
2024-10-23 13:21:36 +02:00
|
|
|
def login(request: HttpRequest):
|
|
|
|
if request.user.is_authenticated:
|
2020-06-16 12:00:24 +02:00
|
|
|
return redirect("homepage")
|
2017-10-14 16:22:23 +02:00
|
|
|
|
2024-10-23 13:21:36 +02:00
|
|
|
match request.method:
|
|
|
|
case "GET":
|
|
|
|
data = request.GET
|
|
|
|
case "POST":
|
|
|
|
data = request.POST
|
|
|
|
case _:
|
|
|
|
raise Http404
|
|
|
|
|
|
|
|
next = data.get("next")
|
|
|
|
|
|
|
|
if next is not None:
|
2017-10-14 16:22:23 +02:00
|
|
|
context = {
|
2020-06-16 12:00:24 +02:00
|
|
|
"pass_url": "{}?next={}".format(
|
2024-10-23 13:21:36 +02:00
|
|
|
reverse("password_login"), urlquote(next, safe="")
|
2020-06-16 12:00:24 +02:00
|
|
|
),
|
|
|
|
"cas_url": "{}?next={}".format(
|
2024-10-23 13:21:36 +02:00
|
|
|
reverse("cas_ng_login"), urlquote(next, safe="")
|
2020-06-16 12:00:24 +02:00
|
|
|
),
|
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
|
|
|
}
|
|
|
|
|
2024-10-23 13:21:36 +02:00
|
|
|
return render(request, "mainsite/login.html", context=context)
|
2017-10-14 16:22:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2024-10-23 13:21:36 +02:00
|
|
|
def logout(request: HttpRequest):
|
|
|
|
if request.session["_auth_user_backend"] != "django_cas_ng.backends.CASBackend":
|
|
|
|
auth_logout(request)
|
2020-06-16 12:00:24 +02:00
|
|
|
return redirect("homepage")
|
2024-10-23 13:21:36 +02:00
|
|
|
|
2020-06-16 12:00:24 +02:00
|
|
|
return redirect("cas_ng_logout")
|