Close the gaps between the documented contract (API-STANDARD, TENANCY, ENTITLEMENTS) and the implementation, and remove committed build artifacts. Security: - verify + process inbound webhooks (HMAC/handler verify, size limit, timestamp tolerance, idempotency via InboundEvent.external_id) - real 2FA login flow (pre-auth challenge; tokens only after verify/recovery) - wire HybridPermission into security/audit views; add API-key rotate and users/<id>/permissions|roles endpoints - tenant-scoped throttling on by default; webhook replay protection - verify MercadoPago webhook signatures - login brute-force guard, configurable password policy, real ALTCHA PoW Correctness: - apply verified billing webhooks idempotently (subscription/entitlement/ invoice/PaymentTransaction); scheduled payment lifecycle jobs - capture audit update diffs automatically; add audit retention purge - working notification retries, per-channel rate limits, log retention - pluggable virus scanner, upload-size limit, pipeline toggle - feature rollout %/environment targeting; settings-driven registrations - workflow guards (instance cap, route depth, self-assignment, clone on re-entry) - wire every previously-dead INFRASYNTH_* setting; drop truly dead ones Delivery: - README + CHANGELOG; CI format check + coverage gate - keep test media out of the tree; untrack .coverage, __pycache__, egg-info, docs/ and invoice artifacts
54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
"""Request/async tenant context (``TENANCY.md`` §3.2, §7).
|
|
|
|
``current_tenant`` is a ``ContextVar`` so the scoped managers can resolve the
|
|
tenant without threading it through every call. It is bound by
|
|
``TenantMiddleware`` for requests and must be bound explicitly by Celery tasks
|
|
and management commands.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Iterator
|
|
from contextlib import contextmanager
|
|
from contextvars import ContextVar, Token
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
if TYPE_CHECKING:
|
|
from .models import Tenant
|
|
|
|
__all__ = [
|
|
"current_tenant",
|
|
"get_current_tenant",
|
|
"set_current_tenant",
|
|
"reset_current_tenant",
|
|
"tenant_context",
|
|
]
|
|
|
|
current_tenant: ContextVar[Tenant | None] = ContextVar("current_tenant", default=None)
|
|
|
|
|
|
def get_current_tenant() -> Tenant | None:
|
|
return current_tenant.get()
|
|
|
|
|
|
def set_current_tenant(tenant: Tenant | None) -> Token:
|
|
return current_tenant.set(tenant)
|
|
|
|
|
|
def reset_current_tenant(token: Token) -> None:
|
|
current_tenant.reset(token)
|
|
|
|
|
|
@contextmanager
|
|
def tenant_context(tenant: Tenant | None) -> Iterator[None]:
|
|
"""Binds ``tenant`` for the duration of the block and always restores it."""
|
|
token = set_current_tenant(tenant)
|
|
try:
|
|
yield
|
|
finally:
|
|
reset_current_tenant(token)
|
|
|
|
|
|
def tenant_id_or_none() -> Any:
|
|
tenant = get_current_tenant()
|
|
return tenant.pk if tenant is not None else None
|