From ee33c787d111d691f511f38c3a66cc44760efb75 Mon Sep 17 00:00:00 2001 From: Jack S Date: Fri, 4 Aug 2023 12:04:57 +0100 Subject: [PATCH] send email changed notification to old email --- app/mailers/devise_notify_mailer.rb | 27 +++++++++++++++++++++++---- config/initializers/devise.rb | 2 +- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/app/mailers/devise_notify_mailer.rb b/app/mailers/devise_notify_mailer.rb index d42ff84d5..b91db566f 100644 --- a/app/mailers/devise_notify_mailer.rb +++ b/app/mailers/devise_notify_mailer.rb @@ -36,8 +36,9 @@ class DeviseNotifyMailer < Devise::Mailer def confirmation_instructions(record, token, _opts = {}) username = record.email - if email_changed(record) + if email_changed?(record) username = record.unconfirmed_email + send_email_changed_to_old_email(record) send_confirmation_email(record.unconfirmed_email, record, token, username) end send_confirmation_email(record.email, record, token, username) @@ -54,9 +55,11 @@ class DeviseNotifyMailer < Devise::Mailer Rails.application.credentials[:email_allowlist] || [] end - def email_changed(record, _opts = {}) - # TODO: do not send if user changed own email address - return true if intercept_send?(email) + def send_email_changed_to_old_email(record) + # Do not send if user changed own email + return if record.versions.last.actor == record + + return true if intercept_send?(record.email) send_email( record.email, @@ -68,6 +71,22 @@ class DeviseNotifyMailer < Devise::Mailer ) end + def email_changed?(record) + # binding.pry + # record.versions.last.changeset.has_key? "unconfirmed_email" + record.confirmable_template == User::CONFIRMABLE_TEMPLATE_ID && (record.unconfirmed_email.present? && record.unconfirmed_email != record.email) + end + + def send_confirmation_email(email, record, token, username) + url = "#{user_confirmation_url}?confirmation_token=" + + send_email( + email, + record.confirmable_template, + personalisation(record, token, url, username:), + ) + end + # def unlock_instructions(record, token, opts = {}) # super # end diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 7e4ad09b3..4759c94bd 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -130,7 +130,7 @@ Devise.setup do |config| # config.pepper = '1c69067fda44e12f0ea1e8573fcdb122e5ff7e51a28cc6944c2548af7b7cbfe1400739e2d71cdf0ab53a3b60305942301d7fd7f43ff3a51316e51438145d80ad' # Send a notification to the original email when the user's email is changed. - config.send_email_changed_notification = false + # config.send_email_changed_notification = false # Send a notification email when the user's password is changed. # config.send_password_change_notification = false