| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 | import jsonimport osfrom functools import wrapsfrom flask import abort, requestfrom flask_login import current_user  # type: ignorefrom configs import dify_configfrom controllers.console.workspace.error import AccountNotInitializedErrorfrom extensions.ext_database import dbfrom models.model import DifySetupfrom services.feature_service import FeatureService, LicenseStatusfrom services.operation_service import OperationServicefrom .error import NotInitValidateError, NotSetupError, UnauthorizedAndForceLogoutdef account_initialization_required(view):    @wraps(view)    def decorated(*args, **kwargs):        # check account initialization        account = current_user        if account.status == "uninitialized":            raise AccountNotInitializedError()        return view(*args, **kwargs)    return decorateddef only_edition_cloud(view):    @wraps(view)    def decorated(*args, **kwargs):        if dify_config.EDITION != "CLOUD":            abort(404)        return view(*args, **kwargs)    return decorateddef only_edition_self_hosted(view):    @wraps(view)    def decorated(*args, **kwargs):        if dify_config.EDITION != "SELF_HOSTED":            abort(404)        return view(*args, **kwargs)    return decorateddef cloud_edition_billing_resource_check(resource: str):    def interceptor(view):        @wraps(view)        def decorated(*args, **kwargs):            features = FeatureService.get_features(current_user.current_tenant_id)            if features.billing.enabled:                members = features.members                apps = features.apps                vector_space = features.vector_space                documents_upload_quota = features.documents_upload_quota                annotation_quota_limit = features.annotation_quota_limit                if resource == "members" and 0 < members.limit <= members.size:                    abort(403, "The number of members has reached the limit of your subscription.")                elif resource == "apps" and 0 < apps.limit <= apps.size:                    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:                    abort(403, "The capacity of the vector 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":                        abort(403, "The number of documents has reached the limit of your subscription.")                    else:                        return view(*args, **kwargs)                elif resource == "workspace_custom" and not features.can_replace_logo:                    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:                    abort(403, "The annotation quota has reached the limit of your subscription.")                else:                    return view(*args, **kwargs)            return view(*args, **kwargs)        return decorated    return interceptordef cloud_edition_billing_knowledge_limit_check(resource: str):    def interceptor(view):        @wraps(view)        def decorated(*args, **kwargs):            features = FeatureService.get_features(current_user.current_tenant_id)            if features.billing.enabled:                if resource == "add_segment":                    if features.billing.subscription.plan == "sandbox":                        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    return interceptordef cloud_utm_record(view):    @wraps(view)    def decorated(*args, **kwargs):        try:            features = FeatureService.get_features(current_user.current_tenant_id)            if features.billing.enabled:                utm_info = request.cookies.get("utm_info")                if utm_info:                    utm_info_dict: dict = json.loads(utm_info)                    OperationService.record_utm(current_user.current_tenant_id, utm_info_dict)        except Exception as e:            pass        return view(*args, **kwargs)    return decorateddef setup_required(view):    @wraps(view)    def decorated(*args, **kwargs):        # check setup        if (            dify_config.EDITION == "SELF_HOSTED"            and os.environ.get("INIT_PASSWORD")            and not db.session.query(DifySetup).first()        ):            raise NotInitValidateError()        elif dify_config.EDITION == "SELF_HOSTED" and not db.session.query(DifySetup).first():            raise NotSetupError()        return view(*args, **kwargs)    return decorateddef enterprise_license_required(view):    @wraps(view)    def decorated(*args, **kwargs):        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
 |