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

This commit is contained in:
Bowen Liang 2024-02-12 01:31:40 +08:00 committed by GitHub
parent 476eb90a90
commit 9232244920
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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):