Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

wraps.py 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from collections.abc import Callable
  2. from functools import wraps
  3. from typing import Concatenate, Optional, ParamSpec, TypeVar
  4. from flask_login import current_user
  5. from flask_restx import Resource
  6. from werkzeug.exceptions import NotFound
  7. from controllers.console.explore.error import AppAccessDeniedError
  8. from controllers.console.wraps import account_initialization_required
  9. from extensions.ext_database import db
  10. from libs.login import login_required
  11. from models import InstalledApp
  12. from services.app_service import AppService
  13. from services.enterprise.enterprise_service import EnterpriseService
  14. from services.feature_service import FeatureService
  15. P = ParamSpec("P")
  16. R = TypeVar("R")
  17. T = TypeVar("T")
  18. def installed_app_required(view: Optional[Callable[Concatenate[InstalledApp, P], R]] = None):
  19. def decorator(view: Callable[Concatenate[InstalledApp, P], R]):
  20. @wraps(view)
  21. def decorated(installed_app_id: str, *args: P.args, **kwargs: P.kwargs):
  22. installed_app = (
  23. db.session.query(InstalledApp)
  24. .where(
  25. InstalledApp.id == str(installed_app_id), InstalledApp.tenant_id == current_user.current_tenant_id
  26. )
  27. .first()
  28. )
  29. if installed_app is None:
  30. raise NotFound("Installed app not found")
  31. if not installed_app.app:
  32. db.session.delete(installed_app)
  33. db.session.commit()
  34. raise NotFound("Installed app not found")
  35. return view(installed_app, *args, **kwargs)
  36. return decorated
  37. if view:
  38. return decorator(view)
  39. return decorator
  40. def user_allowed_to_access_app(view: Optional[Callable[Concatenate[InstalledApp, P], R]] = None):
  41. def decorator(view: Callable[Concatenate[InstalledApp, P], R]):
  42. @wraps(view)
  43. def decorated(installed_app: InstalledApp, *args: P.args, **kwargs: P.kwargs):
  44. feature = FeatureService.get_system_features()
  45. if feature.webapp_auth.enabled:
  46. app_id = installed_app.app_id
  47. app_code = AppService.get_app_code_by_id(app_id)
  48. res = EnterpriseService.WebAppAuth.is_user_allowed_to_access_webapp(
  49. user_id=str(current_user.id),
  50. app_code=app_code,
  51. )
  52. if not res:
  53. raise AppAccessDeniedError()
  54. return view(installed_app, *args, **kwargs)
  55. return decorated
  56. if view:
  57. return decorator(view)
  58. return decorator
  59. class InstalledAppResource(Resource):
  60. # must be reversed if there are multiple decorators
  61. method_decorators = [
  62. user_allowed_to_access_app,
  63. installed_app_required,
  64. account_initialization_required,
  65. login_required,
  66. ]