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.

fix(permissions): avoid repeated team project queries (#100038)

posthog[bot]663098a2c95d
diff --git a/posthog/test/test_user_permissions.py b/posthog/test/test_user_permissions.py
index 41fad50c4e0f..a12e232339aa 100644
--- a/posthog/test/test_user_permissions.py
+++ b/posthog/test/test_user_permissions.py
@@ -941,6 +941,18 @@ class TestUserInsightPermissions(BaseTest, WithPermissionsBase):
class TestUserPermissionsEfficiency(BaseTest, WithPermissionsBase):
+ def test_project_ids_visible_for_user_queries_teams_once(self):
+ teams = [self.team]
+ teams.extend(Team.objects.create(organization=self.organization) for _ in range(3))
+
+ user_permissions = self.permissions()
+ _ = user_permissions.organization_memberships
+
+ with self.assertNumQueries(1):
+ project_ids = user_permissions.project_ids_visible_for_user
+
+ assert set(project_ids) == {team.project_id for team in teams}
+
def test_dashboard_efficiency(self):
self.organization.available_product_features = [
{"name": AvailableFeature.ACCESS_CONTROL, "key": AvailableFeature.ACCESS_CONTROL},
diff --git a/posthog/user_permissions.py b/posthog/user_permissions.py
index 152008ed28b3..ab4091ff13f0 100644
--- a/posthog/user_permissions.py
+++ b/posthog/user_permissions.py
@@ -68,7 +68,7 @@ class UserPermissions:
@cached_property
def teams_visible_for_user(self) -> list[Team]:
candidate_teams = Team.objects.filter(organization_id__in=self.organizations.keys()).only(
- "pk", "organization_id"
+ "pk", "organization_id", "project_id"
)
return [team for team in candidate_teams if self.team(team).effective_membership_level is not None]
diff --git a/products/feature_flags/backend/api/test/__snapshots__/test_organization_feature_flag.ambr b/products/feature_flags/backend/api/test/__snapshots__/test_organization_feature_flag.ambr
index 1630351c94dd..2baa72b24a17 100644
--- a/products/feature_flags/backend/api/test/__snapshots__/test_organization_feature_flag.ambr
+++ b/products/feature_flags/backend/api/test/__snapshots__/test_organization_feature_flag.ambr
@@ -1038,7 +1038,8 @@
# name: TestOrganizationFeatureFlagCopy.test_copy_feature_flag_create_new.5
'''
SELECT "posthog_team"."id",
- "posthog_team"."organization_id"
+ "posthog_team"."organization_id",
+ "posthog_team"."project_id"
FROM "posthog_team"
WHERE "posthog_team"."organization_id" IN ('00000000-0000-0000-0000-000000000000'::uuid)
'''
@@ -1906,7 +1907,8 @@
# name: TestOrganizationFeatureFlagGet.test_get_feature_flag_success.5
'''
SELECT "posthog_team"."id",
- "posthog_team"."organization_id"
+ "posthog_team"."organization_id",
+ "posthog_team"."project_id"
FROM "posthog_team"
WHERE "posthog_team"."organization_id" IN ('00000000-0000-0000-0000-000000000000'::uuid)
'''