fix: application knowledge list (#3367)
This commit is contained in:
parent
db7b267326
commit
6dfcc5e54d
@ -35,6 +35,7 @@ from common.utils.common import get_file_content, valid_license, restricted_load
|
|||||||
from knowledge.models import Knowledge
|
from knowledge.models import Knowledge
|
||||||
from maxkb.conf import PROJECT_DIR
|
from maxkb.conf import PROJECT_DIR
|
||||||
from models_provider.models import Model
|
from models_provider.models import Model
|
||||||
|
from system_manage.models import WorkspaceUserResourcePermission
|
||||||
from tools.models import Tool, ToolScope
|
from tools.models import Tool, ToolScope
|
||||||
from tools.serializers.tool import ToolModelSerializer
|
from tools.serializers.tool import ToolModelSerializer
|
||||||
from users.models import User
|
from users.models import User
|
||||||
@ -295,7 +296,7 @@ class Query(serializers.Serializer):
|
|||||||
workspace_id = serializers.CharField(required=False, label=_('Workspace ID'))
|
workspace_id = serializers.CharField(required=False, label=_('Workspace ID'))
|
||||||
user_id = serializers.UUIDField(required=True, label=_("User ID"))
|
user_id = serializers.UUIDField(required=True, label=_("User ID"))
|
||||||
|
|
||||||
def get_query_set(self, instance: Dict, workspace_manage):
|
def get_query_set(self, instance: Dict, workspace_manage: bool, is_x_pack_ee: bool):
|
||||||
folder_query_set = QuerySet(ApplicationFolder)
|
folder_query_set = QuerySet(ApplicationFolder)
|
||||||
application_query_set = QuerySet(Application)
|
application_query_set = QuerySet(Application)
|
||||||
workspace_id = self.data.get('workspace_id')
|
workspace_id = self.data.get('workspace_id')
|
||||||
@ -317,8 +318,14 @@ class Query(serializers.Serializer):
|
|||||||
application_query_set = application_query_set.filter(desc__contains=desc)
|
application_query_set = application_query_set.filter(desc__contains=desc)
|
||||||
application_custom_sql_query_set = application_query_set
|
application_custom_sql_query_set = application_query_set
|
||||||
application_query_set = application_query_set.order_by("-update_time")
|
application_query_set = application_query_set.order_by("-update_time")
|
||||||
|
|
||||||
return {'folder_query_set': folder_query_set,
|
return {'folder_query_set': folder_query_set,
|
||||||
'application_query_set': application_query_set} if not workspace_manage else {
|
'application_query_set': application_query_set,
|
||||||
|
'workspace_user_resource_permission_query_set': QuerySet(WorkspaceUserResourcePermission).filter(
|
||||||
|
auth_target_type="KNOWLEDGE",
|
||||||
|
workspace_id=workspace_id,
|
||||||
|
user_id=user_id)} if (
|
||||||
|
not workspace_manage and is_x_pack_ee) else {
|
||||||
'folder_query_set': folder_query_set,
|
'folder_query_set': folder_query_set,
|
||||||
'application_query_set': application_query_set,
|
'application_query_set': application_query_set,
|
||||||
'application_custom_sql': application_custom_sql_query_set
|
'application_custom_sql': application_custom_sql_query_set
|
||||||
@ -336,11 +343,12 @@ class Query(serializers.Serializer):
|
|||||||
user_id = self.data.get("user_id")
|
user_id = self.data.get("user_id")
|
||||||
ApplicationQueryRequest(data=instance).is_valid(raise_exception=True)
|
ApplicationQueryRequest(data=instance).is_valid(raise_exception=True)
|
||||||
workspace_manage = is_workspace_manage(user_id, workspace_id)
|
workspace_manage = is_workspace_manage(user_id, workspace_id)
|
||||||
return native_search(self.get_query_set(instance, workspace_manage),
|
is_x_pack_ee = self.is_x_pack_ee()
|
||||||
|
return native_search(self.get_query_set(instance, workspace_manage, is_x_pack_ee),
|
||||||
select_string=get_file_content(
|
select_string=get_file_content(
|
||||||
os.path.join(PROJECT_DIR, "apps", "application", 'sql',
|
os.path.join(PROJECT_DIR, "apps", "application", 'sql',
|
||||||
'list_application.sql' if workspace_manage else (
|
'list_application.sql' if workspace_manage else (
|
||||||
'list_application_user_ee.sql' if self.is_x_pack_ee() else 'list_application_user.sql')
|
'list_application_user_ee.sql' if is_x_pack_ee else 'list_application_user.sql')
|
||||||
)))
|
)))
|
||||||
|
|
||||||
def page(self, current_page: int, page_size: int, instance: Dict):
|
def page(self, current_page: int, page_size: int, instance: Dict):
|
||||||
@ -349,11 +357,12 @@ class Query(serializers.Serializer):
|
|||||||
workspace_id = self.data.get('workspace_id')
|
workspace_id = self.data.get('workspace_id')
|
||||||
user_id = self.data.get("user_id")
|
user_id = self.data.get("user_id")
|
||||||
workspace_manage = is_workspace_manage(user_id, workspace_id)
|
workspace_manage = is_workspace_manage(user_id, workspace_id)
|
||||||
return native_page_search(current_page, page_size, self.get_query_set(instance, workspace_manage),
|
is_x_pack_ee = self.is_x_pack_ee()
|
||||||
|
return native_page_search(current_page, page_size, self.get_query_set(instance, workspace_manage, is_x_pack_ee),
|
||||||
get_file_content(
|
get_file_content(
|
||||||
os.path.join(PROJECT_DIR, "apps", "application", 'sql',
|
os.path.join(PROJECT_DIR, "apps", "application", 'sql',
|
||||||
'list_application.sql' if workspace_manage else (
|
'list_application.sql' if workspace_manage else (
|
||||||
'list_application_user_ee.sql' if self.is_x_pack_ee() else 'list_application_user.sql'))),
|
'list_application_user_ee.sql' if is_x_pack_ee else 'list_application_user.sql'))),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -14,7 +14,7 @@ from (select application."id"::text,
|
|||||||
from application left join "user" on user_id = "user".id
|
from application left join "user" on user_id = "user".id
|
||||||
where "application".id in (select target
|
where "application".id in (select target
|
||||||
from workspace_user_resource_permission
|
from workspace_user_resource_permission
|
||||||
where auth_target_type = 'APPLICATION'
|
${workspace_user_resource_permission_query_set}
|
||||||
and case
|
and case
|
||||||
when auth_type = 'ROLE' then
|
when auth_type = 'ROLE' then
|
||||||
'ROLE' = any (permission_list)
|
'ROLE' = any (permission_list)
|
||||||
|
|||||||
@ -120,7 +120,7 @@ class KnowledgeSerializer(serializers.Serializer):
|
|||||||
role_permission_mapping_model = DatabaseModelManage.get_model("role_permission_mapping_model")
|
role_permission_mapping_model = DatabaseModelManage.get_model("role_permission_mapping_model")
|
||||||
return workspace_user_role_mapping_model is not None and role_permission_mapping_model is not None
|
return workspace_user_role_mapping_model is not None and role_permission_mapping_model is not None
|
||||||
|
|
||||||
def get_query_set(self):
|
def get_query_set(self, workspace_manage, is_x_pack_ee):
|
||||||
workspace_id = self.data.get("workspace_id")
|
workspace_id = self.data.get("workspace_id")
|
||||||
query_set_dict = {}
|
query_set_dict = {}
|
||||||
query_set = QuerySet(model=get_dynamics_model({
|
query_set = QuerySet(model=get_dynamics_model({
|
||||||
@ -157,6 +157,12 @@ class KnowledgeSerializer(serializers.Serializer):
|
|||||||
'knowledge.workspace_id': models.CharField(),
|
'knowledge.workspace_id': models.CharField(),
|
||||||
})).filter(**{'knowledge.workspace_id': workspace_id})
|
})).filter(**{'knowledge.workspace_id': workspace_id})
|
||||||
query_set_dict['folder_query_set'] = folder_query_set
|
query_set_dict['folder_query_set'] = folder_query_set
|
||||||
|
if not workspace_manage and is_x_pack_ee:
|
||||||
|
query_set_dict['workspace_user_resource_permission_query_set'] = QuerySet(
|
||||||
|
WorkspaceUserResourcePermission).filter(
|
||||||
|
auth_target_type="",
|
||||||
|
workspace_id=workspace_id,
|
||||||
|
user_id=self.data.get("user_id"))
|
||||||
return query_set_dict
|
return query_set_dict
|
||||||
|
|
||||||
def page(self, current_page: int, page_size: int):
|
def page(self, current_page: int, page_size: int):
|
||||||
@ -167,17 +173,18 @@ class KnowledgeSerializer(serializers.Serializer):
|
|||||||
if not root:
|
if not root:
|
||||||
raise serializers.ValidationError(_('Folder not found'))
|
raise serializers.ValidationError(_('Folder not found'))
|
||||||
workspace_manage = is_workspace_manage(self.data.get('user_id'), self.data.get('workspace_id'))
|
workspace_manage = is_workspace_manage(self.data.get('user_id'), self.data.get('workspace_id'))
|
||||||
|
is_x_pack_ee = self.is_x_pack_ee()
|
||||||
return native_page_search(
|
return native_page_search(
|
||||||
current_page,
|
current_page,
|
||||||
page_size,
|
page_size,
|
||||||
self.get_query_set(),
|
self.get_query_set(workspace_manage, is_x_pack_ee),
|
||||||
select_string=get_file_content(
|
select_string=get_file_content(
|
||||||
os.path.join(
|
os.path.join(
|
||||||
PROJECT_DIR,
|
PROJECT_DIR,
|
||||||
"apps",
|
"apps",
|
||||||
"knowledge", 'sql',
|
"knowledge", 'sql',
|
||||||
'list_knowledge.sql' if workspace_manage else (
|
'list_knowledge.sql' if workspace_manage else (
|
||||||
'list_knowledge_user_ee.sql' if self.is_x_pack_ee() else 'list_knowledge_user.sql'
|
'list_knowledge_user_ee.sql' if is_x_pack_ee else 'list_knowledge_user.sql'
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
@ -191,8 +198,9 @@ class KnowledgeSerializer(serializers.Serializer):
|
|||||||
if not root:
|
if not root:
|
||||||
raise serializers.ValidationError(_('Folder not found'))
|
raise serializers.ValidationError(_('Folder not found'))
|
||||||
workspace_manage = is_workspace_manage(self.data.get('user_id'), self.data.get('workspace_id'))
|
workspace_manage = is_workspace_manage(self.data.get('user_id'), self.data.get('workspace_id'))
|
||||||
|
is_x_pack_ee = self.is_x_pack_ee()
|
||||||
return native_search(
|
return native_search(
|
||||||
self.get_query_set(),
|
self.get_query_set(workspace_manage, is_x_pack_ee),
|
||||||
select_string=get_file_content(
|
select_string=get_file_content(
|
||||||
os.path.join(
|
os.path.join(
|
||||||
PROJECT_DIR,
|
PROJECT_DIR,
|
||||||
|
|||||||
@ -22,7 +22,7 @@ FROM (SELECT "temp_knowledge".id::text, "temp_knowledge".name,
|
|||||||
FROM knowledge knowledge ${knowledge_custom_sql}
|
FROM knowledge knowledge ${knowledge_custom_sql}
|
||||||
AND "knowledge".id in (select target
|
AND "knowledge".id in (select target
|
||||||
from workspace_user_resource_permission
|
from workspace_user_resource_permission
|
||||||
where auth_target_type = 'KNOWLEDGE'
|
${workspace_user_resource_permission_query_set}
|
||||||
and case
|
and case
|
||||||
when auth_type = 'ROLE' then
|
when auth_type = 'ROLE' then
|
||||||
'ROLE' = any (permission_list)
|
'ROLE' = any (permission_list)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user