Skip to content

Fix duplicate invitation sending email before checking constraints#48854

Open
kota65535 wants to merge 2 commits intokeycloak:mainfrom
kota65535:vk/e099-
Open

Fix duplicate invitation sending email before checking constraints#48854
kota65535 wants to merge 2 commits intokeycloak:mainfrom
kota65535:vk/e099-

Conversation

@kota65535
Copy link
Copy Markdown
Contributor

Closes: #45553

Problem

inviteExistingUser() (the API that invites a user by ID) was missing validation checks that inviteUser() already had:

  • No check for whether the user is already a member of the organization
  • No check for whether the user already has a pending invitation

In both cases, the DB unique constraint (UK_ORG_INVITATION_EMAIL) would eventually reject the duplicate at transaction commit time. However, since JPA's em.persist() does not flush immediately, the invitation email was sent before the constraint violation was detected. The end result: the admin sees an error, but the user still receives an invalid invitation email that shows "Action expired" when clicked.

Fix

Added the same pre-send validation to inviteExistingUser() that inviteUser() already performs:

  1. organization.isMember(user) — reject invitations to existing members with 409 CONFLICT
  2. invitationManager.getByEmail() — reject invitations when a pending one already exists with 409 CONFLICT (expired invitations are removed and re-invitation is allowed)

These checks run before sendInvitation(), preventing unnecessary emails from being sent.

inviteExistingUser() lacked duplicate invitation and existing member
checks that inviteUser() already had. Since JPA defers the unique
constraint check to transaction commit, the email was sent before the
DB rejected the duplicate. Add the same pre-send validation to
inviteExistingUser() so the request fails early with 409 CONFLICT.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Tomohiko Ozawa <kota65535@gmail.com>
@kota65535 kota65535 changed the title fix: prevent duplicate invitation email when inviting existing user Fix duplicate invitation sending email before checking constraints May 9, 2026
@kota65535 kota65535 marked this pull request as ready for review May 9, 2026 17:48
@kota65535 kota65535 requested a review from a team as a code owner May 9, 2026 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

invite-existing-user API should reject invitations for users already in the organization

1 participant