Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

[CHOO-3906] Name the organization in the last-org-admin deletion check

Petr Bauchd8457550d817
diff --git a/core/switch_core/accounts/deletion.py b/core/switch_core/accounts/deletion.py
index 1a72e2443..20db3fc2c 100644
--- a/core/switch_core/accounts/deletion.py
+++ b/core/switch_core/accounts/deletion.py
@@ -64,6 +64,7 @@ from switch_core.db.models import (
ServiceTokenIssuance,
Skill,
Template,
+ Tenant,
TenantJoinDomain,
TenantMember,
User,
@@ -238,28 +239,29 @@ class AccountDeletion:
self, session: AsyncSession, user: User, tenant_id: str
) -> str | None:
"""Refuse the last `org_admin` of the organization `tenant_id` belongs
- to while anyone else is in it: nobody would be left to run it.
- Asked on a session bound to `tenant_id`, which the organization's
- policy derives the organization from."""
+ to while anyone else is in it: nobody would be left to run it."""
if (
await self.users.organization_role(session, tenant_id, user.id)
!= ORGANIZATION_ADMIN_ROLE
):
return None
- others = (
- await session.execute(
+ tenant = await session.get(Tenant, tenant_id)
+ assert tenant is not None and tenant.organization_id is not None
+ organization = await session.get(Organization, tenant.organization_id)
+ assert organization is not None
+ roles = set(
+ await session.scalars(
select(OrganizationMember.role).where(
- OrganizationMember.user_id != user.id
+ OrganizationMember.organization_id == organization.id,
+ OrganizationMember.user_id != user.id,
)
)
- ).scalars()
- roles = set(others)
+ )
if not roles or ORGANIZATION_ADMIN_ROLE in roles:
return None
- organization = await session.scalar(select(Organization.name))
return (
- f"You're the only organization admin of {organization}, which has "
- "other members. Make one of them an organization admin first."
+ f"You're the only organization admin of {organization.name}, which "
+ "has other members. Make one of them an organization admin first."
)
async def delete(
diff --git a/core/tests/switch_core/accounts/test_account_deletion.py b/core/tests/switch_core/accounts/test_account_deletion.py
index 8dd2f206c..9a83e88be 100644
--- a/core/tests/switch_core/accounts/test_account_deletion.py
+++ b/core/tests/switch_core/accounts/test_account_deletion.py
@@ -34,6 +34,7 @@ from switch_core.db.models import (
Invitation,
MessagingInstall,
OidcIdentity,
+ Organization,
OrganizationMember,
Reference,
Room,
@@ -286,6 +287,32 @@ async def test_the_only_org_admin_of_an_organization_with_others_is_refused(
assert reason is not None and "only organization admin" in reason
+async def test_an_org_admin_of_another_organization_does_not_count(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ deletion, _, _ = _deletion(session_factory)
+ async with session_factory() as session:
+ admin = await _user(session, "org-admin")
+ other = await _user(session, "other")
+ elsewhere = await _user(session, "elsewhere")
+ await _member(session, TENANT_ZERO_ID, admin, "admin")
+ await _member(session, TENANT_ZERO_ID, other, "owner")
+ await _organization_role(session, admin, "org_admin")
+ await _organization_role(session, other, "member")
+ session.add(Organization(id="org-elsewhere", slug="org-elsewhere", name="Else"))
+ await session.flush()
+ session.add(
+ OrganizationMember(
+ organization_id="org-elsewhere", user_id=elsewhere.id, role="org_admin"
+ )
+ )
+ await session.commit()
+ reason = await deletion.refusal(
+ session, admin, [Workspace(TENANT_ZERO_ID, "Acme", "owner")]
+ )
+ assert reason is not None and "only organization admin" in reason
+
+
async def test_an_org_admin_with_another_org_admin_beside_them_is_not_refused(
session_factory: async_sessionmaker[AsyncSession],
) -> None: