fix: keep chats shared with an admin readable when ENABLE_ADMIN_CHAT_ACCESS is off (#27127)
get_chat_by_id sent admins down a branch that returned the chat only when ENABLE_ADMIN_CHAT_ACCESS was on, or the chat was internal, and never fell through to the access-grant and shared-folder checks. With the setting off, an admin was therefore denied a chat that had been deliberately shared with them, either directly or through a shared folder, while any non-admin holding the same grant could open it. The admin role removed access the user had been given rather than only closing the admin-only path. Try the admin path first, then let everyone fall through to the grant and folder checks. ENABLE_ADMIN_CHAT_ACCESS=false still closes the admin-only route to other users' chats, and internal chats stay reachable.
This commit is contained in:
1 parent
9b635d8f3d
commit
a35b37adcd
1 file changed
+24
-23
@@ -1203,30 +1203,31 @@ async def compact_chat_by_id(
|
|||||||
async def get_chat_by_id(id: str, user=Depends(get_verified_user), db: AsyncSession = Depends(get_async_session)):
|
async def get_chat_by_id(id: str, user=Depends(get_verified_user), db: AsyncSession = Depends(get_async_session)):
|
||||||
chat = await Chats.get_chat_by_id_and_user_id(id, user.id, db=db)
|
chat = await Chats.get_chat_by_id_and_user_id(id, user.id, db=db)
|
||||||
|
|
||||||
if not chat:
|
if not chat and user.role == 'admin':
|
||||||
# Check if user has access via access grants (shared_chat grants)
|
candidate = await Chats.get_chat_by_id(id, db=db)
|
||||||
if user.role == 'admin':
|
if ENABLE_ADMIN_CHAT_ACCESS or (candidate and is_internal_chat(candidate.meta)):
|
||||||
candidate = await Chats.get_chat_by_id(id, db=db)
|
chat = candidate
|
||||||
if ENABLE_ADMIN_CHAT_ACCESS or (candidate and is_internal_chat(candidate.meta)):
|
|
||||||
chat = candidate
|
|
||||||
else:
|
|
||||||
has_grant = await AccessGrants.has_access(
|
|
||||||
user_id=user.id,
|
|
||||||
resource_type='shared_chat',
|
|
||||||
resource_id=id,
|
|
||||||
permission='read',
|
|
||||||
db=db,
|
|
||||||
)
|
|
||||||
if has_grant:
|
|
||||||
chat = await Chats.get_chat_by_id(id, db=db)
|
|
||||||
|
|
||||||
# Check folder-based access (shared folders)
|
# Access explicitly granted to this user applies to admins too, so an admin
|
||||||
if not chat:
|
# does not lose a chat shared with them when ENABLE_ADMIN_CHAT_ACCESS is off.
|
||||||
candidate = await Chats.get_chat_by_id(id, db=db)
|
if not chat:
|
||||||
if candidate and candidate.folder_id:
|
has_grant = await AccessGrants.has_access(
|
||||||
folder = await Folders.get_folder_by_id(candidate.folder_id, db=db)
|
user_id=user.id,
|
||||||
if folder and await has_folder_access(user.id, folder, 'read', db):
|
resource_type='shared_chat',
|
||||||
chat = candidate
|
resource_id=id,
|
||||||
|
permission='read',
|
||||||
|
db=db,
|
||||||
|
)
|
||||||
|
if has_grant:
|
||||||
|
chat = await Chats.get_chat_by_id(id, db=db)
|
||||||
|
|
||||||
|
# Check folder-based access (shared folders)
|
||||||
|
if not chat:
|
||||||
|
candidate = await Chats.get_chat_by_id(id, db=db)
|
||||||
|
if candidate and candidate.folder_id:
|
||||||
|
folder = await Folders.get_folder_by_id(candidate.folder_id, db=db)
|
||||||
|
if folder and await has_folder_access(user.id, folder, 'read', db):
|
||||||
|
chat = candidate
|
||||||
|
|
||||||
if chat:
|
if chat:
|
||||||
data = ChatResponse(**chat.model_dump()).model_dump()
|
data = ChatResponse(**chat.model_dump()).model_dump()
|
||||||
|
|||||||
Reference in new issue
Block a user