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(api): enforce ORGANIZATION_SECURITY_SETTINGS entitlement server-side (#62143)

Yasen6ebe0d9727a5
diff --git a/posthog/api/organization_personal_api_key.py b/posthog/api/organization_personal_api_key.py
index 0e0038357b45..7d59e8e664ab 100644
--- a/posthog/api/organization_personal_api_key.py
+++ b/posthog/api/organization_personal_api_key.py
@@ -6,8 +6,9 @@ from drf_spectacular.utils import extend_schema, extend_schema_field
from rest_framework import mixins, serializers, viewsets
from posthog.api.routing import TeamAndOrgViewSetMixin
+from posthog.constants import AvailableFeature
from posthog.models.personal_api_key import PersonalAPIKey, get_organization_personal_api_keys
-from posthog.permissions import OrganizationAdminReadPermissions
+from posthog.permissions import OrganizationAdminReadPermissions, PremiumFeaturePermission
class OrganizationPersonalAPIKeyOwnerSerializer(serializers.Serializer):
@@ -83,7 +84,8 @@ class OrganizationPersonalAPIKeyViewSet(
):
scope_object = "INTERNAL"
serializer_class = OrganizationPersonalAPIKeySerializer
- permission_classes = [OrganizationAdminReadPermissions]
+ permission_classes = [OrganizationAdminReadPermissions, PremiumFeaturePermission]
+ premium_feature = AvailableFeature.ORGANIZATION_SECURITY_SETTINGS
queryset = PersonalAPIKey.objects.none()
# PersonalAPIKey has no organization_id; the parent lookup resolves through the owner's membership.
filter_rewrite_rules = {"organization_id": "user__organization_membership__organization_id"}
diff --git a/posthog/api/test/test_organization_personal_api_key.py b/posthog/api/test/test_organization_personal_api_key.py
index 4b7a7d517549..92d702b20ce4 100644
--- a/posthog/api/test/test_organization_personal_api_key.py
+++ b/posthog/api/test/test_organization_personal_api_key.py
@@ -2,11 +2,19 @@ from posthog.test.base import APIBaseTest
from rest_framework import status
+from posthog.constants import AvailableFeature
from posthog.models import Organization, OrganizationMembership, PersonalAPIKey, Team, User
from posthog.models.utils import generate_random_token_personal, hash_key_value
class TestOrganizationPersonalAPIKeyAPI(APIBaseTest):
+ def setUp(self):
+ super().setUp()
+ self.organization.available_product_features = [
+ {"key": AvailableFeature.ORGANIZATION_SECURITY_SETTINGS, "name": "organization_security_settings"}
+ ]
+ self.organization.save()
+
def _create_key(self, user, **kwargs):
return PersonalAPIKey.objects.create(
user=user,
@@ -79,3 +87,13 @@ class TestOrganizationPersonalAPIKeyAPI(APIBaseTest):
self._create_key(outsider, label="outsider")
assert self.client.get(self._url()).json()["count"] == 0
+
+ def test_requires_organization_security_settings_feature(self):
+ self._set_level(OrganizationMembership.Level.ADMIN)
+ self.organization.available_product_features = []
+ self.organization.save()
+
+ response = self.client.get(self._url())
+
+ assert response.status_code == status.HTTP_402_PAYMENT_REQUIRED
+ assert response.json()["code"] == "payment_required"