You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

installed_app.py 7.4KB

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