Skip to content

Commit 6dfcc5e

Browse files
authored
fix: application knowledge list (#3367)
1 parent db7b267 commit 6dfcc5e

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

apps/application/serializers/application.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from knowledge.models import Knowledge
3636
from maxkb.conf import PROJECT_DIR
3737
from models_provider.models import Model
38+
from system_manage.models import WorkspaceUserResourcePermission
3839
from tools.models import Tool, ToolScope
3940
from tools.serializers.tool import ToolModelSerializer
4041
from users.models import User
@@ -295,7 +296,7 @@ class Query(serializers.Serializer):
295296
workspace_id = serializers.CharField(required=False, label=_('Workspace ID'))
296297
user_id = serializers.UUIDField(required=True, label=_("User ID"))
297298

298-
def get_query_set(self, instance: Dict, workspace_manage):
299+
def get_query_set(self, instance: Dict, workspace_manage: bool, is_x_pack_ee: bool):
299300
folder_query_set = QuerySet(ApplicationFolder)
300301
application_query_set = QuerySet(Application)
301302
workspace_id = self.data.get('workspace_id')
@@ -317,8 +318,14 @@ def get_query_set(self, instance: Dict, workspace_manage):
317318
application_query_set = application_query_set.filter(desc__contains=desc)
318319
application_custom_sql_query_set = application_query_set
319320
application_query_set = application_query_set.order_by("-update_time")
321+
320322
return {'folder_query_set': folder_query_set,
321-
'application_query_set': application_query_set} if not workspace_manage else {
323+
'application_query_set': application_query_set,
324+
'workspace_user_resource_permission_query_set': QuerySet(WorkspaceUserResourcePermission).filter(
325+
auth_target_type="KNOWLEDGE",
326+
workspace_id=workspace_id,
327+
user_id=user_id)} if (
328+
not workspace_manage and is_x_pack_ee) else {
322329
'folder_query_set': folder_query_set,
323330
'application_query_set': application_query_set,
324331
'application_custom_sql': application_custom_sql_query_set
@@ -336,11 +343,12 @@ def list(self, instance: Dict):
336343
user_id = self.data.get("user_id")
337344
ApplicationQueryRequest(data=instance).is_valid(raise_exception=True)
338345
workspace_manage = is_workspace_manage(user_id, workspace_id)
339-
return native_search(self.get_query_set(instance, workspace_manage),
346+
is_x_pack_ee = self.is_x_pack_ee()
347+
return native_search(self.get_query_set(instance, workspace_manage, is_x_pack_ee),
340348
select_string=get_file_content(
341349
os.path.join(PROJECT_DIR, "apps", "application", 'sql',
342350
'list_application.sql' if workspace_manage else (
343-
'list_application_user_ee.sql' if self.is_x_pack_ee() else 'list_application_user.sql')
351+
'list_application_user_ee.sql' if is_x_pack_ee else 'list_application_user.sql')
344352
)))
345353

346354
def page(self, current_page: int, page_size: int, instance: Dict):
@@ -349,11 +357,12 @@ def page(self, current_page: int, page_size: int, instance: Dict):
349357
workspace_id = self.data.get('workspace_id')
350358
user_id = self.data.get("user_id")
351359
workspace_manage = is_workspace_manage(user_id, workspace_id)
352-
return native_page_search(current_page, page_size, self.get_query_set(instance, workspace_manage),
360+
is_x_pack_ee = self.is_x_pack_ee()
361+
return native_page_search(current_page, page_size, self.get_query_set(instance, workspace_manage, is_x_pack_ee),
353362
get_file_content(
354363
os.path.join(PROJECT_DIR, "apps", "application", 'sql',
355364
'list_application.sql' if workspace_manage else (
356-
'list_application_user_ee.sql' if self.is_x_pack_ee() else 'list_application_user.sql'))),
365+
'list_application_user_ee.sql' if is_x_pack_ee else 'list_application_user.sql'))),
357366
)
358367

359368

apps/application/sql/list_application_user_ee.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ from (select application."id"::text,
1414
from application left join "user" on user_id = "user".id
1515
where "application".id in (select target
1616
from workspace_user_resource_permission
17-
where auth_target_type = 'APPLICATION'
17+
${workspace_user_resource_permission_query_set}
1818
and case
1919
when auth_type = 'ROLE' then
2020
'ROLE' = any (permission_list)

apps/knowledge/serializers/knowledge.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def is_x_pack_ee():
120120
role_permission_mapping_model = DatabaseModelManage.get_model("role_permission_mapping_model")
121121
return workspace_user_role_mapping_model is not None and role_permission_mapping_model is not None
122122

123-
def get_query_set(self):
123+
def get_query_set(self, workspace_manage, is_x_pack_ee):
124124
workspace_id = self.data.get("workspace_id")
125125
query_set_dict = {}
126126
query_set = QuerySet(model=get_dynamics_model({
@@ -157,6 +157,12 @@ def get_query_set(self):
157157
'knowledge.workspace_id': models.CharField(),
158158
})).filter(**{'knowledge.workspace_id': workspace_id})
159159
query_set_dict['folder_query_set'] = folder_query_set
160+
if not workspace_manage and is_x_pack_ee:
161+
query_set_dict['workspace_user_resource_permission_query_set'] = QuerySet(
162+
WorkspaceUserResourcePermission).filter(
163+
auth_target_type="",
164+
workspace_id=workspace_id,
165+
user_id=self.data.get("user_id"))
160166
return query_set_dict
161167

162168
def page(self, current_page: int, page_size: int):
@@ -167,17 +173,18 @@ def page(self, current_page: int, page_size: int):
167173
if not root:
168174
raise serializers.ValidationError(_('Folder not found'))
169175
workspace_manage = is_workspace_manage(self.data.get('user_id'), self.data.get('workspace_id'))
176+
is_x_pack_ee = self.is_x_pack_ee()
170177
return native_page_search(
171178
current_page,
172179
page_size,
173-
self.get_query_set(),
180+
self.get_query_set(workspace_manage, is_x_pack_ee),
174181
select_string=get_file_content(
175182
os.path.join(
176183
PROJECT_DIR,
177184
"apps",
178185
"knowledge", 'sql',
179186
'list_knowledge.sql' if workspace_manage else (
180-
'list_knowledge_user_ee.sql' if self.is_x_pack_ee() else 'list_knowledge_user.sql'
187+
'list_knowledge_user_ee.sql' if is_x_pack_ee else 'list_knowledge_user.sql'
181188
)
182189
)
183190
),
@@ -191,8 +198,9 @@ def list(self):
191198
if not root:
192199
raise serializers.ValidationError(_('Folder not found'))
193200
workspace_manage = is_workspace_manage(self.data.get('user_id'), self.data.get('workspace_id'))
201+
is_x_pack_ee = self.is_x_pack_ee()
194202
return native_search(
195-
self.get_query_set(),
203+
self.get_query_set(workspace_manage, is_x_pack_ee),
196204
select_string=get_file_content(
197205
os.path.join(
198206
PROJECT_DIR,

apps/knowledge/sql/list_knowledge_user_ee.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ FROM (SELECT "temp_knowledge".id::text, "temp_knowledge".name,
2222
FROM knowledge knowledge ${knowledge_custom_sql}
2323
AND "knowledge".id in (select target
2424
from workspace_user_resource_permission
25-
where auth_target_type = 'KNOWLEDGE'
25+
${workspace_user_resource_permission_query_set}
2626
and case
2727
when auth_type = 'ROLE' then
2828
'ROLE' = any (permission_list)

0 commit comments

Comments
 (0)