Files
dify/api/controllers/console/wraps.py
T

292 lines
9.9 KiB
Python
Raw Normal View History

import contextlib
2024-01-29 17:31:45 +08:00
import json
2024-11-01 15:51:22 +08:00
import os
2025-03-10 19:50:11 +08:00
import time
2025-09-08 11:40:00 +09:00
from collections.abc import Callable
2023-05-15 08:51:32 +08:00
from functools import wraps
2025-09-08 11:40:00 +09:00
from typing import ParamSpec, TypeVar
2023-05-15 08:51:32 +08:00
from flask import abort, request
from flask_login import current_user
from configs import dify_config
from controllers.console.workspace.error import AccountNotInitializedError
+109
2025-02-17 17:05:13 +08:00
from extensions.ext_database import db
2025-03-10 19:50:11 +08:00
from extensions.ext_redis import redis_client
from models.account import AccountStatus
2025-03-10 19:50:11 +08:00
from models.dataset import RateLimitLog
2024-11-01 15:51:22 +08:00
from models.model import DifySetup
2024-11-15 17:59:36 +08:00
from services.feature_service import FeatureService, LicenseStatus
2024-01-29 17:31:45 +08:00
from services.operation_service import OperationService
2023-05-15 08:51:32 +08:00
2024-11-15 17:59:36 +08:00
from .error import NotInitValidateError, NotSetupError, UnauthorizedAndForceLogout
2024-11-01 15:51:22 +08:00
2025-09-08 11:40:00 +09:00
P = ParamSpec("P")
R = TypeVar("R")
2023-05-15 08:51:32 +08:00
2025-09-08 11:40:00 +09:00
def account_initialization_required(view: Callable[P, R]):
2023-05-15 08:51:32 +08:00
@wraps(view)
2025-09-08 11:40:00 +09:00
def decorated(*args: P.args, **kwargs: P.kwargs):
2023-05-15 08:51:32 +08:00
# check account initialization
account = current_user
if account.status == AccountStatus.UNINITIALIZED:
2023-05-15 08:51:32 +08:00
raise AccountNotInitializedError()
return view(*args, **kwargs)
return decorated
2025-09-08 11:40:00 +09:00
def only_edition_cloud(view: Callable[P, R]):
2023-05-15 08:51:32 +08:00
@wraps(view)
2025-09-08 11:40:00 +09:00
def decorated(*args: P.args, **kwargs: P.kwargs):
if dify_config.EDITION != "CLOUD":
2023-05-15 08:51:32 +08:00
abort(404)
return view(*args, **kwargs)
return decorated
2025-09-08 11:40:00 +09:00
def only_edition_enterprise(view: Callable[P, R]):
2025-06-09 17:19:53 +09:00
@wraps(view)
2025-09-08 11:40:00 +09:00
def decorated(*args: P.args, **kwargs: P.kwargs):
2025-06-09 17:19:53 +09:00
if not dify_config.ENTERPRISE_ENABLED:
abort(404)
return view(*args, **kwargs)
return decorated
2025-09-08 11:40:00 +09:00
def only_edition_self_hosted(view: Callable[P, R]):
2023-05-15 08:51:32 +08:00
@wraps(view)
2025-09-08 11:40:00 +09:00
def decorated(*args: P.args, **kwargs: P.kwargs):
if dify_config.EDITION != "SELF_HOSTED":
2023-05-15 08:51:32 +08:00
abort(404)
return view(*args, **kwargs)
return decorated
2023-12-05 16:53:55 +08:00
2025-09-08 11:40:00 +09:00
def cloud_edition_billing_enabled(view: Callable[P, R]):
2025-04-01 02:45:34 -04:00
@wraps(view)
2025-09-08 11:40:00 +09:00
def decorated(*args: P.args, **kwargs: P.kwargs):
2025-04-01 02:45:34 -04:00
features = FeatureService.get_features(current_user.current_tenant_id)
if not features.billing.enabled:
abort(403, "Billing feature is not enabled.")
return view(*args, **kwargs)
return decorated
2024-08-28 18:55:47 +08:00
def cloud_edition_billing_resource_check(resource: str):
2025-09-08 11:40:00 +09:00
def interceptor(view: Callable[P, R]):
2023-12-05 16:53:55 +08:00
@wraps(view)
2025-09-08 11:40:00 +09:00
def decorated(*args: P.args, **kwargs: P.kwargs):
2023-12-20 15:37:57 +08:00
features = FeatureService.get_features(current_user.current_tenant_id)
if features.billing.enabled:
members = features.members
apps = features.apps
vector_space = features.vector_space
2024-03-03 12:45:06 +08:00
documents_upload_quota = features.documents_upload_quota
2023-12-20 15:37:57 +08:00
annotation_quota_limit = features.annotation_quota_limit
if resource == "members" and 0 < members.limit <= members.size:
2024-08-28 18:55:47 +08:00
abort(403, "The number of members has reached the limit of your subscription.")
elif resource == "apps" and 0 < apps.limit <= apps.size:
2024-08-28 18:55:47 +08:00
abort(403, "The number of apps has reached the limit of your subscription.")
elif resource == "vector_space" and 0 < vector_space.limit <= vector_space.size:
2025-03-10 19:50:11 +08:00
abort(
403, "The capacity of the knowledge storage space has reached the limit of your subscription."
)
elif resource == "documents" and 0 < documents_upload_quota.limit <= documents_upload_quota.size:
# The api of file upload is used in the multiple places,
# so we need to check the source of the request from datasets
source = request.args.get("source")
if source == "datasets":
2024-08-28 18:55:47 +08:00
abort(403, "The number of documents has reached the limit of your subscription.")
2024-03-03 12:45:06 +08:00
else:
return view(*args, **kwargs)
elif resource == "workspace_custom" and not features.can_replace_logo:
2024-08-28 18:55:47 +08:00
abort(403, "The workspace custom feature has reached the limit of your subscription.")
elif resource == "annotation" and 0 < annotation_quota_limit.limit < annotation_quota_limit.size:
2024-08-28 18:55:47 +08:00
abort(403, "The annotation quota has reached the limit of your subscription.")
2023-12-05 16:53:55 +08:00
else:
return view(*args, **kwargs)
return view(*args, **kwargs)
2023-12-05 16:53:55 +08:00
return decorated
return interceptor
2024-08-28 18:55:47 +08:00
def cloud_edition_billing_knowledge_limit_check(resource: str):
2025-09-08 11:40:00 +09:00
def interceptor(view: Callable[P, R]):
@wraps(view)
2025-09-08 11:40:00 +09:00
def decorated(*args: P.args, **kwargs: P.kwargs):
features = FeatureService.get_features(current_user.current_tenant_id)
if features.billing.enabled:
if resource == "add_segment":
if features.billing.subscription.plan == "sandbox":
2024-08-28 18:55:47 +08:00
abort(
403,
"To unlock this feature and elevate your Dify experience, please upgrade to a paid plan.",
)
else:
return view(*args, **kwargs)
return view(*args, **kwargs)
return decorated
2023-12-05 16:53:55 +08:00
return interceptor
2024-01-29 17:31:45 +08:00
2025-03-10 19:50:11 +08:00
def cloud_edition_billing_rate_limit_check(resource: str):
2025-09-08 11:40:00 +09:00
def interceptor(view: Callable[P, R]):
2025-03-10 19:50:11 +08:00
@wraps(view)
2025-09-08 11:40:00 +09:00
def decorated(*args: P.args, **kwargs: P.kwargs):
2025-03-10 19:50:11 +08:00
if resource == "knowledge":
knowledge_rate_limit = FeatureService.get_knowledge_rate_limit(current_user.current_tenant_id)
if knowledge_rate_limit.enabled:
current_time = int(time.time() * 1000)
key = f"rate_limit_{current_user.current_tenant_id}"
redis_client.zadd(key, {current_time: current_time})
redis_client.zremrangebyscore(key, 0, current_time - 60000)
request_count = redis_client.zcard(key)
if request_count > knowledge_rate_limit.limit:
# add ratelimit record
rate_limit_log = RateLimitLog(
tenant_id=current_user.current_tenant_id,
subscription_plan=knowledge_rate_limit.subscription_plan,
operation="knowledge",
)
db.session.add(rate_limit_log)
db.session.commit()
abort(
403, "Sorry, you have reached the knowledge base request rate limit of your subscription."
)
return view(*args, **kwargs)
return decorated
return interceptor
2025-09-08 11:40:00 +09:00
def cloud_utm_record(view: Callable[P, R]):
2024-01-29 17:31:45 +08:00
@wraps(view)
2025-09-08 11:40:00 +09:00
def decorated(*args: P.args, **kwargs: P.kwargs):
with contextlib.suppress(Exception):
2024-01-29 17:31:45 +08:00
features = FeatureService.get_features(current_user.current_tenant_id)
if features.billing.enabled:
utm_info = request.cookies.get("utm_info")
2024-01-29 17:31:45 +08:00
if utm_info:
2024-12-24 18:38:51 +08:00
utm_info_dict: dict = json.loads(utm_info)
OperationService.record_utm(current_user.current_tenant_id, utm_info_dict)
2024-01-29 17:31:45 +08:00
return view(*args, **kwargs)
2024-01-29 17:31:45 +08:00
return decorated
2024-11-01 15:51:22 +08:00
2025-09-08 11:40:00 +09:00
def setup_required(view: Callable[P, R]):
2024-11-01 15:51:22 +08:00
@wraps(view)
2025-09-08 11:40:00 +09:00
def decorated(*args: P.args, **kwargs: P.kwargs):
2024-11-01 15:51:22 +08:00
# check setup
+109
2025-02-17 17:05:13 +08:00
if (
dify_config.EDITION == "SELF_HOSTED"
and os.environ.get("INIT_PASSWORD")
and not db.session.query(DifySetup).first()
):
2024-11-01 15:51:22 +08:00
raise NotInitValidateError()
+109
2025-02-17 17:05:13 +08:00
elif dify_config.EDITION == "SELF_HOSTED" and not db.session.query(DifySetup).first():
2024-11-01 15:51:22 +08:00
raise NotSetupError()
return view(*args, **kwargs)
return decorated
2024-11-15 17:59:36 +08:00
2025-09-08 11:40:00 +09:00
def enterprise_license_required(view: Callable[P, R]):
2024-11-15 17:59:36 +08:00
@wraps(view)
2025-09-08 11:40:00 +09:00
def decorated(*args: P.args, **kwargs: P.kwargs):
2024-11-15 17:59:36 +08:00
settings = FeatureService.get_system_features()
if settings.license.status in [LicenseStatus.INACTIVE, LicenseStatus.EXPIRED, LicenseStatus.LOST]:
raise UnauthorizedAndForceLogout("Your license is invalid. Please contact your administrator.")
return view(*args, **kwargs)
return decorated
2025-04-18 05:06:09 -04:00
2025-09-08 11:40:00 +09:00
def email_password_login_enabled(view: Callable[P, R]):
2025-04-18 05:06:09 -04:00
@wraps(view)
2025-09-08 11:40:00 +09:00
def decorated(*args: P.args, **kwargs: P.kwargs):
2025-04-18 05:06:09 -04:00
features = FeatureService.get_system_features()
if features.enable_email_password_login:
return view(*args, **kwargs)
# otherwise, return 403
abort(403)
return decorated
2025-07-17 10:55:59 +08:00
2025-09-12 10:24:54 +08:00
def email_register_enabled(view):
@wraps(view)
def decorated(*args, **kwargs):
features = FeatureService.get_system_features()
if features.is_allow_register:
return view(*args, **kwargs)
# otherwise, return 403
abort(403)
return decorated
2025-09-08 11:40:00 +09:00
def enable_change_email(view: Callable[P, R]):
2025-07-17 10:55:59 +08:00
@wraps(view)
2025-09-08 11:40:00 +09:00
def decorated(*args: P.args, **kwargs: P.kwargs):
2025-07-17 10:55:59 +08:00
features = FeatureService.get_system_features()
if features.enable_change_email:
return view(*args, **kwargs)
# otherwise, return 403
abort(403)
return decorated
2025-09-08 11:40:00 +09:00
def is_allow_transfer_owner(view: Callable[P, R]):
2025-07-17 10:55:59 +08:00
@wraps(view)
2025-09-08 11:40:00 +09:00
def decorated(*args: P.args, **kwargs: P.kwargs):
2025-07-17 10:55:59 +08:00
features = FeatureService.get_features(current_user.current_tenant_id)
if features.is_allow_transfer_workspace:
return view(*args, **kwargs)
# otherwise, return 403
abort(403)
return decorated
+9
2025-09-18 12:49:10 +08:00
def knowledge_pipeline_publish_enabled(view):
@wraps(view)
def decorated(*args, **kwargs):
features = FeatureService.get_features(current_user.current_tenant_id)
if features.knowledge_pipeline.publish_enabled:
return view(*args, **kwargs)
abort(403)
return decorated