您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

installed_app.py 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import logging
  2. from typing import Any
  3. from flask import request
  4. from flask_login import current_user
  5. from flask_restful import Resource, inputs, marshal_with, reqparse
  6. from sqlalchemy import and_
  7. from werkzeug.exceptions import BadRequest, Forbidden, NotFound
  8. from controllers.console import api
  9. from controllers.console.explore.wraps import InstalledAppResource
  10. from controllers.console.wraps import account_initialization_required, cloud_edition_billing_resource_check
  11. from extensions.ext_database import db
  12. from fields.installed_app_fields import installed_app_list_fields
  13. from libs.datetime_utils import naive_utc_now
  14. from libs.login import login_required
  15. from models import App, InstalledApp, RecommendedApp
  16. from services.account_service import TenantService
  17. from services.app_service import AppService
  18. from services.enterprise.enterprise_service import EnterpriseService
  19. from services.feature_service import FeatureService
  20. logger = logging.getLogger(__name__)
  21. class InstalledAppsListApi(Resource):
  22. @login_required
  23. @account_initialization_required
  24. @marshal_with(installed_app_list_fields)
  25. def get(self):
  26. app_id = request.args.get("app_id", default=None, type=str)
  27. current_tenant_id = current_user.current_tenant_id
  28. if app_id:
  29. installed_apps = (
  30. db.session.query(InstalledApp)
  31. .filter(and_(InstalledApp.tenant_id == current_tenant_id, InstalledApp.app_id == app_id))
  32. .all()
  33. )
  34. else:
  35. installed_apps = db.session.query(InstalledApp).filter(InstalledApp.tenant_id == current_tenant_id).all()
  36. current_user.role = TenantService.get_user_role(current_user, current_user.current_tenant)
  37. installed_app_list: list[dict[str, Any]] = [
  38. {
  39. "id": installed_app.id,
  40. "app": installed_app.app,
  41. "app_owner_tenant_id": installed_app.app_owner_tenant_id,
  42. "is_pinned": installed_app.is_pinned,
  43. "last_used_at": installed_app.last_used_at,
  44. "editable": current_user.role in {"owner", "admin"},
  45. "uninstallable": current_tenant_id == installed_app.app_owner_tenant_id,
  46. }
  47. for installed_app in installed_apps
  48. if installed_app.app is not None
  49. ]
  50. # filter out apps that user doesn't have access to
  51. if FeatureService.get_system_features().webapp_auth.enabled:
  52. user_id = current_user.id
  53. res = []
  54. app_ids = [installed_app["app"].id for installed_app in installed_app_list]
  55. webapp_settings = EnterpriseService.WebAppAuth.batch_get_app_access_mode_by_id(app_ids)
  56. for installed_app in installed_app_list:
  57. webapp_setting = webapp_settings.get(installed_app["app"].id)
  58. if not webapp_setting:
  59. continue
  60. if webapp_setting.access_mode == "sso_verified":
  61. continue
  62. app_code = AppService.get_app_code_by_id(str(installed_app["app"].id))
  63. if EnterpriseService.WebAppAuth.is_user_allowed_to_access_webapp(
  64. user_id=user_id,
  65. app_code=app_code,
  66. ):
  67. res.append(installed_app)
  68. installed_app_list = res
  69. logger.debug(f"installed_app_list: {installed_app_list}, user_id: {user_id}")
  70. installed_app_list.sort(
  71. key=lambda app: (
  72. -app["is_pinned"],
  73. app["last_used_at"] is None,
  74. -app["last_used_at"].timestamp() if app["last_used_at"] is not None else 0,
  75. )
  76. )
  77. return {"installed_apps": installed_app_list}
  78. @login_required
  79. @account_initialization_required
  80. @cloud_edition_billing_resource_check("apps")
  81. def post(self):
  82. parser = reqparse.RequestParser()
  83. parser.add_argument("app_id", type=str, required=True, help="Invalid app_id")
  84. args = parser.parse_args()
  85. recommended_app = db.session.query(RecommendedApp).filter(RecommendedApp.app_id == args["app_id"]).first()
  86. if recommended_app is None:
  87. raise NotFound("App not found")
  88. current_tenant_id = current_user.current_tenant_id
  89. app = db.session.query(App).filter(App.id == args["app_id"]).first()
  90. if app is None:
  91. raise NotFound("App not found")
  92. if not app.is_public:
  93. raise Forbidden("You can't install a non-public app")
  94. installed_app = (
  95. db.session.query(InstalledApp)
  96. .filter(and_(InstalledApp.app_id == args["app_id"], InstalledApp.tenant_id == current_tenant_id))
  97. .first()
  98. )
  99. if installed_app is None:
  100. # todo: position
  101. recommended_app.install_count += 1
  102. new_installed_app = InstalledApp(
  103. app_id=args["app_id"],
  104. tenant_id=current_tenant_id,
  105. app_owner_tenant_id=app.tenant_id,
  106. is_pinned=False,
  107. last_used_at=naive_utc_now(),
  108. )
  109. db.session.add(new_installed_app)
  110. db.session.commit()
  111. return {"message": "App installed successfully"}
  112. class InstalledAppApi(InstalledAppResource):
  113. """
  114. update and delete an installed app
  115. use InstalledAppResource to apply default decorators and get installed_app
  116. """
  117. def delete(self, installed_app):
  118. if installed_app.app_owner_tenant_id == current_user.current_tenant_id:
  119. raise BadRequest("You can't uninstall an app owned by the current tenant")
  120. db.session.delete(installed_app)
  121. db.session.commit()
  122. return {"result": "success", "message": "App uninstalled successfully"}, 204
  123. def patch(self, installed_app):
  124. parser = reqparse.RequestParser()
  125. parser.add_argument("is_pinned", type=inputs.boolean)
  126. args = parser.parse_args()
  127. commit_args = False
  128. if "is_pinned" in args:
  129. installed_app.is_pinned = args["is_pinned"]
  130. commit_args = True
  131. if commit_args:
  132. db.session.commit()
  133. return {"result": "success", "message": "App info updated successfully"}
  134. api.add_resource(InstalledAppsListApi, "/installed-apps")
  135. api.add_resource(InstalledAppApi, "/installed-apps/<uuid:installed_app_id>")