2023-08-25 19:06:01 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
|
|
|
from storage.authentication import has_permission
|
|
|
|
|
|
|
|
|
|
|
|
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/'
|
|
|
|
'/complete/'
|
|
|
|
]
|
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):
|
|
|
|
return get_response(request)
|
|
|
|
else:
|
|
|
|
raise PermissionDenied()
|
2023-08-25 19:06:01 +00:00
|
|
|
|
|
|
|
return middleware
|