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(notifications): hide notifications from projects the user cannot open (#117210)

Daniel Marchuk0b2cf7faf665
diff --git a/products/notifications/backend/presentation/views.py b/products/notifications/backend/presentation/views.py
index 337fad5cc124..748c82db6dcd 100644
--- a/products/notifications/backend/presentation/views.py
+++ b/products/notifications/backend/presentation/views.py
@@ -4,7 +4,7 @@ from typing import cast
from uuid import UUID
from django.db import transaction
-from django.db.models import Exists, OuterRef, QuerySet, Subquery
+from django.db.models import Exists, OuterRef, Q, QuerySet, Subquery
from django.shortcuts import get_object_or_404
from django.utils.dateparse import parse_datetime
@@ -22,7 +22,11 @@ from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.api.utils import action
from posthog.models import User
-from products.access_control.backend.facade.user_access_control import ACCESS_CONTROL_RESOURCES, UserAccessControl
+from products.access_control.backend.facade.user_access_control import (
+ ACCESS_CONTROL_RESOURCES,
+ UserAccessControl,
+ visible_teams_for_user,
+)
from products.notifications.backend.cache import get_unread_count, invalidate_unread_count, set_unread_count
from products.notifications.backend.models import NotificationArchiveState, NotificationEvent, NotificationReadState
from products.notifications.backend.presentation.serializers import NotificationEventSerializer
@@ -74,8 +78,12 @@ class NotificationsViewSet(TeamAndOrgViewSetMixin, GenericViewSet):
def _get_base_queryset(self) -> QuerySet:
user = self._get_user()
team = self.team
+ visible_team_ids = visible_teams_for_user(
+ team.organization, self.user_access_control, self.user_permissions
+ ).values("id")
return (
NotificationEvent.objects.filter(
+ Q(team__isnull=True) | Q(team_id__in=visible_team_ids),
organization_id=team.organization_id,
resolved_user_ids__contains=[user.id],
)
diff --git a/products/notifications/backend/tests/test_api.py b/products/notifications/backend/tests/test_api.py
index 2c9e0130f5ce..cd399a46f1b4 100644
--- a/products/notifications/backend/tests/test_api.py
+++ b/products/notifications/backend/tests/test_api.py
@@ -72,6 +72,28 @@ class TestNotificationsAPI(BaseTest):
assert self.client.get(url).json()["results"] == []
assert self.client.get(url + "unread_count/").json()["count"] == 0
+ def test_notification_from_a_project_the_user_cannot_open_is_hidden(self) -> None:
+ self.organization.available_product_features = [{"key": AvailableFeature.ACCESS_CONTROL}]
+ self.organization.save(update_fields=["available_product_features"])
+ private_team = Team.objects.create(organization=self.organization, name="Private")
+ private_event = NotificationEvent.objects.create(
+ organization=self.organization,
+ team=private_team,
+ notification_type="workflow_suggestion",
+ title="Secret workflow",
+ target_type="user",
+ target_id=str(self.user.id),
+ resolved_user_ids=[self.user.id],
+ )
+ url = f"/api/environments/{self.team.id}/notifications/"
+ assert {e["id"] for e in self.client.get(url).json()["results"]} == {str(self.event.id), str(private_event.id)}
+ AccessControl.objects.create(
+ team=private_team, resource="project", resource_id=str(private_team.id), access_level="none"
+ )
+ cache.clear()
+ assert [e["id"] for e in self.client.get(url).json()["results"]] == [str(self.event.id)]
+ assert self.client.get(url + "unread_count/").json()["count"] == 1
+
def test_unread_count(self):
resp = self.client.get(f"/api/environments/{self.team.id}/notifications/unread_count/")
assert resp.status_code == 200