|
@@ -5,6 +5,7 @@ from typing import Optional, cast
|
|
|
|
|
|
from flask_login import current_user # type: ignore
|
|
|
from flask_sqlalchemy.pagination import Pagination
|
|
|
+from sqlalchemy import literal, select, union
|
|
|
|
|
|
from configs import dify_config
|
|
|
from constants.model_template import default_app_templates
|
|
@@ -25,6 +26,7 @@ from tasks.remove_app_and_related_data_task import remove_app_and_related_data_t
|
|
|
|
|
|
|
|
|
class AppService:
|
|
|
+
|
|
|
def get_paginate_apps(self, user_id: str, tenant_id: str, args: dict) -> Pagination | None:
|
|
|
"""
|
|
|
Get app list with pagination
|
|
@@ -60,6 +62,66 @@ class AppService:
|
|
|
else:
|
|
|
return None
|
|
|
|
|
|
+ # 初始化 main_query
|
|
|
+ main_query = db.select(App).where(*filters)
|
|
|
+ # 添加 auth_type 参数
|
|
|
+ auth_type = args.get("auth_type")
|
|
|
+ print('auth_type', auth_type)
|
|
|
+ # 根据 auth_type 构建不同的查询
|
|
|
+ if auth_type is None:
|
|
|
+ query1 = db.select(App.id).where(
|
|
|
+ *filters,
|
|
|
+ App.created_by == user_id
|
|
|
+ )
|
|
|
+ query2 = db.select(App.id).join(
|
|
|
+ Account, App.dept_id == Account.dept_id
|
|
|
+ ).where(
|
|
|
+ *filters,
|
|
|
+ App.edit_auth == 2,
|
|
|
+ Account.id == user_id
|
|
|
+ )
|
|
|
+ query3 = db.select(App.id).join(
|
|
|
+ AppPermissionAll, App.id == AppPermissionAll.app_id
|
|
|
+ ).where(
|
|
|
+ *filters,
|
|
|
+ AppPermissionAll.has_read_permission == True,
|
|
|
+ AppPermissionAll.account_id == user_id
|
|
|
+ )
|
|
|
+ union_query = union(query1, query2, query3).subquery()
|
|
|
+ main_query = db.select(App).where(App.id.in_(select(union_query.c.id)))
|
|
|
+ elif auth_type == "1":
|
|
|
+ # 只查询部门编辑权限
|
|
|
+ main_query = db.select(App).where(
|
|
|
+ *filters,
|
|
|
+ App.created_by == user_id
|
|
|
+ )
|
|
|
+ elif auth_type == "2":
|
|
|
+ # 只查询部门编辑权限
|
|
|
+ main_query = db.select(App).join(
|
|
|
+ Account, App.dept_id == Account.dept_id
|
|
|
+ ).where(
|
|
|
+ *filters,
|
|
|
+ App.edit_auth == 2,
|
|
|
+ Account.id == user_id
|
|
|
+ )
|
|
|
+ elif auth_type == "3":
|
|
|
+ # 只查询授权编辑权限
|
|
|
+ main_query = db.select(App).join(
|
|
|
+ AppPermissionAll, App.id == AppPermissionAll.app_id
|
|
|
+ ).where(
|
|
|
+ *filters,
|
|
|
+ AppPermissionAll.has_read_permission == True,
|
|
|
+ AppPermissionAll.account_id == user_id
|
|
|
+ )
|
|
|
+ if args.get("creator"):
|
|
|
+ main_query = main_query.where(App.created_by == args.get("creator"))
|
|
|
+ if args.get("creator_dept"):
|
|
|
+ main_query = main_query.join(Account, App.created_by == Account.id)
|
|
|
+ main_query = main_query.where(Account.dept_id == args.get("creator_dept"))
|
|
|
+
|
|
|
+ print(str(main_query))
|
|
|
+
|
|
|
+ # 执行分页查询
|
|
|
app_models = db.paginate(
|
|
|
db.select(App).where(*filters).order_by(App.created_at.desc()),
|
|
|
page=args["page"],
|