users: use sudo mode when changing email addresses.

When a user tries to change their email, redirect them to the confirm
password page (like Github's sudo mode) instead of having them re-enter
their password on the change email page. This is the same thing we do
when a user updates their API keys. This way we have can use the same
confirm password authentication flow for everything that needs a
password.
This commit is contained in:
evazion
2021-05-18 22:50:09 -05:00
parent 12eacbe76f
commit 24ead500f0
5 changed files with 47 additions and 36 deletions

View File

@@ -361,6 +361,17 @@ class User < ApplicationRecord
def can_receive_email?(require_verification: true)
email_address.present? && email_address.is_deliverable? && (email_address.is_verified? || !require_verification)
end
def change_email(new_email, request)
transaction do
update(email_address_attributes: { address: new_email })
if errors.none?
UserEvent.create_from_request!(self, :email_change, request)
UserMailer.email_change_confirmation(self).deliver_later
end
end
end
end
concerning :BlacklistMethods do