Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

chore(models): add Mapped annotations to EndUser and ApiToken (#42146)

KingPointer085a79c748be
diff --git a/api/controllers/service_api/wraps.py b/api/controllers/service_api/wraps.py
index 77201f96f0c..a28e731cd72 100644
--- a/api/controllers/service_api/wraps.py
+++ b/api/controllers/service_api/wraps.py
@@ -197,10 +197,14 @@ def cloud_edition_billing_resource_check[**P, R](
if dify_config.DEPLOYMENT_EDITION != DeploymentEdition.CLOUD:
return view(*args, **kwargs)
+ tenant_id = api_token.tenant_id
+ if tenant_id is None:
+ raise Unauthorized("Tenant id is required for this token.")
+
if resource == "vector_space":
- vector_space = application_services().feature_queries.get_workspace_vector_space(api_token.tenant_id)
+ vector_space = application_services().feature_queries.get_workspace_vector_space(tenant_id)
if vector_space.usage_unknown:
- features = FeatureService.get_features(api_token.tenant_id, exclude_vector_space=True)
+ features = FeatureService.get_features(tenant_id, exclude_vector_space=True)
if features.billing.subscription.plan == CloudPlan.SANDBOX:
raise ServiceUnavailable(
"Unable to verify vector space usage right now. Please try again later."
@@ -209,7 +213,7 @@ def cloud_edition_billing_resource_check[**P, R](
raise Forbidden("The capacity of the vector space has reached the limit of your subscription.")
return view(*args, **kwargs)
- features = FeatureService.get_features(api_token.tenant_id, exclude_vector_space=True)
+ features = FeatureService.get_features(tenant_id, exclude_vector_space=True)
members = features.members
apps = features.apps
@@ -244,7 +248,11 @@ def cloud_edition_billing_knowledge_limit_check[**P, R](
if dify_config.DEPLOYMENT_EDITION != DeploymentEdition.CLOUD or resource != "add_segment":
return view(*args, **kwargs)
- features = FeatureService.get_features(api_token.tenant_id, exclude_vector_space=True)
+ tenant_id = api_token.tenant_id
+ if tenant_id is None:
+ raise Unauthorized("Tenant id is required for this token.")
+
+ features = FeatureService.get_features(tenant_id, exclude_vector_space=True)
if features.billing.subscription.plan == CloudPlan.SANDBOX:
raise Forbidden(
"To unlock this feature and elevate your Dify experience, please upgrade to a paid plan."
@@ -267,10 +275,13 @@ def cloud_edition_billing_rate_limit_check[**P, R](
api_token = validate_and_get_api_token(api_token_type)
if resource == "knowledge":
- knowledge_rate_limit = FeatureService.get_knowledge_rate_limit(api_token.tenant_id)
+ tenant_id = api_token.tenant_id
+ if tenant_id is None:
+ raise Unauthorized("Tenant id is required for this token.")
+ knowledge_rate_limit = FeatureService.get_knowledge_rate_limit(tenant_id)
if knowledge_rate_limit.enabled:
current_time = int(time.time() * 1000)
- key = f"rate_limit_{api_token.tenant_id}"
+ key = f"rate_limit_{tenant_id}"
redis_client.zadd(key, {current_time: current_time})
@@ -281,7 +292,7 @@ def cloud_edition_billing_rate_limit_check[**P, R](
if request_count > knowledge_rate_limit.limit:
# add ratelimit record
rate_limit_log = RateLimitLog(
- tenant_id=api_token.tenant_id,
+ tenant_id=tenant_id,
subscription_plan=knowledge_rate_limit.subscription_plan,
operation="knowledge",
)
diff --git a/api/models/model.py b/api/models/model.py
index 4fb4cbf166e..1af4d50c163 100644
--- a/api/models/model.py
+++ b/api/models/model.py
@@ -2119,10 +2119,10 @@ class EndUser(Base, UserMixin):
id: Mapped[str] = mapped_column(StringUUID, default=lambda: str(uuid4()))
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
- app_id = mapped_column(StringUUID, nullable=True)
+ app_id: Mapped[str | None] = mapped_column(StringUUID, nullable=True)
type: Mapped[EndUserType] = mapped_column(EnumText(EndUserType, length=255), nullable=False)
- external_user_id = mapped_column(String(255), nullable=True)
- name = mapped_column(String(255))
+ external_user_id: Mapped[str | None] = mapped_column(String(255), nullable=True)
+ name: Mapped[str | None] = mapped_column(String(255), nullable=True)
_is_anonymous: Mapped[bool] = mapped_column("is_anonymous", sa.Boolean, nullable=False, server_default=sa.true())
@property
@@ -2136,8 +2136,8 @@ class EndUser(Base, UserMixin):
self._is_anonymous = value
session_id: Mapped[str] = mapped_column(String(255), nullable=False)
- created_at = mapped_column(sa.DateTime, nullable=False, server_default=func.current_timestamp())
- updated_at = mapped_column(
+ created_at: Mapped[datetime] = mapped_column(sa.DateTime, nullable=False, server_default=func.current_timestamp())
+ updated_at: Mapped[datetime] = mapped_column(
sa.DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp()
)
@@ -2298,13 +2298,13 @@ class ApiToken(Base):
sa.Index("api_token_tenant_idx", "tenant_id", "type"),
)
- id = mapped_column(StringUUID, default=lambda: str(uuid4()))
- app_id = mapped_column(StringUUID, nullable=True)
- tenant_id = mapped_column(StringUUID, nullable=True)
+ id: Mapped[str] = mapped_column(StringUUID, default=lambda: str(uuid4()))
+ app_id: Mapped[str | None] = mapped_column(StringUUID, nullable=True)
+ tenant_id: Mapped[str | None] = mapped_column(StringUUID, nullable=True)
type: Mapped[ApiTokenType] = mapped_column(EnumText(ApiTokenType, length=16), nullable=False)
token: Mapped[str] = mapped_column(String(255), nullable=False)
- last_used_at = mapped_column(sa.DateTime, nullable=True)
- created_at = mapped_column(sa.DateTime, nullable=False, server_default=func.current_timestamp())
+ last_used_at: Mapped[datetime | None] = mapped_column(sa.DateTime, nullable=True)
+ created_at: Mapped[datetime] = mapped_column(sa.DateTime, nullable=False, server_default=func.current_timestamp())
@staticmethod
def generate_api_key(prefix: str, n: int, *, session: Session) -> str:
diff --git a/api/services/end_user_service.py b/api/services/end_user_service.py
index a01d3a8128f..cd55a3dba41 100644
--- a/api/services/end_user_service.py
+++ b/api/services/end_user_service.py
@@ -154,6 +154,8 @@ class EndUserService:
found_app_ids: set[str] = set()
for eu in existing_end_users:
# If duplicates exist due to weak DB constraints, prefer the first
+ if eu.app_id is None:
+ continue
if eu.app_id not in result:
result[eu.app_id] = eu
found_app_ids.add(eu.app_id)
@@ -179,6 +181,8 @@ class EndUserService:
session.add_all(new_end_users)
for eu in new_end_users:
+ if eu.app_id is None:
+ continue
result[eu.app_id] = eu
return result
diff --git a/api/tests/integration_tests/libs/test_api_token_cache_integration.py b/api/tests/integration_tests/libs/test_api_token_cache_integration.py
index a942690cbdf..be60f01c469 100644
--- a/api/tests/integration_tests/libs/test_api_token_cache_integration.py
+++ b/api/tests/integration_tests/libs/test_api_token_cache_integration.py
@@ -303,7 +303,9 @@ class TestEndToEndCacheFlow:
assert cached_token.token == test_token_value
# Step 3: Verify tenant index
- index_key = ApiTokenCache._make_tenant_index_key(test_token.tenant_id)
+ tenant_id = test_token.tenant_id
+ assert tenant_id is not None
+ index_key = ApiTokenCache._make_tenant_index_key(tenant_id)
assert redis_client.exists(index_key) == 1
assert cache_key.encode() in redis_client.smembers(index_key)
@@ -313,10 +315,12 @@ class TestEndToEndCacheFlow:
assert cache_key.encode() not in redis_client.smembers(index_key)
finally:
+ tenant_id = test_token.tenant_id
db_session.delete(test_token)
db_session.commit()
redis_client.delete(f"api_token:{test_scope}:{test_token_value}")
- redis_client.delete(ApiTokenCache._make_tenant_index_key(test_token.tenant_id))
+ if tenant_id is not None:
+ redis_client.delete(ApiTokenCache._make_tenant_index_key(tenant_id))
def test_high_concurrency_simulation(self):
"""Simulate high concurrency access to cache."""
diff --git a/api/tests/unit_tests/controllers/inner_api/app/test_file_grants.py b/api/tests/unit_tests/controllers/inner_api/app/test_file_grants.py
index 97b5e1adffc..795fce195d9 100644
--- a/api/tests/unit_tests/controllers/inner_api/app/test_file_grants.py
+++ b/api/tests/unit_tests/controllers/inner_api/app/test_file_grants.py
@@ -326,7 +326,9 @@ def test_mint_folds_an_oversized_subject_into_one_identity(app: Flask, sqlite_se
end_users = sqlite_session.scalars(select(EndUser).order_by(EndUser.created_at)).all()
assert len(end_users) == 2
- assert all(len(end_user.external_user_id) == 255 for end_user in end_users)
+ assert all(
+ end_user.external_user_id is not None and len(end_user.external_user_id) == 255 for end_user in end_users
+ )
assert _subject_of(first) == _subject_of(again) != _subject_of(other)