wraps.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import json
  2. import os
  3. from functools import wraps
  4. from flask import abort, request
  5. from flask_login import current_user # type: ignore
  6. from configs import dify_config
  7. from controllers.console.workspace.error import AccountNotInitializedError
  8. from extensions.ext_database import db
  9. from models.model import DifySetup
  10. from services.feature_service import FeatureService, LicenseStatus
  11. from services.operation_service import OperationService
  12. from .error import NotInitValidateError, NotSetupError, UnauthorizedAndForceLogout
  13. def account_initialization_required(view):
  14. @wraps(view)
  15. def decorated(*args, **kwargs):
  16. # check account initialization
  17. account = current_user
  18. if account.status == "uninitialized":
  19. raise AccountNotInitializedError()
  20. return view(*args, **kwargs)
  21. return decorated
  22. def only_edition_cloud(view):
  23. @wraps(view)
  24. def decorated(*args, **kwargs):
  25. if dify_config.EDITION != "CLOUD":
  26. abort(404)
  27. return view(*args, **kwargs)
  28. return decorated
  29. def only_edition_self_hosted(view):
  30. @wraps(view)
  31. def decorated(*args, **kwargs):
  32. if dify_config.EDITION != "SELF_HOSTED":
  33. abort(404)
  34. return view(*args, **kwargs)
  35. return decorated
  36. def cloud_edition_billing_resource_check(resource: str):
  37. def interceptor(view):
  38. @wraps(view)
  39. def decorated(*args, **kwargs):
  40. features = FeatureService.get_features(current_user.current_tenant_id)
  41. if features.billing.enabled:
  42. members = features.members
  43. apps = features.apps
  44. vector_space = features.vector_space
  45. documents_upload_quota = features.documents_upload_quota
  46. annotation_quota_limit = features.annotation_quota_limit
  47. if resource == "members" and 0 < members.limit <= members.size:
  48. abort(403, "The number of members has reached the limit of your subscription.")
  49. elif resource == "apps" and 0 < apps.limit <= apps.size:
  50. abort(403, "The number of apps has reached the limit of your subscription.")
  51. elif resource == "vector_space" and 0 < vector_space.limit <= vector_space.size:
  52. abort(403, "The capacity of the vector space has reached the limit of your subscription.")
  53. elif resource == "documents" and 0 < documents_upload_quota.limit <= documents_upload_quota.size:
  54. # The api of file upload is used in the multiple places,
  55. # so we need to check the source of the request from datasets
  56. source = request.args.get("source")
  57. if source == "datasets":
  58. abort(403, "The number of documents has reached the limit of your subscription.")
  59. else:
  60. return view(*args, **kwargs)
  61. elif resource == "workspace_custom" and not features.can_replace_logo:
  62. abort(403, "The workspace custom feature has reached the limit of your subscription.")
  63. elif resource == "annotation" and 0 < annotation_quota_limit.limit < annotation_quota_limit.size:
  64. abort(403, "The annotation quota has reached the limit of your subscription.")
  65. else:
  66. return view(*args, **kwargs)
  67. return view(*args, **kwargs)
  68. return decorated
  69. return interceptor
  70. def cloud_edition_billing_knowledge_limit_check(resource: str):
  71. def interceptor(view):
  72. @wraps(view)
  73. def decorated(*args, **kwargs):
  74. features = FeatureService.get_features(current_user.current_tenant_id)
  75. if features.billing.enabled:
  76. if resource == "add_segment":
  77. if features.billing.subscription.plan == "sandbox":
  78. abort(
  79. 403,
  80. "To unlock this feature and elevate your Dify experience, please upgrade to a paid plan.",
  81. )
  82. else:
  83. return view(*args, **kwargs)
  84. return view(*args, **kwargs)
  85. return decorated
  86. return interceptor
  87. def cloud_utm_record(view):
  88. @wraps(view)
  89. def decorated(*args, **kwargs):
  90. try:
  91. features = FeatureService.get_features(current_user.current_tenant_id)
  92. if features.billing.enabled:
  93. utm_info = request.cookies.get("utm_info")
  94. if utm_info:
  95. utm_info_dict: dict = json.loads(utm_info)
  96. OperationService.record_utm(current_user.current_tenant_id, utm_info_dict)
  97. except Exception as e:
  98. pass
  99. return view(*args, **kwargs)
  100. return decorated
  101. def setup_required(view):
  102. @wraps(view)
  103. def decorated(*args, **kwargs):
  104. # check setup
  105. if (
  106. dify_config.EDITION == "SELF_HOSTED"
  107. and os.environ.get("INIT_PASSWORD")
  108. and not db.session.query(DifySetup).first()
  109. ):
  110. raise NotInitValidateError()
  111. elif dify_config.EDITION == "SELF_HOSTED" and not db.session.query(DifySetup).first():
  112. raise NotSetupError()
  113. return view(*args, **kwargs)
  114. return decorated
  115. def enterprise_license_required(view):
  116. @wraps(view)
  117. def decorated(*args, **kwargs):
  118. settings = FeatureService.get_system_features()
  119. if settings.license.status in [LicenseStatus.INACTIVE, LicenseStatus.EXPIRED, LicenseStatus.LOST]:
  120. raise UnauthorizedAndForceLogout("Your license is invalid. Please contact your administrator.")
  121. return view(*args, **kwargs)
  122. return decorated