Skip to content

Commit

Permalink
fix: allow user invites to have capitalized email domains (#20879)
Browse files Browse the repository at this point in the history
* allow user invites to have capitalized email domains

* move fix into api

* fix for api requests as well
  • Loading branch information
xrdt authored Apr 4, 2024
1 parent cdeaab3 commit 11c6e44
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 additions & 0 deletions posthog/api/organization_invite.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class Meta:
]
extra_kwargs = {"target_email": {"required": True, "allow_null": False}}

def validate_target_email(self, email: str):
local_part, domain = email.split("@")
return f"{local_part}@{domain.lower()}"

def create(self, validated_data: Dict[str, Any], *args: Any, **kwargs: Any) -> OrganizationInvite:
if OrganizationMembership.objects.filter(
organization_id=self.context["organization_id"],
Expand Down Expand Up @@ -96,6 +100,25 @@ def get_queryset(self):
.order_by(self.ordering)
)

def lowercase_email_domain(self, email: str):
# According to the email RFC https://www.rfc-editor.org/rfc/rfc1035, anything before the @ can be
# case-sensitive but the domain should not be. There have been a small number of customers who type in their emails
# with a capitalized domain. We shouldn't prevent them from inviting teammates because of this.
local_part, domain = email.split("@")
return f"{local_part}@{domain.lower()}"

def create(self, request: request.Request, **kwargs) -> response.Response:
data = cast(Any, request.data.copy())

serializer = OrganizationInviteSerializer(
data=data,
context={**self.get_serializer_context()},
)
serializer.is_valid(raise_exception=True)
serializer.save()

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

@action(methods=["POST"], detail=False, required_scopes=["organization_member:write"])
def bulk(self, request: request.Request, **kwargs) -> response.Response:
data = cast(Any, request.data)
Expand Down
2 changes: 1 addition & 1 deletion posthog/api/test/test_organization_invites.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def test_maximum_20_invites_per_request(self):
# No emails should be sent
self.assertEqual(len(mail.outbox), 0)

def test_invites_are_create_atomically(self):
def test_invites_are_created_atomically(self):
count = OrganizationInvite.objects.count()
payload = self.helper_generate_bulk_invite_payload(5)
payload[4]["target_email"] = None
Expand Down

0 comments on commit 11c6e44

Please sign in to comment.