www-bocal/bocal_auth/views.py

43 lines
1.2 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):
2020-06-16 12:00:24 +02:00
if req.user.is_authenticated:
return redirect("homepage")
2020-06-16 12:00:24 +02:00
if req.method == "GET":
reqDict = req.GET
2020-06-16 12:00:24 +02:00
elif req.method == "POST":
reqDict = req.POST
2020-06-16 12:00:24 +02:00
if "next" in reqDict:
nextUrl = reqDict["next"]
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="")
),
}
else:
context = {
2020-06-16 12:00:24 +02:00
"pass_url": reverse("password_login"),
"cas_url": reverse("cas_ng_login"),
}
2020-06-16 12:00:24 +02:00
return render(req, "mainsite/login.html", context=context)
@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:
auth_logout(req)
2020-06-16 12:00:24 +02:00
return redirect("homepage")
return redirect("cas_ng_logout")