wraps.py 6.7 KB

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