Skip to content

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

Backfill only workspace owners as organization admins

Petr Bauch9bc4c7fbe0da
diff --git a/core/switch_core/db/models.py b/core/switch_core/db/models.py
index c36aecf18..bb60eff03 100644
--- a/core/switch_core/db/models.py
+++ b/core/switch_core/db/models.py
@@ -121,9 +121,9 @@ class OrganizationMember(Base):
changing or removing a workspace membership leaves this row as it is. Only
an organization's own `org_admin`s grant or revoke `org_admin`, through
the organization's API. The one exception is migration `238c68b7306e`:
- it made each existing workspace's owners and admins `org_admin` of the
- organization it created for that workspace alone, which gave them nothing
- they did not already administer.
+ it made each existing workspace's owners `org_admin` of the organization
+ it created for that workspace alone, which gave them nothing they did not
+ already control.
"""
__tablename__ = "organization_members"
diff --git a/core/switch_core/migrations/versions/238c68b7306e_organizations.py b/core/switch_core/migrations/versions/238c68b7306e_organizations.py
index f059229bf..f29b60922 100644
--- a/core/switch_core/migrations/versions/238c68b7306e_organizations.py
+++ b/core/switch_core/migrations/versions/238c68b7306e_organizations.py
@@ -4,8 +4,10 @@ Two organization-scoped tables, `organizations` and `organization_members`,
the `require_organization_id()` function their policies compare against, and
a nullable `tenants.organization_id`. Every existing workspace gets an
organization of its own, named and slugged as the workspace is; its owners
-and admins become the organization's `org_admin`s and everyone else its
-`member`s.
+become the organization's `org_admin`s and everyone else, admins included,
+its `member`s. Admins keep administering the workspace through their
+workspace role. Making them `org_admin` too would have let an admin demote
+the owner within the organization, and then lock them out of it.
The row-level-security DDL is a verbatim copy of `switch_core/db/rls_ddl.py`
as it stood when this migration was written, copied rather than imported for
@@ -150,7 +152,7 @@ def upgrade() -> None:
op.execute(
"INSERT INTO organization_members (organization_id, user_id, role, created_at) "
"SELECT tenants.organization_id, tenant_members.user_id, "
- "CASE WHEN tenant_members.role IN ('owner', 'admin') "
+ "CASE WHEN tenant_members.role = 'owner' "
"THEN 'org_admin' ELSE 'member' END, tenant_members.created_at "
"FROM tenant_members JOIN tenants ON tenants.id = tenant_members.tenant_id"
)
diff --git a/core/tests/switch_core/test_migration_organizations.py b/core/tests/switch_core/test_migration_organizations.py
index e0a83bc2c..4de3f2b21 100644
--- a/core/tests/switch_core/test_migration_organizations.py
+++ b/core/tests/switch_core/test_migration_organizations.py
@@ -2,8 +2,8 @@
Seeds two workspaces, one with an owner, an admin and a member and one with
nobody, stops the chain one revision short, runs it, and reads what it made:
-one organization per workspace, named as it is, its owners and admins as
-`org_admin` and everyone else as `member`.
+one organization per workspace, named as it is, its owners as `org_admin`
+and everyone else, admins included, as `member`.
"""
from __future__ import annotations
@@ -21,7 +21,7 @@ _MIGRATION_UNDER_TEST = "238c68b7306e"
_TENANT_ZERO_ID = "00000000-0000-0000-0000-000000000000"
-async def test_every_workspace_gets_an_organization_and_its_admins_run_it(
+async def test_every_workspace_gets_an_organization_and_its_owners_run_it(
postgres_url: str,
) -> None:
engine = await _fixture_engine(postgres_url)
@@ -96,5 +96,5 @@ async def test_every_workspace_gets_an_organization_and_its_admins_run_it(
assert organizations == 2
assert workspaces[_TENANT_ZERO_ID][0] == "default"
assert workspaces["empty"] == ("empty", "Empty")
- assert members == {"ada": "org_admin", "bob": "org_admin", "cy": "member"}
+ assert members == {"ada": "org_admin", "bob": "member", "cy": "member"}
assert empty_members == 0