Skip to content

Commit 8acce14

Browse files
committed
refac
1 parent e7e752f commit 8acce14

4 files changed

Lines changed: 13 additions & 24 deletions

File tree

backend/open_webui/functions.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
load_function_module_by_id,
3535
get_function_module_from_cache,
3636
)
37+
from open_webui.utils.access_control import check_model_access
3738

38-
from open_webui.env import GLOBAL_LOG_LEVEL
39+
from open_webui.env import GLOBAL_LOG_LEVEL, BYPASS_MODEL_ACCESS_CONTROL
40+
from open_webui.config import BYPASS_ADMIN_ACCESS_CONTROL
3941

4042
from open_webui.utils.misc import (
4143
add_or_update_system_message,
@@ -260,6 +262,10 @@ async def get_function_params(function_module, form_data, user, extra_params=Non
260262
if model_info.base_model_id:
261263
form_data['model'] = model_info.base_model_id
262264

265+
if not BYPASS_MODEL_ACCESS_CONTROL:
266+
bypass = isinstance(user, UserModel) and user.role == 'admin' and BYPASS_ADMIN_ACCESS_CONTROL
267+
await check_model_access(user if isinstance(user, UserModel) else UserModel(**user), model_info, bypass)
268+
263269
params = model_info.params.model_dump()
264270

265271
if params:

backend/open_webui/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,8 @@ async def chat_completion(
18231823
request.state.metadata = metadata
18241824
form_data['metadata'] = metadata
18251825

1826+
except HTTPException:
1827+
raise
18261828
except Exception as e:
18271829
log.debug(f'Error processing chat metadata: {e}')
18281830
raise HTTPException(

backend/open_webui/routers/ollama.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,29 +1383,9 @@ async def generate_responses(
13831383
if model_info.base_model_id:
13841384
payload['model'] = model_info.base_model_id
13851385

1386-
# Check if user has access to the model
1387-
if user.role == 'user':
1388-
user_group_ids = {group.id for group in await Groups.get_groups_by_member_id(user.id)}
1389-
if not (
1390-
user.id == model_info.user_id
1391-
or await AccessGrants.has_access(
1392-
user_id=user.id,
1393-
resource_type='model',
1394-
resource_id=model_info.id,
1395-
permission='read',
1396-
user_group_ids=user_group_ids,
1397-
)
1398-
):
1399-
raise HTTPException(
1400-
status_code=403,
1401-
detail=ERROR_MESSAGES.MODEL_NOT_FOUND(),
1402-
)
1386+
await check_model_access(user, model_info)
14031387
else:
1404-
if user.role != 'admin':
1405-
raise HTTPException(
1406-
status_code=403,
1407-
detail=ERROR_MESSAGES.MODEL_NOT_FOUND(),
1408-
)
1388+
await check_model_access(user, None)
14091389

14101390
url, url_idx = await get_ollama_url(request, payload['model'], url_idx)
14111391
api_config = request.app.state.config.OLLAMA_API_CONFIGS.get(

backend/open_webui/utils/access_control/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,11 @@ async def check_model_access(
339339
raise HTTPException(status_code=403, detail='Model not found')
340340

341341
# Enforce access on chained base models
342-
if not await has_base_model_chain_access(
342+
if not await has_base_model_access(
343343
user.id, model_info, user_group_ids=user_group_ids
344344
):
345345
raise HTTPException(status_code=403, detail='Model not found')
346346
else:
347347
if user.role != 'admin':
348348
raise HTTPException(status_code=403, detail='Model not found')
349+

0 commit comments

Comments
 (0)