o
    pj@                     @  s^   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddddZG dd dZdS )z8Resolve the active tenant for an incoming Flask request.    )annotations)Request)Session)get_control_plane_engine)TenantNrequestr   trusted_tenant_id
str | Nonereturndict[str, str]c                 C  s   |pd  }|stdt }t|dd?}|t|}|du r'td| |jr/|jdkr6t	d| |j
|j|jp>d|jpF|jpF|j
|jpJdd	W  d   S 1 sWw   Y  dS )
a  Resolve a tenant from the ``X-Tenant-ID`` header.

    Raises:
        ValueError: when the request does not include a tenant identifier.
        LookupError: when the tenant cannot be found.
        PermissionError: when the tenant exists but is inactive.
     zMissing X-Tenant-ID headerT)futureNzTenant not found: activezTenant inactive: )	tenant_idslugdb_namestorage_keyplan)strip
ValueErrorr   r   getr   LookupError	is_activestatusPermissionErrorr   r   data_db_namer   plan_id)r   r   r   enginesessiontenant r    E/var/www/html/flask_server/bridge_platform/tenants/tenant_resolver.pyresolve_tenant   s"   $r"   c                   @  s   e Zd ZdZd	ddZdS )
TenantResolverz4Compatibility wrapper around :func:`resolve_tenant`.r   r   r
   r   c                 C  s   t |S N)r"   )selfr   r    r    r!   resolve_from_request+   s   z#TenantResolver.resolve_from_requestN)r   r   r
   r   )__name__
__module____qualname____doc__r&   r    r    r    r!   r#   (   s    r#   r$   )r   r   r   r	   r
   r   )r*   
__future__r   flaskr   sqlalchemy.ormr   config.control_planer   bridge_platform.tenants.modelsr   r"   r#   r    r    r    r!   <module>   s    