mirror of
https://github.com/BerriAI/litellm.git
synced 2026-08-28 05:25:59 +00:00
refactor(proxy): extract _apply_non_admin_alias_scope to fix PLR0915
Extracts the non-admin scoping logic from key_aliases into a helper to bring the function under ruff's 50-statement limit. No behavior change.
This commit is contained in:
parent
cb099ee75c
commit
6434010c2b
1 changed files with 39 additions and 26 deletions
|
|
@ -4246,6 +4246,42 @@ async def list_keys(
|
|||
)
|
||||
|
||||
|
||||
async def _apply_non_admin_alias_scope(
|
||||
user_api_key_dict: UserAPIKeyAuth,
|
||||
prisma_client: Any,
|
||||
query_params: List[Any],
|
||||
where_parts: List[str],
|
||||
) -> None:
|
||||
"""Append SQL scope conditions so non-admin users only see aliases for
|
||||
keys they own or keys belonging to teams they are members of."""
|
||||
scope_conditions: List[str] = []
|
||||
if user_api_key_dict.user_id:
|
||||
query_params.append(user_api_key_dict.user_id)
|
||||
scope_conditions.append(f"user_id = ${len(query_params)}")
|
||||
|
||||
# Look up the user's teams from the user table
|
||||
user_teams: List[str] = []
|
||||
if user_api_key_dict.user_id:
|
||||
user_row = await prisma_client.db.litellm_usertable.find_unique(
|
||||
where={"user_id": user_api_key_dict.user_id}
|
||||
)
|
||||
if user_row is not None:
|
||||
user_teams = getattr(user_row, "teams", []) or []
|
||||
|
||||
if user_teams:
|
||||
team_placeholders = ", ".join(
|
||||
f"${len(query_params) + i + 1}" for i in range(len(user_teams))
|
||||
)
|
||||
query_params.extend(user_teams)
|
||||
scope_conditions.append(f"team_id IN ({team_placeholders})")
|
||||
|
||||
if scope_conditions:
|
||||
where_parts.append(f"({' OR '.join(scope_conditions)})")
|
||||
else:
|
||||
# No user_id and no teams — return nothing
|
||||
where_parts.append("FALSE")
|
||||
|
||||
|
||||
@router.get(
|
||||
"/key/aliases",
|
||||
tags=["key management"],
|
||||
|
|
@ -4306,32 +4342,9 @@ async def key_aliases(
|
|||
LitellmUserRoles.PROXY_ADMIN_VIEW_ONLY.value,
|
||||
]
|
||||
if not is_proxy_admin:
|
||||
scope_conditions: List[str] = []
|
||||
if user_api_key_dict.user_id:
|
||||
query_params.append(user_api_key_dict.user_id)
|
||||
scope_conditions.append(f"user_id = ${len(query_params)}")
|
||||
|
||||
# Look up the user's teams from the user table
|
||||
user_teams: List[str] = []
|
||||
if user_api_key_dict.user_id:
|
||||
user_row = await prisma_client.db.litellm_usertable.find_unique(
|
||||
where={"user_id": user_api_key_dict.user_id}
|
||||
)
|
||||
if user_row is not None:
|
||||
user_teams = getattr(user_row, "teams", []) or []
|
||||
|
||||
if user_teams:
|
||||
team_placeholders = ", ".join(
|
||||
f"${len(query_params) + i + 1}" for i in range(len(user_teams))
|
||||
)
|
||||
query_params.extend(user_teams)
|
||||
scope_conditions.append(f"team_id IN ({team_placeholders})")
|
||||
|
||||
if scope_conditions:
|
||||
where_parts.append(f"({' OR '.join(scope_conditions)})")
|
||||
else:
|
||||
# No user_id and no teams — return nothing
|
||||
where_parts.append("FALSE")
|
||||
await _apply_non_admin_alias_scope(
|
||||
user_api_key_dict, prisma_client, query_params, where_parts
|
||||
)
|
||||
|
||||
if search:
|
||||
query_params.append(f"%{search}%")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue