Skip to content

Commit b971a2c

Browse files
authored
fix: knowledge list (#3348)
1 parent 37a2041 commit b971a2c

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

apps/application/serializers/application.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ class Query(serializers.Serializer):
294294
workspace_id = serializers.CharField(required=False, label=_('Workspace ID'))
295295
user_id = serializers.UUIDField(required=True, label=_("User ID"))
296296

297-
def get_query_set(self, instance: Dict, workspace_manage: bool):
297+
def get_query_set(self, instance: Dict, workspace_manage: bool, is_x_pack_ee: bool):
298298
folder_query_set = QuerySet(ApplicationFolder)
299299
application_query_set = QuerySet(Application)
300300
workspace_id = self.data.get('workspace_id')
@@ -321,10 +321,11 @@ def get_query_set(self, instance: Dict, workspace_manage: bool):
321321
'folder_query_set': folder_query_set,
322322
'application_query_set': application_query_set,
323323
'application_custom_sql': application_custom_sql_query_set
324-
} if workspace_manage else {'folder_query_set': folder_query_set,
325-
'application_query_set': application_query_set,
326-
'user_query_set': QuerySet(workspace_user_role_mapping_model).filter(
327-
user_id=user_id, workspace_id=workspace_id)}
324+
} if (workspace_manage and is_x_pack_ee) else {'folder_query_set': folder_query_set,
325+
'application_query_set': application_query_set,
326+
'user_query_set': QuerySet(
327+
workspace_user_role_mapping_model).filter(
328+
user_id=user_id, workspace_id=workspace_id)}
328329

329330
@staticmethod
330331
def is_x_pack_ee():
@@ -338,24 +339,26 @@ def list(self, instance: Dict):
338339
user_id = self.data.get("user_id")
339340
ApplicationQueryRequest(data=instance).is_valid(raise_exception=True)
340341
workspace_manage = is_workspace_manage(user_id, workspace_id)
341-
342-
return native_search(self.get_query_set(instance, workspace_manage), select_string=get_file_content(
343-
os.path.join(PROJECT_DIR, "apps", "application", 'sql',
344-
'list_application.sql' if workspace_manage else (
345-
'list_application_user_ee.sql' if self.is_x_pack_ee() else 'list_application_user.sql')
346-
)))
342+
is_x_pack_ee = self.is_x_pack_ee()
343+
return native_search(self.get_query_set(instance, workspace_manage, is_x_pack_ee),
344+
select_string=get_file_content(
345+
os.path.join(PROJECT_DIR, "apps", "application", 'sql',
346+
'list_application.sql' if workspace_manage else (
347+
'list_application_user_ee.sql' if is_x_pack_ee else 'list_application_user.sql')
348+
)))
347349

348350
def page(self, current_page: int, page_size: int, instance: Dict):
349351
self.is_valid(raise_exception=True)
350352
ApplicationQueryRequest(data=instance).is_valid(raise_exception=True)
351353
workspace_id = self.data.get('workspace_id')
352354
user_id = self.data.get("user_id")
353355
workspace_manage = is_workspace_manage(user_id, workspace_id)
354-
return native_page_search(current_page, page_size, self.get_query_set(instance, workspace_manage),
356+
is_x_pack_ee = self.is_x_pack_ee()
357+
return native_page_search(current_page, page_size, self.get_query_set(instance, workspace_manage, is_x_pack_ee),
355358
get_file_content(
356359
os.path.join(PROJECT_DIR, "apps", "application", 'sql',
357360
'list_application.sql' if workspace_manage else (
358-
'list_application_user_ee.sql' if self.is_x_pack_ee() else 'list_application_user.sql'))),
361+
'list_application_user_ee.sql' if is_x_pack_ee else 'list_application_user.sql'))),
359362
)
360363

361364

apps/knowledge/serializers/knowledge.py

Lines changed: 8 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({
@@ -160,6 +160,10 @@ def get_query_set(self):
160160
'knowledge.workspace_id': models.CharField(),
161161
})).filter(**{'knowledge.workspace_id': workspace_id})
162162
query_set_dict['folder_query_set'] = folder_query_set
163+
workspace_user_role_mapping_model = DatabaseModelManage.get_model('workspace_user_role_mapping')
164+
if workspace_manage and is_x_pack_ee:
165+
query_set_dict['user_query_set'] = QuerySet(workspace_user_role_mapping_model).filter(
166+
user_id=self.data.get("user_id"), workspace_id=workspace_id)
163167
return query_set_dict
164168

165169
def page(self, current_page: int, page_size: int):
@@ -170,18 +174,18 @@ def page(self, current_page: int, page_size: int):
170174
if not root:
171175
raise serializers.ValidationError(_('Folder not found'))
172176
workspace_manage = is_workspace_manage(self.data.get('user_id'), self.data.get('workspace_id'))
173-
177+
is_x_pack_ee = self.is_x_pack_ee()
174178
return native_page_search(
175179
current_page,
176180
page_size,
177-
self.get_query_set(),
181+
self.get_query_set(workspace_manage, is_x_pack_ee),
178182
select_string=get_file_content(
179183
os.path.join(
180184
PROJECT_DIR,
181185
"apps",
182186
"knowledge", 'sql',
183187
'list_knowledge.sql' if workspace_manage else (
184-
'list_knowledge_user_ee.sql' if self.is_x_pack_ee() else 'list_knowledge_user.sql'
188+
'list_knowledge_user_ee.sql' if is_x_pack_ee else 'list_knowledge_user.sql'
185189
)
186190
)
187191
),

apps/knowledge/sql/list_knowledge_user_ee.sql

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ FROM (SELECT "temp_knowledge".id::text, "temp_knowledge".name,
2020
"document_temp".document_count
2121
FROM (SELECT knowledge.*
2222
FROM knowledge knowledge ${knowledge_custom_sql}
23-
AND id in (select target
23+
AND "knowledge".id in (select target
2424
from workspace_user_resource_permission
2525
where auth_target_type = 'KNOWLEDGE'
2626
and case
2727
when auth_type = 'ROLE' then
28-
'KNOWLEDGE_READ' in (select permission_id
29-
from role_permission
30-
where role_id in (select role_id
31-
from user_role_relation))
28+
'KNOWLEDGE:READ' in (select (case when user_role_relation.role_id = any (array ['USER']) THEN 'KNOWLEDGE:READ' else role_permission.permission_id END)
29+
from role_permission role_permission
30+
right join user_role_relation user_role_relation
31+
on user_role_relation.role_id=role_permission.role_id
32+
${user_query_set})
3233
else
3334
'VIEW' = any (permission_list)
3435
end

0 commit comments

Comments
 (0)