From 7b478fed4e436e85e4903eacd89aa2b83fc7d442 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 10 May 2018 20:40:08 +1000 Subject: [PATCH] Redirect if not authed If user not logged in, redirect any requests to login page --- InvenTree/InvenTree/middleware.py | 26 ++++++++++ InvenTree/InvenTree/settings.py | 1 + InvenTree/static/css/inventree.css | 7 +++ InvenTree/templates/navbar.html | 2 +- InvenTree/templates/registration/login.html | 57 +++++++++++++++++---- 5 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 InvenTree/InvenTree/middleware.py diff --git a/InvenTree/InvenTree/middleware.py b/InvenTree/InvenTree/middleware.py new file mode 100644 index 0000000000..fc75be2fb4 --- /dev/null +++ b/InvenTree/InvenTree/middleware.py @@ -0,0 +1,26 @@ +from django.shortcuts import HttpResponseRedirect +from django.urls import reverse_lazy + + +class AuthRequiredMiddleware(object): + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + # Code to be executed for each request before + # the view (and later middleware) are called. + + assert hasattr(request, 'user') + + response = self.get_response(request) + + if not request.user.is_authenticated(): + print(request.path_info) + + if not request.path_info == reverse_lazy('login'): + return HttpResponseRedirect(reverse_lazy('login')) + + # Code to be executed for each request/response after + # the view is called. + + return response \ No newline at end of file diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 7859c43270..c3245134c7 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -71,6 +71,7 @@ MIDDLEWARE = [ 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'simple_history.middleware.HistoryRequestMiddleware', + 'InvenTree.middleware.AuthRequiredMiddleware' ] ROOT_URLCONF = 'InvenTree.urls' diff --git a/InvenTree/static/css/inventree.css b/InvenTree/static/css/inventree.css index cadb012959..14b021adc7 100644 --- a/InvenTree/static/css/inventree.css +++ b/InvenTree/static/css/inventree.css @@ -128,4 +128,11 @@ .warning-msg { color: #e00; +} + +.login { + position: absolute; + top: 50%; + left: 50%; + transform: translate(-50%, -50%); } \ No newline at end of file diff --git a/InvenTree/templates/navbar.html b/InvenTree/templates/navbar.html index 01c4ceb1b3..31e0d4de9b 100644 --- a/InvenTree/templates/navbar.html +++ b/InvenTree/templates/navbar.html @@ -2,7 +2,7 @@