You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
2.2 KiB

Fix “Email changed” notification sometimes having wrong e-mail (#13475) * Fix “Email changed” notification sometimes having wrong e-mail Fixes #6778 The root of the issue is that `send_devise_notification` was called before the changes were properly commited to the database, causing the mailer to pick previous values if running too early. Devise's documentation provides guidance on how to handle that[1][2], however, I have found it to not be working, as the following happens, in that order: - `send_devise_notification` is called for the `email_changed` notification. In that case, `changed?` is false and `saved_changes?` is true, so if we use the former, we have the same issue. - the `after_commit` hook is called - `send_devise_notification` is called for the `confirmation_instructions` notification. In that case, `changed?` is still false, and `saved_changes?` still true, so if we use the latter, that second notification email is simply not going to be sent (as we would be queuing the notification *after* executing the after_commit hook). This is because it may be called from either an `after_update` or `after_commit` hook, the difference not being a call to `save` but the transaction actually being committed to the database. This may arguably be a bug in Devise, or Devise's notification. The proposed workaround is inspired by Devise's documentation but checks whether a transaction is open to make the call whether to immediately send the notification or defer it to the `after_commit` hook. [1]: https://www.rubydoc.info/github/plataformatec/devise/Devise%2FModels%2FAuthenticatable:send_devise_notification [2]: https://github.com/heartcombo/devise/blob/406915cb781e38255a30ad2a0609e33952b9ec50/lib/devise/models/authenticatable.rb#L133-L194 * Fix cases when sending notifications without changing the model * Defer sending if and only if in transaction including current record
4 years ago
  1. %table.email-table{ cellspacing: 0, cellpadding: 0 }
  2. %tbody
  3. %tr
  4. %td.email-body
  5. .email-container
  6. %table.content-section{ cellspacing: 0, cellpadding: 0 }
  7. %tbody
  8. %tr
  9. %td.content-cell.hero
  10. .email-row
  11. .col-6
  12. %table.column{ cellspacing: 0, cellpadding: 0 }
  13. %tbody
  14. %tr
  15. %td.column-cell.text-center.padded
  16. %table.hero-icon{ align: 'center', cellspacing: 0, cellpadding: 0 }
  17. %tbody
  18. %tr
  19. %td
  20. = image_tag full_pack_url('media/images/mailer/icon_email.png'), alt: ''
  21. %h1= t 'devise.mailer.email_changed.title'
  22. %p.lead= t 'devise.mailer.email_changed.explanation'
  23. %table.email-table{ cellspacing: 0, cellpadding: 0 }
  24. %tbody
  25. %tr
  26. %td.email-body
  27. .email-container
  28. %table.content-section{ cellspacing: 0, cellpadding: 0 }
  29. %tbody
  30. %tr
  31. %td.content-cell.content-start
  32. %table.column{ cellspacing: 0, cellpadding: 0 }
  33. %tbody
  34. %tr
  35. %td.column-cell.input-cell
  36. %table.input{ align: 'center', cellspacing: 0, cellpadding: 0 }
  37. %tbody
  38. %tr
  39. %td= @resource.unconfirmed_email
  40. %table.email-table{ cellspacing: 0, cellpadding: 0 }
  41. %tbody
  42. %tr
  43. %td.email-body
  44. .email-container
  45. %table.content-section{ cellspacing: 0, cellpadding: 0 }
  46. %tbody
  47. %tr
  48. %td.content-cell
  49. .email-row
  50. .col-6
  51. %table.column{ cellspacing: 0, cellpadding: 0 }
  52. %tbody
  53. %tr
  54. %td.column-cell.text-center.padded
  55. %p= t 'devise.mailer.email_changed.extra'