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.

fix recreating users' default tenant relations when loading user (#2408)

Bowen Liang923224492015
diff --git a/api/services/account_service.py b/api/services/account_service.py
index 30580afdb55..5924d9047ed 100644
--- a/api/services/account_service.py
+++ b/api/services/account_service.py
@@ -55,18 +55,19 @@ class AccountService:
if account.status in [AccountStatus.BANNED.value, AccountStatus.CLOSED.value]:
raise Forbidden('Account is banned or closed.')
-
- # init owner's tenant
- tenant_owner = TenantAccountJoin.query.filter_by(account_id=account.id, role='owner').first()
- if not tenant_owner:
- _create_tenant_for_account(account)
-
+
current_tenant = TenantAccountJoin.query.filter_by(account_id=account.id, current=True).first()
if current_tenant:
account.current_tenant_id = current_tenant.tenant_id
+ account.current_tenant_id = current_tenant.tenant_id
else:
- account.current_tenant_id = tenant_owner.tenant_id
- tenant_owner.current = True
+ available_tenant = TenantAccountJoin.query.filter_by(account_id=account.id) \
+ .order_by(TenantAccountJoin.id.asc()).first()
+ if not available_tenant:
+ raise Forbidden('No available tenant for the user.')
+
+ account.current_tenant_id = available_tenant.tenant_id
+ available_tenant.current = True
db.session.commit()
if datetime.utcnow() - account.last_active_at > timedelta(minutes=10):