Skip to content

Commit 0743754

Browse files
authored
fix: The conversation user is not authorized to use (#3581)
1 parent 958924e commit 0743754

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

apps/application/serializers/application_chat_record.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,10 @@ def reset_chat_record(chat_record, show_source, show_exec):
125125
'paragraph_list') or [])
126126

127127
if item.get('type') == 'reranker-node' and item.get('show_knowledge', False):
128-
paragraph_list = paragraph_list + [rl.get('metadata') for rl in item.get('result_list') if
129-
'document_id' in rl.get('metadata') and 'knowledge_id' in rl.get(
130-
'metadata')]
128+
paragraph_list = paragraph_list + [rl.get('metadata') for rl in (item.get('result_list') or []) if
129+
'document_id' in (rl.get('metadata') or {}) and 'knowledge_id' in (
130+
rl.get(
131+
'metadata') or {})]
131132
paragraph_list = list({p.get('id'): p for p in paragraph_list}.values())
132133
knowledge_list = knowledge_list + [{'id': knowledge_id, **knowledge} for knowledge_id, knowledge in
133134
reduce(lambda x, y: {**x, **y},

apps/chat/api/chat_authentication_api.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,4 @@ def get_parameters():
5252
class ChatOpenAPI(APIMixin):
5353
@staticmethod
5454
def get_parameters():
55-
return [OpenApiParameter(
56-
name="workspace_id",
57-
description="工作空间id",
58-
type=OpenApiTypes.STR,
59-
location='path',
60-
required=True,
61-
),
62-
OpenApiParameter(
63-
name="application_id",
64-
description="应用id",
65-
type=OpenApiTypes.STR,
66-
location='path',
67-
required=True,
68-
)]
55+
return []

apps/chat/serializers/chat.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
ChatUserType, ApplicationChatUserStats, ApplicationAccessToken, ChatRecord, Chat, ApplicationVersion
3030
from application.serializers.application import ApplicationOperateSerializer
3131
from application.serializers.common import ChatInfo
32+
from common.database_model_manage.database_model_manage import DatabaseModelManage
3233
from common.exception.app_exception import AppApiException, AppChatNumOutOfBoundsFailed, ChatException
3334
from common.handle.base_to_response import BaseToResponse
3435
from common.handle.impl.response.openai_to_response import OpenaiToResponse
@@ -308,13 +309,23 @@ def chat_work_flow(self, chat_info: ChatInfo, instance: dict, base_to_response):
308309
r = work_flow_manage.run()
309310
return r
310311

312+
def is_valid_chat_user(self):
313+
chat_user_id = self.data.get('chat_user_id')
314+
application_id = self.data.get('application_id')
315+
is_auth_chat_user = DatabaseModelManage.get_model("is_auth_chat_user")
316+
if self.chat_user_type == ChatUserType.CHAT_USER.value and is_auth_chat_user:
317+
is_auth = is_auth_chat_user(chat_user_id, application_id)
318+
if not is_auth:
319+
raise ChatException(500, _("The chat user is not authorized."))
320+
311321
def chat(self, instance: dict, base_to_response: BaseToResponse = SystemToResponse()):
312322
super().is_valid(raise_exception=True)
313323
ChatMessageSerializers(data=instance).is_valid(raise_exception=True)
314324
chat_info = self.get_chat_info()
315325
chat_info.get_application()
316326
chat_info.get_chat_user()
317327
self.is_valid_chat_id(chat_info)
328+
self.is_valid_chat_user()
318329
if chat_info.application.type == ApplicationTypeChoices.SIMPLE:
319330
self.is_valid_application_simple(raise_exception=True, chat_info=chat_info)
320331
return self.chat_simple(chat_info, instance, base_to_response)

0 commit comments

Comments
 (0)