Fix duplicate invitation sending email before checking constraints#48854
Open
kota65535 wants to merge 2 commits intokeycloak:mainfrom
Open
Fix duplicate invitation sending email before checking constraints#48854kota65535 wants to merge 2 commits intokeycloak:mainfrom
kota65535 wants to merge 2 commits intokeycloak:mainfrom
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes: #45553
Problem
inviteExistingUser()(the API that invites a user by ID) was missing validation checks thatinviteUser()already had:In both cases, the DB unique constraint (
UK_ORG_INVITATION_EMAIL) would eventually reject the duplicate at transaction commit time. However, since JPA'sem.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()thatinviteUser()already performs:organization.isMember(user)— reject invitations to existing members with 409 CONFLICTinvitationManager.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.