refac: run the admin check for an explicit backend index inside the listing handlers

The model listing routes accept the backend index as a path segment and as a query parameter on the index-less sibling route, so the check now runs in the handler and applies to both forms.
This commit is contained in:
Classic298 2026-09-04 14:23:29 +02:00
parent 18a48cffba
commit 69a93ec4b8
2 changed files with 16 additions and 4 deletions

View file

@ -478,13 +478,16 @@ async def get_filtered_models(models, user, db=None):
@router.get('/api/tags')
@router.get('/api/tags/{url_idx}', dependencies=[Depends(get_admin_user)])
@router.get('/api/tags/{url_idx}')
async def get_ollama_tags(
request: Request,
url_idx: int | None = None,
user=Depends(get_verified_user),
):
"""List Ollama model tags, optionally from a specific backend."""
if url_idx is not None and user.role != 'admin':
raise HTTPException(status_code=401, detail=ERROR_MESSAGES.ACCESS_PROHIBITED)
if not await Config.get('ollama.enable'):
raise HTTPException(status_code=503, detail=ERROR_MESSAGES.OLLAMA_API_DISABLED)
@ -541,13 +544,16 @@ async def get_ollama_loaded_models(
@router.get('/api/version')
@router.get('/api/version/{url_idx}', dependencies=[Depends(get_admin_user)])
@router.get('/api/version/{url_idx}')
async def get_ollama_versions(
request: Request,
user=Depends(get_verified_user),
url_idx: int | None = None,
):
"""Return the lowest Ollama version across all configured backends."""
if url_idx is not None and user.role != 'admin':
raise HTTPException(status_code=401, detail=ERROR_MESSAGES.ACCESS_PROHIBITED)
if not await Config.get('ollama.enable'):
return {'version': False}
@ -1479,7 +1485,7 @@ async def generate_responses(
@router.get('/v1/models')
@router.get('/v1/models/{url_idx}', dependencies=[Depends(get_admin_user)])
@router.get('/v1/models/{url_idx}')
async def get_openai_models(
request: Request,
url_idx: int | None = None,
@ -1487,6 +1493,9 @@ async def get_openai_models(
db: AsyncSession = Depends(get_async_session),
) -> dict:
"""List models in the OpenAI-compatible format."""
if url_idx is not None and user.role != 'admin':
raise HTTPException(status_code=401, detail=ERROR_MESSAGES.ACCESS_PROHIBITED)
if url_idx is None:
model_list = await get_all_models(request, user=user)
raw_models = model_list['models']

View file

@ -864,8 +864,11 @@ async def get_all_models(request: Request, user: UserModel) -> dict[str, list]:
@router.get('/models')
@router.get('/models/{url_idx}', dependencies=[Depends(get_admin_user)])
@router.get('/models/{url_idx}')
async def get_models(request: Request, url_idx: int | None = None, user=Depends(get_verified_user)):
if url_idx is not None and user.role != 'admin':
raise HTTPException(status_code=401, detail=ERROR_MESSAGES.ACCESS_PROHIBITED)
if not await Config.get('openai.enable'):
raise HTTPException(status_code=503, detail='OpenAI API is disabled')