wraps.py 5.6 KB

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