Skip to content

Commit 1f69288

Browse files
Add AdminRedirectMiddleware to handle admin site access for non-superusers
1 parent a0f4300 commit 1f69288

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django.conf import settings
2+
from django.http import HttpResponseRedirect
3+
4+
5+
class AdminRedirectMiddleware:
6+
def __init__(self, get_response):
7+
self.get_response = get_response
8+
9+
def __call__(self, request):
10+
# Check if the request is for the admin site and the user is not a superuser
11+
if settings.SECRET_ADMIN_PREFIX in request.path and not request.user.is_superuser:
12+
# Remove the '/admin' prefix and redirect to the CRM site's URL
13+
new_path = request.path.replace(
14+
settings.SECRET_ADMIN_PREFIX, settings.SECRET_CRM_PREFIX)
15+
query_string = request.META.get('QUERY_STRING')
16+
if query_string:
17+
new_path = f"{new_path}?{query_string}"
18+
return HttpResponseRedirect(new_path)
19+
return self.get_response(request)

webcrm/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
'django.contrib.auth.middleware.AuthenticationMiddleware',
132132
'django.contrib.messages.middleware.MessageMiddleware',
133133
'django.middleware.clickjacking.XFrameOptionsMiddleware',
134+
'common.utils.admin_redirect_middleware.AdminRedirectMiddleware',
134135
'common.utils.usermiddleware.UserMiddleware'
135136
]
136137

0 commit comments

Comments
 (0)