
    g"              	           d Z ddlmZmZmZmZ ddlmZ ddl de	dee
   defdZde
d	eee   eeee   f   f   fd
Z	 dde	dee
   d	efdZy)z
Auth Checks for Organizations
    )DictListOptionalTuple)status)*request_bodyuser_objectroutec           
         |y| j                  dd      }|dk(  ri|j                  t        j                  j                  k7  rBt        d|j                   t        j                  j                  dt        j                        |j                  t        j                  j                  k(  ry|t        j                  j                  v r5t        |      \  }}|j                  9t        d| dt        j                  j                  dt        j                        |5t        d	t        j                  j                  dt        j                        |j                  |      }|9t        d
| dt        j                  j                  dt        j                        |t        j                  j                  k7  rAt        d| d| d| d| t        j                  j                  dt        j                        y|dk(  rt        |      \  }}|j                  t        |j                        dkD  r|8t        d| t        j                  j                  dt        j                        |j                  |      }|t        j                  j                  k7  r>t        d| d| d| t        j                  j                  dt        j                        yyyy)a^  
    Role based access control checks only run if a user is part of an Organization

    Organization Checks:
    ONLY RUN IF user_object.organization_memberships is not None

    1. Only Proxy Admins can access /organization/new
    2. IF route is a LiteLLMRoutes.org_admin_only_routes, then check if user is an Org Admin for that organization

    Norganization_idz/organization/newz8Only proxy admins can create new organizations. You are 	user_role)messagetypeparamcodezTried to access route=z` but you are not a member of any organization. Please contact the proxy admin to request access.zNPassed organization_id is None, please pass an organization_id in your requestzQYou do not have a role within the selected organization. Passed organization_id: z:. Please contact the organization admin to request access.z-You do not have the required role to perform z in Organization z. Your role is z	/team/newr   z|Passed organization_id is None, please specify the organization_id in your request. You are part of multiple organizations: z*You do not have the required role to call )getr   LitellmUserRolesPROXY_ADMINvalueProxyExceptionProxyErrorTypes
auth_errorr   HTTP_401_UNAUTHORIZEDLiteLLMRoutesorg_admin_only_routesget_user_organization_infoorganization_memberships	ORG_ADMINlen)r	   r
   r   passed_organization_id_user_organizations_user_organization_role_mappingr   _user_role_in_passed_orgs           `/var/www/openai/venv/lib/python3.12/site-packages/litellm/proxy/auth/auth_checks_organization.py$organization_role_based_access_checkr&      sJ     ,8,<,<=NPT,U##  $4$@$@$F$FF RS^ShShRij$//55!11	   0 < < B BB 33999&{3 	=< //7 0  8X  Y$//55'11	  ") h$//55'11	  1P0S0S"1
	  k  mC  lD  D~  $//55'11	  (22888 GwN_`v_w  xG  HQ  GR  Rc  dz  c{  |$//55!11	  9 
+	 '{3 	=< 00<K889A=%-$ [  \o  [p  q(3399+55	  (G'J'J&($ (+;+E+E+K+KK$H_w^x  yJ  Ka  Jb  c(3399%55	  L > = 
    returnc                     g }i }| j                   R| j                   D ]C  }|j                  |j                  |j                         |j                  ||j                  <   E ||fS )a  
    Helper function to extract user organization information.

    Args:
        user_object (LiteLLM_UserTable): The user object containing organization memberships.

    Returns:
        Tuple[List[str], Dict[str, Optional[LitellmUserRoles]]]: A tuple containing:
            - List of organization IDs the user is a member of
            - Dictionary mapping organization IDs to user roles
    )r   r   appendr   )r
   r"   r#   _memberships       r%   r   r   r   sr     &(MO#++7&??K**6#**;+F+FGOZOdOd/0K0KL @
  ???r'   Nrequest_datac                     | j                  dd      y|y|j                  y|j                  D ]K  }|j                  | j                  dd      k(  s#|j                  t        j
                  j                  k(  sK y y)zY
    Helper function to check if user is an org admin for the passed organization_id
    r   NFT)r   r   r   r   r   r   r   )r,   r
   r+   s      r%   _user_is_org_adminr.      s     )408++3";;&&,*:*:;Ld*SS$$(8(B(B(H(HH <
 r'   )N)__doc__typingr   r   r   r   fastapir   litellm.proxy._typesdictLiteLLM_UserTablestrr&   r   r   boolr.    r'   r%   <module>r8      s    / .  "cc+,c cL@"@
49d3)9 ::;;<@8 04+, 
r'   