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.

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