mirror of
https://github.com/open-webui/open-webui.git
synced 2026-12-05 11:07:55 -05:00
45 lines
2.2 KiB
Python
45 lines
2.2 KiB
Python
from open_webui.models.access_grants import AccessGrants
|
|
from open_webui.models.folders import FolderModel, Folders
|
|
from open_webui.models.groups import Groups
|
|
from open_webui.models.tasks import TaskModel
|
|
from open_webui.models.teams import Teams
|
|
from open_webui.utils.access_control.folders import has_folder_access
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
async def has_task_access(user_id: str, task: TaskModel, permission: str, db: AsyncSession | None = None) -> bool:
|
|
"""A task is open to whoever has the same access to its folder."""
|
|
folder = await Folders.get_folder_by_id(task.folder_id, db=db)
|
|
return bool(folder) and await has_folder_access(user_id, folder, permission, db)
|
|
|
|
|
|
async def folder_user_ids(folder: FolderModel, permission: str, db: AsyncSession | None = None) -> set[str] | None:
|
|
"""Everyone with this access to the folder: its owner plus whoever its grants, and its parents', reach.
|
|
|
|
Read access counts write grants too. None means a grant is open to everyone.
|
|
"""
|
|
permissions = {'write'} if permission == 'write' else {'read', 'write'}
|
|
user_ids, group_ids, team_ids = set(), set(), set()
|
|
seen = set()
|
|
while folder and folder.id not in seen:
|
|
seen.add(folder.id)
|
|
user_ids.add(folder.user_id)
|
|
for grant in await AccessGrants.get_grants_by_resource('folder', folder.id, db=db):
|
|
if grant.permission not in permissions:
|
|
continue
|
|
if grant.principal_type == 'user':
|
|
if grant.principal_id == '*':
|
|
return None
|
|
user_ids.add(grant.principal_id)
|
|
elif grant.principal_type == 'group':
|
|
group_ids.add(grant.principal_id)
|
|
elif grant.principal_type == 'team':
|
|
team_ids.add(grant.principal_id)
|
|
folder = await Folders.get_folder_by_id(folder.parent_id, db=db) if folder.parent_id else None
|
|
if group_ids:
|
|
members = await Groups.get_group_user_ids_by_ids(list(group_ids), db=db, include_inherited=True)
|
|
user_ids.update(user_id for ids in members.values() for user_id in ids)
|
|
if team_ids:
|
|
user_ids.update(await Teams.get_user_ids_by_team_ids(list(team_ids), db=db))
|
|
return user_ids
|