From 85c948ef6718b081ddc7d6da057a7a4ecde7bbae Mon Sep 17 00:00:00 2001 From: Andy Seracuse Date: Sun, 3 May 2020 14:11:57 -0600 Subject: [PATCH] made changed middleware and made necessary changes to settings and urls fles so that the user is redirected to their desired page after a login redirect unless the desired page was logout, in which case the user will be redirect --- InvenTree/InvenTree/middleware.py | 7 ++++++- InvenTree/InvenTree/settings.py | 3 +++ InvenTree/InvenTree/urls.py | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/InvenTree/InvenTree/middleware.py b/InvenTree/InvenTree/middleware.py index 8a2c0b17a4..d68db75de8 100644 --- a/InvenTree/InvenTree/middleware.py +++ b/InvenTree/InvenTree/middleware.py @@ -1,6 +1,9 @@ from django.shortcuts import HttpResponseRedirect from django.urls import reverse_lazy from django.db import connection +from django.shortcuts import redirect +from django.conf import settings +from django.shortcuts import redirect import logging import time import operator @@ -63,8 +66,10 @@ class AuthRequiredMiddleware(object): # No authorization was found for the request if not authorized: - if not request.path_info == reverse_lazy('login') and not request.path_info.startswith('/api/'): + if request.path_info == reverse_lazy('logout'): return HttpResponseRedirect(reverse_lazy('login')) + if not request.path_info == reverse_lazy('login') and not request.path_info.startswith('/api/'): + return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path)) # Code to be executed for each request/response after # the view is called. diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index fa6bf96ec7..3d6068d454 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -39,6 +39,9 @@ else: with open(cfg_filename, 'r') as cfg: CONFIG = yaml.safe_load(cfg) +#provide a default login url +LOGIN_URL = "/login" + # Read the autogenerated key-file key_file = open(os.path.join(BASE_DIR, 'secret_key.txt'), 'r') diff --git a/InvenTree/InvenTree/urls.py b/InvenTree/InvenTree/urls.py index d9600333f4..348b0621b8 100644 --- a/InvenTree/InvenTree/urls.py +++ b/InvenTree/InvenTree/urls.py @@ -89,7 +89,7 @@ urlpatterns = [ url(r'^auth/', include('rest_framework.urls', namespace='rest_framework')), - url(r'^login/', auth_views.LoginView.as_view(), name='login'), + url(r'^login/?', auth_views.LoginView.as_view(), name='login'), url(r'^logout/', auth_views.LogoutView.as_view(template_name='registration/logout.html'), name='logout'), url(r'^settings/', include(settings_urls)),