|
82 | 82 | from open_webui.retrieval.web.ydc import search_youcom |
83 | 83 |
|
84 | 84 | from open_webui.retrieval.utils import ( |
| 85 | + filter_accessible_collections, |
85 | 86 | get_content_from_url, |
86 | 87 | get_embedding_function, |
87 | 88 | get_reranking_function, |
@@ -2350,50 +2351,19 @@ async def search_query_with_semaphore(query): |
2350 | 2351 |
|
2351 | 2352 | async def _validate_collection_access(collection_names: list[str], user, access_type: str = 'read') -> None: |
2352 | 2353 | """ |
2353 | | - Prevent users from accessing collections they don't own. |
2354 | | - Enforces ownership on user-memory-*, file-*, and knowledge-base collections. |
2355 | | - Admins bypass this check. |
| 2354 | + Raise 403 if the user lacks access to any of the requested collections. |
| 2355 | + Delegates to the shared filter_accessible_collections utility so the |
| 2356 | + access rules stay in one place. |
2356 | 2357 | """ |
2357 | | - if user.role == 'admin': |
2358 | | - return |
2359 | | - |
2360 | | - for name in collection_names: |
2361 | | - # The 'knowledge-bases' meta-collection stores embedded metadata |
2362 | | - # (names, descriptions, UUIDs) for every KB in the instance. |
2363 | | - # Querying it would let any user enumerate all knowledge bases. |
2364 | | - if name == 'knowledge-bases': |
2365 | | - raise HTTPException( |
2366 | | - status_code=status.HTTP_403_FORBIDDEN, |
2367 | | - detail=ERROR_MESSAGES.ACCESS_PROHIBITED, |
2368 | | - ) |
2369 | | - elif name.startswith('user-memory-') and name != f'user-memory-{user.id}': |
2370 | | - raise HTTPException( |
2371 | | - status_code=status.HTTP_403_FORBIDDEN, |
2372 | | - detail=ERROR_MESSAGES.ACCESS_PROHIBITED, |
2373 | | - ) |
2374 | | - elif name.startswith('file-'): |
2375 | | - file_id = name[len('file-') :] |
2376 | | - if not await has_access_to_file( |
2377 | | - file_id=file_id, |
2378 | | - access_type=access_type, |
2379 | | - user=user, |
2380 | | - ): |
2381 | | - raise HTTPException( |
2382 | | - status_code=status.HTTP_403_FORBIDDEN, |
2383 | | - detail=ERROR_MESSAGES.ACCESS_PROHIBITED, |
2384 | | - ) |
2385 | | - else: |
2386 | | - # Non-prefixed collection names may be knowledge base IDs. |
2387 | | - # Verify the caller has the required permission on the knowledge base. |
2388 | | - knowledge = await Knowledges.get_knowledge_by_id(name) |
2389 | | - if knowledge is not None: |
2390 | | - if not await Knowledges.check_access_by_user_id( |
2391 | | - name, user.id, permission=access_type |
2392 | | - ): |
2393 | | - raise HTTPException( |
2394 | | - status_code=status.HTTP_403_FORBIDDEN, |
2395 | | - detail=ERROR_MESSAGES.ACCESS_PROHIBITED, |
2396 | | - ) |
| 2358 | + requested = set(collection_names) |
| 2359 | + allowed = await filter_accessible_collections(requested, user, access_type=access_type) |
| 2360 | + denied = requested - allowed |
| 2361 | + if denied: |
| 2362 | + raise HTTPException( |
| 2363 | + status_code=status.HTTP_403_FORBIDDEN, |
| 2364 | + detail=ERROR_MESSAGES.ACCESS_PROHIBITED, |
| 2365 | + ) |
| 2366 | + |
2397 | 2367 |
|
2398 | 2368 |
|
2399 | 2369 | class QueryDocForm(BaseModel): |
|
0 commit comments