from core.audit import clear_current_user, set_current_user


class CurrentUserAuditMiddleware:
    """
    Stores request user in thread-local state for audit logging.
    """

    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        set_current_user(getattr(request, "user", None))
        try:
            response = self.get_response(request)
        finally:
            clear_current_user()
        return response
