Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

wraps.py 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import contextlib
  2. import json
  3. import os
  4. import time
  5. from functools import wraps
  6. from flask import abort, request
  7. from flask_login import current_user
  8. from configs import dify_config
  9. from controllers.console.workspace.error import AccountNotInitializedError
  10. from extensions.ext_database import db
  11. from extensions.ext_redis import redis_client
  12. from models.account import AccountStatus
  13. from models.dataset import RateLimitLog
  14. from models.model import DifySetup
  15. from services.feature_service import FeatureService, LicenseStatus
  16. from services.operation_service import OperationService
  17. from .error import NotInitValidateError, NotSetupError, UnauthorizedAndForceLogout
  18. def account_initialization_required(view):
  19. @wraps(view)
  20. def decorated(*args, **kwargs):
  21. # check account initialization
  22. account = current_user
  23. if account.status == AccountStatus.UNINITIALIZED:
  24. raise AccountNotInitializedError()
  25. return view(*args, **kwargs)
  26. return decorated
  27. def only_edition_cloud(view):
  28. @wraps(view)
  29. def decorated(*args, **kwargs):
  30. if dify_config.EDITION != "CLOUD":
  31. abort(404)
  32. return view(*args, **kwargs)
  33. return decorated
  34. def only_edition_enterprise(view):
  35. @wraps(view)
  36. def decorated(*args, **kwargs):
  37. if not dify_config.ENTERPRISE_ENABLED:
  38. abort(404)
  39. return view(*args, **kwargs)
  40. return decorated
  41. def only_edition_self_hosted(view):
  42. @wraps(view)
  43. def decorated(*args, **kwargs):
  44. if dify_config.EDITION != "SELF_HOSTED":
  45. abort(404)
  46. return view(*args, **kwargs)
  47. return decorated
  48. def cloud_edition_billing_enabled(view):
  49. @wraps(view)
  50. def decorated(*args, **kwargs):
  51. features = FeatureService.get_features(current_user.current_tenant_id)
  52. if not features.billing.enabled:
  53. abort(403, "Billing feature is not enabled.")
  54. return view(*args, **kwargs)
  55. return decorated
  56. def cloud_edition_billing_resource_check(resource: str):
  57. def interceptor(view):
  58. @wraps(view)
  59. def decorated(*args, **kwargs):
  60. features = FeatureService.get_features(current_user.current_tenant_id)
  61. if features.billing.enabled:
  62. members = features.members
  63. apps = features.apps
  64. vector_space = features.vector_space
  65. documents_upload_quota = features.documents_upload_quota
  66. annotation_quota_limit = features.annotation_quota_limit
  67. if resource == "members" and 0 < members.limit <= members.size:
  68. abort(403, "The number of members has reached the limit of your subscription.")
  69. elif resource == "apps" and 0 < apps.limit <= apps.size:
  70. abort(403, "The number of apps has reached the limit of your subscription.")
  71. elif resource == "vector_space" and 0 < vector_space.limit <= vector_space.size:
  72. abort(
  73. 403, "The capacity of the knowledge storage space has reached the limit of your subscription."
  74. )
  75. elif resource == "documents" and 0 < documents_upload_quota.limit <= documents_upload_quota.size:
  76. # The api of file upload is used in the multiple places,
  77. # so we need to check the source of the request from datasets
  78. source = request.args.get("source")
  79. if source == "datasets":
  80. abort(403, "The number of documents has reached the limit of your subscription.")
  81. else:
  82. return view(*args, **kwargs)
  83. elif resource == "workspace_custom" and not features.can_replace_logo:
  84. abort(403, "The workspace custom feature has reached the limit of your subscription.")
  85. elif resource == "annotation" and 0 < annotation_quota_limit.limit < annotation_quota_limit.size:
  86. abort(403, "The annotation quota has reached the limit of your subscription.")
  87. else:
  88. return view(*args, **kwargs)
  89. return view(*args, **kwargs)
  90. return decorated
  91. return interceptor
  92. def cloud_edition_billing_knowledge_limit_check(resource: str):
  93. def interceptor(view):
  94. @wraps(view)
  95. def decorated(*args, **kwargs):
  96. features = FeatureService.get_features(current_user.current_tenant_id)
  97. if features.billing.enabled:
  98. if resource == "add_segment":
  99. if features.billing.subscription.plan == "sandbox":
  100. abort(
  101. 403,
  102. "To unlock this feature and elevate your Dify experience, please upgrade to a paid plan.",
  103. )
  104. else:
  105. return view(*args, **kwargs)
  106. return view(*args, **kwargs)
  107. return decorated
  108. return interceptor
  109. def cloud_edition_billing_rate_limit_check(resource: str):
  110. def interceptor(view):
  111. @wraps(view)
  112. def decorated(*args, **kwargs):
  113. if resource == "knowledge":
  114. knowledge_rate_limit = FeatureService.get_knowledge_rate_limit(current_user.current_tenant_id)
  115. if knowledge_rate_limit.enabled:
  116. current_time = int(time.time() * 1000)
  117. key = f"rate_limit_{current_user.current_tenant_id}"
  118. redis_client.zadd(key, {current_time: current_time})
  119. redis_client.zremrangebyscore(key, 0, current_time - 60000)
  120. request_count = redis_client.zcard(key)
  121. if request_count > knowledge_rate_limit.limit:
  122. # add ratelimit record
  123. rate_limit_log = RateLimitLog(
  124. tenant_id=current_user.current_tenant_id,
  125. subscription_plan=knowledge_rate_limit.subscription_plan,
  126. operation="knowledge",
  127. )
  128. db.session.add(rate_limit_log)
  129. db.session.commit()
  130. abort(
  131. 403, "Sorry, you have reached the knowledge base request rate limit of your subscription."
  132. )
  133. return view(*args, **kwargs)
  134. return decorated
  135. return interceptor
  136. def cloud_utm_record(view):
  137. @wraps(view)
  138. def decorated(*args, **kwargs):
  139. with contextlib.suppress(Exception):
  140. features = FeatureService.get_features(current_user.current_tenant_id)
  141. if features.billing.enabled:
  142. utm_info = request.cookies.get("utm_info")
  143. if utm_info:
  144. utm_info_dict: dict = json.loads(utm_info)
  145. OperationService.record_utm(current_user.current_tenant_id, utm_info_dict)
  146. return view(*args, **kwargs)
  147. return decorated
  148. def setup_required(view):
  149. @wraps(view)
  150. def decorated(*args, **kwargs):
  151. # check setup
  152. if (
  153. dify_config.EDITION == "SELF_HOSTED"
  154. and os.environ.get("INIT_PASSWORD")
  155. and not db.session.query(DifySetup).first()
  156. ):
  157. raise NotInitValidateError()
  158. elif dify_config.EDITION == "SELF_HOSTED" and not db.session.query(DifySetup).first():
  159. raise NotSetupError()
  160. return view(*args, **kwargs)
  161. return decorated
  162. def enterprise_license_required(view):
  163. @wraps(view)
  164. def decorated(*args, **kwargs):
  165. settings = FeatureService.get_system_features()
  166. if settings.license.status in [LicenseStatus.INACTIVE, LicenseStatus.EXPIRED, LicenseStatus.LOST]:
  167. raise UnauthorizedAndForceLogout("Your license is invalid. Please contact your administrator.")
  168. return view(*args, **kwargs)
  169. return decorated
  170. def email_password_login_enabled(view):
  171. @wraps(view)
  172. def decorated(*args, **kwargs):
  173. features = FeatureService.get_system_features()
  174. if features.enable_email_password_login:
  175. return view(*args, **kwargs)
  176. # otherwise, return 403
  177. abort(403)
  178. return decorated
  179. def enable_change_email(view):
  180. @wraps(view)
  181. def decorated(*args, **kwargs):
  182. features = FeatureService.get_system_features()
  183. if features.enable_change_email:
  184. return view(*args, **kwargs)
  185. # otherwise, return 403
  186. abort(403)
  187. return decorated
  188. def is_allow_transfer_owner(view):
  189. @wraps(view)
  190. def decorated(*args, **kwargs):
  191. features = FeatureService.get_features(current_user.current_tenant_id)
  192. if features.is_allow_transfer_workspace:
  193. return view(*args, **kwargs)
  194. # otherwise, return 403
  195. abort(403)
  196. return decorated