Merge pull request #10604 from mfo/US/fix-race-with-lock

pan pan
This commit is contained in:
mfo 2024-07-12 14:56:33 +00:00 committed by GitHub
commit fe37f1acfe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 9 deletions

View file

@ -9,11 +9,11 @@ module Users
def update_email def update_email
requested_user = User.find_by(email: requested_email) requested_user = User.find_by(email: requested_email)
if requested_user.present? && current_user.ask_for_merge(requested_user) if requested_user.present?
current_user.update(unconfirmed_email: nil) current_user.ask_for_merge(requested_user)
flash.notice = t('devise.registrations.update_needs_confirmation') flash.notice = t('devise.registrations.update_needs_confirmation')
elsif current_user.update(update_email_params) elsif update_with_lock(update_email_params)
current_user.update(requested_merge_into: nil) current_user.update(requested_merge_into: nil)
flash.notice = t('devise.registrations.update_needs_confirmation') flash.notice = t('devise.registrations.update_needs_confirmation')
@ -22,6 +22,15 @@ module Users
end end
redirect_to profil_path redirect_to profil_path
rescue ActiveRecord::RecordInvalid => e
flash.alert = e.record.errors.full_messages
redirect_to profil_path
end
def update_with_lock(params)
current_user.with_lock do
current_user.update!(params)
end
end end
def transfer_all_dossiers def transfer_all_dossiers

View file

@ -257,12 +257,8 @@ class User < ApplicationRecord
end end
def ask_for_merge(requested_user) def ask_for_merge(requested_user)
if update(requested_merge_into: requested_user) update!(requested_merge_into: requested_user, unconfirmed_email: nil)
UserMailer.ask_for_merge(self, requested_user.email).deliver_later UserMailer.ask_for_merge(self, requested_user.email).deliver_later
return true
else
return false
end
end end
def send_devise_notification(notification, *args) def send_devise_notification(notification, *args)