from django.conf import settings
from django.contrib import auth
from django.utils.deprecation import MiddlewareMixin


class AuthenticateSessionMiddleware(MiddlewareMixin):
    """
    Authenticate into a session when accessing Django Admin
    """

    def process_request(self, request):
        """
        Associate user with session if needed
        """
        # request has associated user or path is not an admin page, skip forced authentication
        if (hasattr(request, 'user') and request.user.is_authenticated) or \
            (
                settings.FORCE_SCRIPT_NAME and not request.path_info.lower().
            removeprefix(settings.FORCE_SCRIPT_NAME.lower()).
            startswith("/admin")
        ) or not request.path_info.lower().startswith("/admin"):
            return

        # force authenticate user
        user = auth.authenticate(request)
        if hasattr(user, 'backend'):
            request._cached_user = user
            auth.login(
                request, user, user.backend)
