diff --git a/apps/account/middleware/admin_access.py b/apps/account/middleware/admin_access.py index ff99fec..00d09dd 100644 --- a/apps/account/middleware/admin_access.py +++ b/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') diff --git a/config/enhanced_auth_middleware.py b/config/enhanced_auth_middleware.py index b64a952..9129b42 100644 --- a/config/enhanced_auth_middleware.py +++ b/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: