2023-08-25 19:06:01 +00:00
|
|
|
from storage.authentication import has_permission
|
2023-09-09 13:43:23 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2023-09-09 17:26:36 +00:00
|
|
|
from spejstore.settings import STATIC_URL, MEDIA_URL, LOGIN_URL
|
|
|
|
from django.core.exceptions import PermissionDenied
|
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 = [
|
2023-09-09 17:26:36 +00:00
|
|
|
"/login",
|
|
|
|
LOGIN_URL[:-1],
|
|
|
|
STATIC_URL[:-1],
|
|
|
|
MEDIA_URL[:-1],
|
2023-09-09 13:43:23 +00:00
|
|
|
"/admin/static",
|
|
|
|
"/complete",
|
|
|
|
"/favicon.ico",
|
2023-09-09 17:26:36 +00:00
|
|
|
"/api/1",
|
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-09-09 17:26:36 +00:00
|
|
|
is_within_lan, error_message = has_permission(request)
|
2023-08-25 19:06:01 +00:00
|
|
|
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:
|
2023-09-09 13:43:23 +00:00
|
|
|
if request.path.startswith(login_path):
|
2023-09-09 13:43:23 +00:00
|
|
|
return get_response(request)
|
|
|
|
else:
|
2023-09-09 17:26:36 +00:00
|
|
|
raise PermissionDenied()
|
2023-08-25 19:06:01 +00:00
|
|
|
|
|
|
|
return middleware
|