Skip to content

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

Take a workspace admin's own membership to set a retention window

Petr Bauchd3a3f425a9c9
diff --git a/core/switch_core/gateway/tenants.py b/core/switch_core/gateway/tenants.py
index 33e3e4239..89635c62b 100644
--- a/core/switch_core/gateway/tenants.py
+++ b/core/switch_core/gateway/tenants.py
@@ -864,13 +864,18 @@ async def set_retention_policy(
session: Annotated[AsyncSession, Depends(get_session)],
retention_store: Annotated[RetentionStore, Depends(get_retention_store)],
user: Annotated[User, Depends(require_tenant_admin)],
+ user_store: Annotated[UserStore, Depends(get_user_store)],
) -> RetentionPolicyResponse:
"""Delete the bound tenant's room messages once they are older than
- `message_retention_days`. Admins only.
+ `message_retention_days`. Admins only, by their own admin membership:
+ the messages it deletes cannot be brought back.
Takes effect at the next hourly retention pass, not in this request.
"""
_require_bound_tenant(tenant_id)
+ await _require_admin_membership(
+ session, user_store, tenant_id, user, "set when messages are deleted"
+ )
previous = await retention_store.get_policy(session)
previous_days = None if previous is None else previous.message_retention_days
policy = await retention_store.set_policy(
@@ -946,14 +951,19 @@ def _erasure_detail(erasure: PersonErasure) -> ErasureDetail:
async def _require_admin_membership(
- session: AsyncSession, user_store: UserStore, tenant_id: str, user: User
+ session: AsyncSession,
+ user_store: UserStore,
+ tenant_id: str,
+ user: User,
+ action: str,
) -> None:
- """Erasing a person cannot be undone, so, like deleting the workspace, it
- takes the caller's own admin membership: not the deployment operator's
- bypass, and not an organization admin acting as one."""
+ """What cannot be undone, erasing a person or setting messages to be
+ deleted, takes, like deleting the workspace, the caller's own admin
+ membership: not the deployment operator's bypass, and not an organization
+ admin acting as one. `action` completes "Only a workspace admin may"."""
if await user_store.tenant_role(session, tenant_id, user.id) != "admin":
raise HTTPException(
- status_code=403, detail="Only a workspace admin may erase people"
+ status_code=403, detail=f"Only a workspace admin may {action}"
)
@@ -971,7 +981,9 @@ async def list_people(
including deleted accounts. Admins only, by their own admin membership:
this is the list a person is erased from."""
_require_bound_tenant(tenant_id)
- await _require_admin_membership(session, user_store, tenant_id, user)
+ await _require_admin_membership(
+ session, user_store, tenant_id, user, "erase people"
+ )
people = [
PersonDetail(
id=person.external_user_id,
@@ -1025,7 +1037,9 @@ async def list_erasures(
"""The bound tenant's erasure requests, newest first. Admins only, by
their own admin membership."""
_require_bound_tenant(tenant_id)
- await _require_admin_membership(session, user_store, tenant_id, user)
+ await _require_admin_membership(
+ session, user_store, tenant_id, user, "erase people"
+ )
return [
_erasure_detail(e) for e in await erasure_store.list_erasures(session, limit=50)
]
@@ -1049,7 +1063,9 @@ async def create_erasure(
in this workspace; 409 means one is already being erased.
"""
_require_bound_tenant(tenant_id)
- await _require_admin_membership(session, user_store, tenant_id, user)
+ await _require_admin_membership(
+ session, user_store, tenant_id, user, "erase people"
+ )
ids = list(dict.fromkeys(body.external_user_ids))
former_ids = list(dict.fromkeys(body.former_sender_ids))
member_ids = list(dict.fromkeys(body.member_client_ids))
diff --git a/core/tests/switch_core/gateway/test_tenant_api_routes.py b/core/tests/switch_core/gateway/test_tenant_api_routes.py
index 34e72cb88..a2766f7df 100644
--- a/core/tests/switch_core/gateway/test_tenant_api_routes.py
+++ b/core/tests/switch_core/gateway/test_tenant_api_routes.py
@@ -2856,6 +2856,39 @@ class TestRetentionRoutes:
assert read.status_code == 403
assert written.status_code == 403
+ async def test_an_operator_without_admin_membership_cannot_set_it(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ operator_id = await _make_member(
+ session_factory,
+ name="retention-operator",
+ tenant_id=TENANT_A,
+ role="member",
+ user_role="admin",
+ )
+ operator = create_jwt(
+ operator_id,
+ "admin",
+ _KEYRING,
+ TENANT_A,
+ session_version=0,
+ auth_time=datetime.now(UTC),
+ )
+
+ async with _client(_app(session_factory), operator) as client:
+ read = await client.get(f"/tenants/{TENANT_A}/retention")
+ written = await client.put(
+ f"/tenants/{TENANT_A}/retention", json={"message_retention_days": 30}
+ )
+
+ assert read.status_code == 200
+ assert written.status_code == 403
+ assert written.json()["detail"] == (
+ "Only a workspace admin may set when messages are deleted"
+ )
+
@pytest.mark.parametrize("days", [0, 3651])
async def test_a_window_out_of_range_is_refused(
self, session_factory: async_sessionmaker[AsyncSession], days: int