fix: access control behaviour

This commit is contained in:
Timothy Jaeryang Baek 2024-11-17 02:51:57 -08:00
parent 892f6ba42b
commit 1d4c3a8c58
3 changed files with 70 additions and 57 deletions

View File

@ -358,7 +358,7 @@ async def get_ollama_tags(
for model in models.get("models", []):
model_info = Models.get_model_by_id(model["model"])
if model_info:
if has_access(
if user.id == model_info.user_id or has_access(
user.id, type="read", access_control=model_info.access_control
):
filtered_models.append(model)
@ -366,7 +366,6 @@ async def get_ollama_tags(
filtered_models.append(model)
models["models"] = filtered_models
return models
@ -953,8 +952,12 @@ async def generate_chat_completion(
payload = apply_model_system_prompt_to_body(params, payload, user)
# Check if user has access to the model
if not bypass_filter and user.role == "user" and not has_access(
if not bypass_filter and user.role == "user":
if not (
user.id == model_info.user_id
or has_access(
user.id, type="read", access_control=model_info.access_control
)
):
raise HTTPException(
status_code=403,
@ -964,7 +967,6 @@ async def generate_chat_completion(
if ":" not in payload["model"]:
payload["model"] = f"{payload['model']}:latest"
url = await get_ollama_url(url_idx, payload["model"])
log.info(f"url: {url}")
log.debug(f"generate_chat_completion() - 2.payload = {payload}")
@ -1026,7 +1028,6 @@ async def generate_openai_chat_completion(
if ":" not in model_id:
model_id = f"{model_id}:latest"
model_info = Models.get_model_by_id(model_id)
if model_info:
if model_info.base_model_id:
@ -1039,8 +1040,12 @@ async def generate_openai_chat_completion(
payload = apply_model_system_prompt_to_body(params, payload, user)
# Check if user has access to the model
if user.role == "user" and not has_access(
if user.role == "user":
if not (
user.id == model_info.user_id
or has_access(
user.id, type="read", access_control=model_info.access_control
)
):
raise HTTPException(
status_code=403,
@ -1118,14 +1123,13 @@ async def get_openai_models(
detail=error_detail,
)
if user.role == "user":
# Filter models based on user access control
filtered_models = []
for model in models:
model_info = Models.get_model_by_id(model["id"])
if model_info:
if has_access(
if user.id == model_info.user_id or has_access(
user.id, type="read", access_control=model_info.access_control
):
filtered_models.append(model)
@ -1133,7 +1137,6 @@ async def get_openai_models(
filtered_models.append(model)
models = filtered_models
return {
"data": models,
"object": "list",

View File

@ -420,7 +420,7 @@ async def get_models(url_idx: Optional[int] = None, user=Depends(get_verified_us
for model in models.get("data", []):
model_info = Models.get_model_by_id(model["id"])
if model_info:
if has_access(
if user.id == model_info.user_id or has_access(
user.id, type="read", access_control=model_info.access_control
):
filtered_models.append(model)
@ -501,8 +501,12 @@ async def generate_chat_completion(
payload = apply_model_system_prompt_to_body(params, payload, user)
# Check if user has access to the model
if not bypass_filter and user.role == "user" and not has_access(
if not bypass_filter and user.role == "user":
if not (
user.id == model_info.user_id
or has_access(
user.id, type="read", access_control=model_info.access_control
)
):
raise HTTPException(
status_code=403,

View File

@ -557,8 +557,11 @@ class ChatCompletionMiddleware(BaseHTTPMiddleware):
model_info = Models.get_model_by_id(model["id"])
if user.role == "user":
if model_info and not has_access(
if model_info and not (
user.id == model_info.user_id
or has_access(
user.id, type="read", access_control=model_info.access_control
)
):
return JSONResponse(
status_code=status.HTTP_403_FORBIDDEN,
@ -1106,7 +1109,7 @@ async def get_models(user=Depends(get_verified_user)):
for model in models:
model_info = Models.get_model_by_id(model["id"])
if model_info:
if has_access(
if user.id == model_info.user_id or has_access(
user.id, type="read", access_control=model_info.access_control
):
filtered_models.append(model)
@ -1144,8 +1147,11 @@ async def generate_chat_completions(
# Check if user has access to the model
if user.role == "user":
model_info = Models.get_model_by_id(model_id)
if not has_access(
if not (
user.id == model_info.user_id
or has_access(
user.id, type="read", access_control=model_info.access_control
)
):
raise HTTPException(
status_code=403,