www-bocal/bocal_auth/views.py

50 lines
1.3 KiB
Python
Raw Permalink Normal View History

from urllib.parse import quote as urlquote
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
from django.shortcuts import redirect, render
from django.urls import reverse
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")
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:
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
),
}
else:
context = {
2020-06-16 12:00:24 +02:00
"pass_url": reverse("password_login"),
"cas_url": reverse("cas_ng_login"),
}
2024-10-23 13:21:36 +02:00
return render(request, "mainsite/login.html", context=context)
@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")