Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions apps/application/serializers/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from knowledge.models import Knowledge
from maxkb.conf import PROJECT_DIR
from models_provider.models import Model
from system_manage.models import WorkspaceUserResourcePermission
from tools.models import Tool, ToolScope
from tools.serializers.tool import ToolModelSerializer
from users.models import User
Expand Down Expand Up @@ -295,7 +296,7 @@ class Query(serializers.Serializer):
workspace_id = serializers.CharField(required=False, label=_('Workspace 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)
application_query_set = QuerySet(Application)
workspace_id = self.data.get('workspace_id')
Expand All @@ -317,8 +318,14 @@ def get_query_set(self, instance: Dict, workspace_manage):
application_query_set = application_query_set.filter(desc__contains=desc)
application_custom_sql_query_set = application_query_set
application_query_set = application_query_set.order_by("-update_time")

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,
'application_query_set': application_query_set,
'application_custom_sql': application_custom_sql_query_set
Expand All @@ -336,11 +343,12 @@ def list(self, instance: Dict):
user_id = self.data.get("user_id")
ApplicationQueryRequest(data=instance).is_valid(raise_exception=True)
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(
os.path.join(PROJECT_DIR, "apps", "application", 'sql',
'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):
Expand All @@ -349,11 +357,12 @@ def page(self, current_page: int, page_size: int, instance: Dict):
workspace_id = self.data.get('workspace_id')
user_id = self.data.get("user_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(
os.path.join(PROJECT_DIR, "apps", "application", 'sql',
'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'))),
)


Expand Down
2 changes: 1 addition & 1 deletion apps/application/sql/list_application_user_ee.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ from (select application."id"::text,
from application left join "user" on user_id = "user".id
where "application".id in (select target
from workspace_user_resource_permission
where auth_target_type = 'APPLICATION'
${workspace_user_resource_permission_query_set}
and case
when auth_type = 'ROLE' then
'ROLE' = any (permission_list)
Expand Down
16 changes: 12 additions & 4 deletions apps/knowledge/serializers/knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def is_x_pack_ee():
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

def get_query_set(self):
def get_query_set(self, workspace_manage, is_x_pack_ee):
workspace_id = self.data.get("workspace_id")
query_set_dict = {}
query_set = QuerySet(model=get_dynamics_model({
Expand Down Expand Up @@ -157,6 +157,12 @@ def get_query_set(self):
'knowledge.workspace_id': models.CharField(),
})).filter(**{'knowledge.workspace_id': workspace_id})
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

def page(self, current_page: int, page_size: int):
Expand All @@ -167,17 +173,18 @@ def page(self, current_page: int, page_size: int):
if not root:
raise serializers.ValidationError(_('Folder not found'))
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(
current_page,
page_size,
self.get_query_set(),
self.get_query_set(workspace_manage, is_x_pack_ee),
select_string=get_file_content(
os.path.join(
PROJECT_DIR,
"apps",
"knowledge", 'sql',
'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'
)
)
),
Expand All @@ -191,8 +198,9 @@ def list(self):
if not root:
raise serializers.ValidationError(_('Folder not found'))
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(
self.get_query_set(),
self.get_query_set(workspace_manage, is_x_pack_ee),
select_string=get_file_content(
os.path.join(
PROJECT_DIR,
Expand Down
2 changes: 1 addition & 1 deletion apps/knowledge/sql/list_knowledge_user_ee.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ FROM (SELECT "temp_knowledge".id::text, "temp_knowledge".name,
FROM knowledge knowledge ${knowledge_custom_sql}
AND "knowledge".id in (select target
from workspace_user_resource_permission
where auth_target_type = 'KNOWLEDGE'
${workspace_user_resource_permission_query_set}
and case
when auth_type = 'ROLE' then
'ROLE' = any (permission_list)
Expand Down
Loading