2021-05-03 20:35:35 +01:00
|
|
|
|
|
|
|
from django.conf import settings
|
2023-01-19 18:35:56 +00:00
|
|
|
from django.contrib.auth import authenticate
|
2021-04-06 00:49:09 +01:00
|
|
|
from django.contrib.auth import forms as auth_forms
|
2023-01-19 18:35:56 +00:00
|
|
|
from django.contrib.auth import login, logout
|
2021-05-03 20:35:35 +01:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2023-01-19 18:35:56 +00:00
|
|
|
from django.shortcuts import redirect, render
|
2022-06-25 16:13:02 +01:00
|
|
|
from django.utils.http import is_safe_url
|
2021-05-03 20:35:35 +01:00
|
|
|
|
|
|
|
"""This enforces the login requirement for non-public pages using
|
|
|
|
the decorator mechanism.
|
|
|
|
https://www.fullstackpython.com/django-contrib-auth-decorators-login-required-examples.html
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2023-01-30 19:04:36 +00:00
|
|
|
class login_required_if_public(object):
|
2021-05-03 20:35:35 +01:00
|
|
|
def __init__(self, f):
|
|
|
|
if settings.PUBLIC_SITE:
|
|
|
|
self.f = login_required(f)
|
|
|
|
else:
|
|
|
|
self.f = f
|
|
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
return self.f(*args, **kwargs)
|
|
|
|
|
2021-04-06 00:49:09 +01:00
|
|
|
|
|
|
|
# This is copied from CUYC.cuy.website.view.auth
|
2023-01-30 19:04:36 +00:00
|
|
|
# If we want to do the whole online-email thing, we would also need to copy across the code in these
|
|
|
|
# imported files and delete what is superfluous.
|
2021-04-06 00:49:09 +01:00
|
|
|
# Or we could just load the latest version of django-registration app.
|
2023-01-30 19:04:36 +00:00
|
|
|
# from cuy.club.models import Member, Message
|
|
|
|
# from ..forms import WebsiteLoginForm, WebsiteRegisterForm
|
|
|
|
# from ...common import mail_site_error
|
|
|
|
# from .generic import user_is_active
|
2021-04-06 00:49:09 +01:00
|
|
|
|
2023-01-30 19:04:36 +00:00
|
|
|
"""The login and logout functions.
|
2021-04-06 00:49:09 +01:00
|
|
|
This is also where we would manage registration: for people wanting to create and validate their individual
|
2023-01-30 19:04:36 +00:00
|
|
|
logon accounts/forgottenpassword"""
|
2021-04-06 00:49:09 +01:00
|
|
|
|
|
|
|
############################
|
|
|
|
# Authentication Functions #
|
|
|
|
############################
|
|
|
|
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2021-04-06 00:49:09 +01:00
|
|
|
def expologout(request):
|
|
|
|
login_form = auth_forms.AuthenticationForm()
|
|
|
|
logout(request)
|
2023-01-30 19:04:36 +00:00
|
|
|
|
|
|
|
return render(request, "login/logout.html", {"form": login_form})
|
|
|
|
|
2021-04-06 00:49:09 +01:00
|
|
|
|
|
|
|
def expologin(request):
|
|
|
|
# GET
|
2023-01-30 19:04:36 +00:00
|
|
|
if not request.method == "POST":
|
2021-04-06 00:49:09 +01:00
|
|
|
if (not request.user.is_authenticated) or (not request.user.is_active):
|
2023-01-30 19:04:36 +00:00
|
|
|
return render(request, "login/index.html", {})
|
2021-04-06 00:49:09 +01:00
|
|
|
else:
|
|
|
|
# going to login page when you are already logged in
|
2023-01-30 19:04:36 +00:00
|
|
|
return render(request, "tasks.html", {})
|
2021-04-06 00:49:09 +01:00
|
|
|
|
|
|
|
# POST
|
2023-01-30 19:04:36 +00:00
|
|
|
username = request.POST["username"]
|
|
|
|
password = request.POST["password"]
|
2021-04-06 00:49:09 +01:00
|
|
|
|
|
|
|
user = authenticate(username=username, password=password)
|
|
|
|
if user is None:
|
2023-01-30 19:04:36 +00:00
|
|
|
return render(request, "login/index.html", {"invalid": True, "username": username})
|
2021-04-06 00:49:09 +01:00
|
|
|
if not user.is_active:
|
2023-01-30 19:04:36 +00:00
|
|
|
return render(request, "login/enable.html", {"login_state": "notenabled"})
|
2021-04-06 00:49:09 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
login(request, user)
|
|
|
|
# Should do the ?next= stuff here..
|
2022-06-25 16:13:02 +01:00
|
|
|
return redirect_after_login(request)
|
2021-04-06 00:49:09 +01:00
|
|
|
except:
|
2023-01-30 19:04:36 +00:00
|
|
|
return render(request, "errors/generic.html", {})
|
|
|
|
|
|
|
|
|
2022-06-25 16:13:02 +01:00
|
|
|
def redirect_after_login(request):
|
|
|
|
nxt = request.GET.get("next", None)
|
|
|
|
if nxt is None:
|
|
|
|
return redirect(settings.LOGIN_REDIRECT_URL)
|
2023-01-30 19:04:36 +00:00
|
|
|
elif not is_safe_url(url=nxt, allowed_hosts={request.get_host()}, require_https=request.is_secure()):
|
2022-06-25 16:13:02 +01:00
|
|
|
return redirect(settings.LOGIN_REDIRECT_URL)
|
|
|
|
else:
|
|
|
|
return redirect(nxt)
|