1
0
Fork 0
spejstore-new/storage/middleware.py

30 lines
866 B
Python
Raw Normal View History

2023-08-25 19:06:01 +00:00
from storage.authentication import has_permission
from django.http import HttpResponseRedirect
2023-08-25 19:06:01 +00:00
def is_authorized_or_in_lan_middleware(get_response):
# One-time configuration and initialization.
2023-09-09 13:43:23 +00:00
login_paths_to_ignore = [
"/admin/login",
"/static",
"/admin/static",
"/complete",
"/favicon.ico",
"/api",
2023-09-09 13:43:23 +00:00
]
2023-08-25 19:06:01 +00:00
def middleware(request):
if request.user.is_authenticated:
2023-09-09 13:43:23 +00:00
return get_response(request)
2023-08-25 19:06:01 +00:00
is_within_lan = has_permission(request)
if is_within_lan:
2023-09-09 13:43:23 +00:00
return get_response(request)
2023-08-25 19:06:01 +00:00
else:
2023-09-09 13:43:23 +00:00
for login_path in login_paths_to_ignore:
if request.path.startswith(login_path):
2023-09-09 13:43:23 +00:00
return get_response(request)
else:
return HttpResponseRedirect("/admin/login")
2023-08-25 19:06:01 +00:00
return middleware