Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding capture events for created and deleting org domains under authentication domains and sso #26654

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions posthog/api/organization_domain.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from typing import Any, cast
import posthoganalytics

from rest_framework import exceptions, request, response, serializers
from posthog.api.utils import action
Expand All @@ -11,10 +12,29 @@
from posthog.models import OrganizationDomain
from posthog.models.organization import Organization
from posthog.permissions import OrganizationAdminWritePermissions
from posthog.event_usage import groups

DOMAIN_REGEX = r"^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$"


def _capture_domain_event(request, domain: OrganizationDomain, event_type: str, properties: dict | None = None) -> None:
if not properties:
properties = {}

properties.update(
{
"domain": domain.domain,
}
)

posthoganalytics.capture(
request.user.distinct_id,
f"organization domain {event_type}",
properties=properties,
groups=groups(domain.organization),
)


class OrganizationDomainSerializer(serializers.ModelSerializer):
UPDATE_ONLY_WHEN_VERIFIED = ["jit_provisioning_enabled", "sso_enforcement"]

Expand Down Expand Up @@ -96,3 +116,38 @@ def verify(self, request: request.Request, **kw) -> response.Response:

serializer = self.get_serializer(instance=instance)
return response.Response(serializer.data)

def create(self, request: request.Request, *args: Any, **kwargs: Any) -> response.Response:
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
instance = serializer.save()

_capture_domain_event(
request,
instance,
"created",
properties={
"jit_provisioning_enabled": instance.jit_provisioning_enabled,
"sso_enforcement": instance.sso_enforcement or None,
},
)

return response.Response(serializer.data, status=201)

def destroy(self, request: request.Request, *args: Any, **kwargs: Any) -> response.Response:
instance = self.get_object()

_capture_domain_event(
request,
instance,
"deleted",
properties={
"is_verified": instance.is_verified,
"had_saml": instance.has_saml,
"had_jit_provisioning": instance.jit_provisioning_enabled,
"had_sso_enforcement": bool(instance.sso_enforcement),
},
)

instance.delete()
return response.Response(status=204)
Loading