Browse Source

Add admin namespace determination for domain-based redirection

- Introduced a new function `get_admin_namespace` to determine the appropriate admin namespace based on the request domain (Dovoodi or Imam Javad).
- Updated `AdminAccessMiddleware` and `enhanced_auth_middleware` to utilize the new function for redirecting users to the correct admin login page, enhancing access control based on the domain.
- Improved code readability and maintainability by centralizing the logic for admin namespace resolution.
master
mortezaei 3 months ago
parent
commit
16460c3da7
  1. 21
      apps/account/middleware/admin_access.py
  2. 18
      config/enhanced_auth_middleware.py

21
apps/account/middleware/admin_access.py

@ -7,6 +7,20 @@ from django.contrib import messages
from django.utils.translation import gettext_lazy as _
def get_admin_namespace(request):
"""
Determine the admin namespace based on the request domain.
Returns the appropriate admin namespace for use in reverse() calls.
"""
host = request.get_host()
# Check if the request is from Dovoodi domain
if 'dovodi' in host or 'dovoodi' in host:
return 'dovoodi_admin'
else:
return 'imam_javad_admin'
class AdminAccessMiddleware:
"""Middleware برای کنترل دسترسی به admin panel"""
@ -93,8 +107,11 @@ class AdminAccessMiddleware:
def handle_restricted_access(self, request):
"""مدیریت دسترسی محدود شده"""
# Get the correct admin namespace based on domain
admin_namespace = get_admin_namespace(request)
if not request.user.is_authenticated:
return redirect('admin:login')
return redirect(f'{admin_namespace}:login')
# اگر کاربر استاد است، در همان admin panel می‌ماند
if request.user.is_authenticated and request.user.has_role('professor'):
@ -111,4 +128,4 @@ class AdminAccessMiddleware:
request,
_('You do not have permission to access this page.')
)
return redirect('admin:login')
return redirect(f'{admin_namespace}:login')

18
config/enhanced_auth_middleware.py

@ -6,6 +6,21 @@ from django.contrib import messages
User = get_user_model()
def get_admin_namespace(request):
"""
Determine the admin namespace based on the request domain.
Returns the appropriate admin namespace for use in reverse() calls.
"""
host = request.get_host()
# Check if the request is from Dovoodi domain
if 'dovodi' in host or 'dovoodi' in host:
return 'dovoodi_admin'
else:
return 'imam_javad_admin'
def enhanced_auth_middleware(get_response):
"""
Enhanced middleware for API authentication with admin restriction
@ -49,8 +64,9 @@ def enhanced_auth_middleware(get_response):
# For swagger-auth paths, allow access (they handle their own auth)
if '/swagger-auth/' not in request.path:
# Redirect to admin login for other protected paths
admin_namespace = get_admin_namespace(request)
messages.warning(request, 'You must be logged in as a staff member to access API documentation.')
return redirect(f"{reverse('admin:login')}?next={request.path}")
return redirect(f"{reverse(f'{admin_namespace}:login')}?next={request.path}")
# For non-protected API paths, handle normal authentication
elif "/admin/" not in request.path and request.META.get('HTTP_AUTHORIZATION') is None:

Loading…
Cancel
Save