Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat: scope email domain ownership to organization instead of team (#61899)

Alex V1386ff78aae4
diff --git a/products/conversations/backend/api/email_settings.py b/products/conversations/backend/api/email_settings.py
index 1abfd64445eb..38ec1967c717 100644
--- a/products/conversations/backend/api/email_settings.py
+++ b/products/conversations/backend/api/email_settings.py
@@ -149,12 +149,12 @@ class EmailConnectView(APIView):
status=400,
)
- # Guard: cross-team domain ownership
- if EmailChannel.objects.filter(domain=domain).exclude(team=team).exists():
- return Response({"error": "This domain is already in use by another team."}, status=409)
+ # Guard: cross-org domain ownership
+ if EmailChannel.objects.filter(domain=domain).exclude(team__organization_id=team.organization_id).exists():
+ return Response({"error": "This domain is already in use by another organization."}, status=409)
- # Check if team already has a config on this domain (single query for both existence + data)
- sibling = EmailChannel.objects.filter(team=team, domain=domain).first()
+ # Check if org already has a config on this domain (reuse Mailgun registration + DNS records)
+ sibling = EmailChannel.objects.filter(team__organization_id=team.organization_id, domain=domain).first()
dns_records: dict = {}
if sibling:
@@ -221,7 +221,7 @@ class EmailConnectView(APIView):
class EmailVerifyDomainView(APIView):
"""Trigger Mailgun DNS verification and update local config."""
- permission_classes = [IsAuthenticated]
+ permission_classes = [IsAuthenticated, IsConversationsAdmin]
throttle_classes = [EmailVerifyDomainThrottle]
def post(self, request: Request, *args, **kwargs) -> Response:
@@ -241,8 +241,8 @@ class EmailVerifyDomainView(APIView):
is_active = mg_result.get("state") == "active"
dns_records = {"sending_dns_records": mg_result.get("sending_dns_records", [])}
- # Update all configs on this team sharing the same domain
- EmailChannel.objects.filter(team=team, domain=config.domain).update(
+ # Update all configs in this org sharing the same domain
+ EmailChannel.objects.filter(team__organization_id=team.organization_id, domain=config.domain).update(
domain_verified=is_active,
dns_records=dns_records,
)
diff --git a/products/conversations/backend/api/tests/test_email_endpoints.py b/products/conversations/backend/api/tests/test_email_endpoints.py
index 6460adc652ec..3d81da393cbd 100644
--- a/products/conversations/backend/api/tests/test_email_endpoints.py
+++ b/products/conversations/backend/api/tests/test_email_endpoints.py
@@ -12,7 +12,7 @@ from parameterized import parameterized
from PIL import Image
from posthog.models.comment import Comment
-from posthog.models.organization import OrganizationMembership
+from posthog.models.organization import Organization, OrganizationMembership
from posthog.models.team import Team
from products.conversations.backend.mailgun import (
@@ -62,7 +62,7 @@ class TestEmailConnectDomainCaseInsensitivity(BaseTest):
"products.conversations.backend.api.email_settings.get_instance_setting",
return_value="mg.posthog.com",
)
- def test_connect_rejects_duplicate_domain_different_casing(
+ def test_connect_rejects_duplicate_domain_different_casing_cross_org(
self, _mock_setting: MagicMock, _mock_mailgun: MagicMock
):
self.client.post(
@@ -71,8 +71,12 @@ class TestEmailConnectDomainCaseInsensitivity(BaseTest):
content_type="application/json",
)
- # Second team tries the same domain with different casing
- second_team = Team.objects.create(organization=self.organization)
+ # A team in a different org tries the same domain with different casing
+ other_org = Organization.objects.create(name="Other Org")
+ second_team = Team.objects.create(organization=other_org)
+ OrganizationMembership.objects.create(
+ user=self.user, organization=other_org, level=OrganizationMembership.Level.ADMIN
+ )
self.user.current_team = second_team
self.user.save()
@@ -214,24 +218,54 @@ class TestEmailMultiConfig(BaseTest):
"products.conversations.backend.api.email_settings.get_instance_setting",
return_value="mg.posthog.com",
)
- def test_cross_team_domain_rejected(self, _mock_setting: MagicMock, _mock_mailgun: MagicMock):
+ def test_same_org_different_team_domain_allowed(self, _mock_setting: MagicMock, mock_mailgun: MagicMock):
self.client.post(
"/api/conversations/v1/email/connect",
{"from_email": "[email protected]", "from_name": "Support"},
content_type="application/json",
)
+ mock_mailgun.reset_mock()
second_team = Team.objects.create(organization=self.organization)
self.user.current_team = second_team
self.user.save()
+ r2 = self.client.post(
+ "/api/conversations/v1/email/connect",
+ {"from_email": "[email protected]", "from_name": "Billing"},
+ content_type="application/json",
+ )
+ assert r2.status_code == 200
+ # Reuses existing Mailgun registration — no second add_domain call
+ mock_mailgun.assert_not_called()
+
+ @patch("products.conversations.backend.api.email_settings.mailgun_add_domain", return_value={})
+ @patch(
+ "products.conversations.backend.api.email_settings.get_instance_setting",
+ return_value="mg.posthog.com",
+ )
+ def test_cross_org_domain_rejected(self, _mock_setting: MagicMock, _mock_mailgun: MagicMock):
+ self.client.post(
+ "/api/conversations/v1/email/connect",
+ {"from_email": "[email protected]", "from_name": "Support"},
+ content_type="application/json",
+ )
+
+ other_org = Organization.objects.create(name="Other Org")
+ second_team = Team.objects.create(organization=other_org)
+ OrganizationMembership.objects.create(
+ user=self.user, organization=other_org, level=OrganizationMembership.Level.ADMIN
+ )
+ self.user.current_team = second_team
+ self.user.save()
+
r2 = self.client.post(
"/api/conversations/v1/email/connect",
{"from_email": "[email protected]", "from_name": "Billing"},
content_type="application/json",
)
assert r2.status_code == 409
- assert "already in use" in r2.json()["error"]
+ assert "another organization" in r2.json()["error"]
@patch("products.conversations.backend.api.email_settings.mailgun_add_domain", return_value={})
@patch("products.conversations.backend.api.email_settings.mailgun_delete_domain")
@@ -446,8 +480,22 @@ class TestEmailMultiConfig(BaseTest):
{"from_email": "[email protected]", "from_name": "Billing"},
content_type="application/json",
)
- config2_id = r2.json()["config"]["id"]
+ # Also connect on a different team in the same org
+ second_team = Team.objects.create(organization=self.organization)
+ self.user.current_team = second_team
+ self.user.save()
+ self.client.post(
+ "/api/conversations/v1/email/connect",
+ {"from_email": "[email protected]", "from_name": "Sales"},
+ content_type="application/json",
+ )
+
+ # Switch back to original team to verify
+ self.user.current_team = self.team
+ self.user.save()
+
+ config2_id = r2.json()["config"]["id"]
response = self.client.post(
"/api/conversations/v1/email/verify-domain",
{"config_id": config2_id},
@@ -456,8 +504,9 @@ class TestEmailMultiConfig(BaseTest):
assert response.status_code == 200
assert response.json()["domain_verified"] is True
- # Both configs should be verified
- configs = EmailChannel.objects.filter(team=self.team)
+ # All configs across the org sharing the domain should be verified
+ configs = EmailChannel.objects.filter(team__organization_id=self.organization.id, domain="example.com")
+ assert configs.count() == 3
assert all(c.domain_verified for c in configs)
@patch("products.conversations.backend.api.email_settings.mailgun_add_domain", return_value={})
diff --git a/products/conversations/frontend/scenes/settings/EmailSection.tsx b/products/conversations/frontend/scenes/settings/EmailSection.tsx
index 343830132164..fd11c74a4606 100644
--- a/products/conversations/frontend/scenes/settings/EmailSection.tsx
+++ b/products/conversations/frontend/scenes/settings/EmailSection.tsx
@@ -128,6 +128,7 @@ function EmailConfigContent({ config }: { config: EmailConfigStatus }): JSX.Elem
size="small"
onClick={() => verifyEmailDomain(config.id)}
loading={isVerifying}
+ disabledReason={adminRestrictionReason}
icon={<IconRefresh />}
>
{config.domain_verified ? 'Re-verify' : 'Verify domain'}