2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-07 14:40:11 -05:00
|
|
|
class EmailUpdater
|
|
|
|
include HasErrors
|
|
|
|
|
|
|
|
attr_reader :user
|
|
|
|
|
2020-02-19 18:52:21 -05:00
|
|
|
def initialize(guardian: nil, user: nil, initiating_user: nil)
|
2016-03-07 14:40:11 -05:00
|
|
|
@guardian = guardian
|
|
|
|
@user = user
|
2020-02-19 18:52:21 -05:00
|
|
|
@initiating_user = initiating_user
|
2016-03-07 14:40:11 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.human_attribute_name(name, options = {})
|
2016-03-07 14:40:11 -05:00
|
|
|
User.human_attribute_name(name, options)
|
|
|
|
end
|
|
|
|
|
2020-02-19 22:42:57 -05:00
|
|
|
def changing_staff_user_email?
|
2016-03-07 14:40:11 -05:00
|
|
|
@user.staff?
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_to(email_input)
|
|
|
|
@guardian.ensure_can_edit_email!(@user)
|
|
|
|
|
|
|
|
email = Email.downcase(email_input.strip)
|
|
|
|
EmailValidator.new(attributes: :email).validate_each(self, :email, email)
|
|
|
|
|
2016-03-21 14:36:26 -04:00
|
|
|
if existing_user = User.find_by_email(email)
|
2017-10-04 11:41:08 -04:00
|
|
|
if SiteSetting.hide_email_address_taken
|
|
|
|
Jobs.enqueue(:critical_user_email, type: :account_exists, user_id: existing_user.id)
|
|
|
|
else
|
2019-05-02 18:17:27 -04:00
|
|
|
error_message = +'change_email.error'
|
2017-10-04 11:41:08 -04:00
|
|
|
error_message << '_staged' if existing_user.staged?
|
|
|
|
errors.add(:base, I18n.t(error_message))
|
|
|
|
end
|
2016-03-21 14:36:26 -04:00
|
|
|
end
|
2016-03-07 14:40:11 -05:00
|
|
|
|
2017-10-04 11:41:08 -04:00
|
|
|
if errors.blank? && existing_user.nil?
|
2016-03-07 14:40:11 -05:00
|
|
|
args = {
|
|
|
|
old_email: @user.email,
|
|
|
|
new_email: email,
|
|
|
|
}
|
|
|
|
|
2020-02-19 18:52:21 -05:00
|
|
|
args, email_token = prepare_change_request(args)
|
2016-12-21 04:00:45 -05:00
|
|
|
@user.email_change_requests.create!(args)
|
2016-03-07 14:40:11 -05:00
|
|
|
|
2020-02-19 22:42:57 -05:00
|
|
|
if initiating_admin_changing_another_user_email? && !changing_staff_user_email?
|
2020-02-19 18:52:21 -05:00
|
|
|
auto_confirm_and_send_password_reset(email_token)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-03-07 14:40:11 -05:00
|
|
|
if args[:change_state] == EmailChangeRequest.states[:authorizing_new]
|
|
|
|
send_email(:confirm_new_email, email_token)
|
|
|
|
elsif args[:change_state] == EmailChangeRequest.states[:authorizing_old]
|
|
|
|
send_email(:confirm_old_email, email_token)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def confirm(token)
|
|
|
|
confirm_result = nil
|
|
|
|
change_req = nil
|
|
|
|
|
|
|
|
User.transaction do
|
|
|
|
result = EmailToken.atomic_confirm(token)
|
|
|
|
if result[:success]
|
|
|
|
token = result[:email_token]
|
|
|
|
@user = token.user
|
|
|
|
|
|
|
|
change_req = user.email_change_requests
|
2017-07-27 21:20:09 -04:00
|
|
|
.where('old_email_token_id = :token_id OR new_email_token_id = :token_id', token_id: token.id)
|
|
|
|
.first
|
2016-03-07 14:40:11 -05:00
|
|
|
|
|
|
|
# Simple state machine
|
|
|
|
case change_req.try(:change_state)
|
|
|
|
when EmailChangeRequest.states[:authorizing_old]
|
|
|
|
new_token = user.email_tokens.create(email: change_req.new_email)
|
|
|
|
change_req.update_columns(change_state: EmailChangeRequest.states[:authorizing_new],
|
|
|
|
new_email_token_id: new_token.id)
|
|
|
|
send_email(:confirm_new_email, new_token)
|
|
|
|
confirm_result = :authorizing_new
|
|
|
|
when EmailChangeRequest.states[:authorizing_new]
|
|
|
|
change_req.update_column(:change_state, EmailChangeRequest.states[:complete])
|
2017-11-07 22:35:53 -05:00
|
|
|
user.primary_email.update!(email: token.email)
|
2018-05-08 15:23:45 -04:00
|
|
|
user.set_automatic_groups
|
2016-03-07 14:40:11 -05:00
|
|
|
confirm_result = :complete
|
|
|
|
end
|
|
|
|
else
|
|
|
|
errors.add(:base, I18n.t('change_email.already_done'))
|
|
|
|
confirm_result = :error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if confirm_result == :complete && change_req.old_email_token_id.blank?
|
|
|
|
notify_old(change_req.old_email, token.email)
|
|
|
|
end
|
|
|
|
|
|
|
|
confirm_result || :error
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def notify_old(old_email, new_email)
|
|
|
|
Jobs.enqueue :critical_user_email,
|
|
|
|
to_address: old_email,
|
|
|
|
type: :notify_old_email,
|
|
|
|
user_id: @user.id
|
|
|
|
end
|
2016-03-07 14:40:11 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def send_email(type, email_token)
|
|
|
|
Jobs.enqueue :critical_user_email,
|
|
|
|
to_address: email_token.email,
|
|
|
|
type: type,
|
|
|
|
user_id: @user.id,
|
|
|
|
email_token: email_token.token
|
|
|
|
end
|
2016-03-07 14:40:11 -05:00
|
|
|
|
2020-02-19 22:42:57 -05:00
|
|
|
# regular users or changes requested by admin are always
|
|
|
|
# authorizing new only (as long as they are not changing their
|
|
|
|
# own email!)
|
|
|
|
#
|
|
|
|
# however even if an admin requests an email change for another
|
|
|
|
# admin the other admin must always confirm their old email
|
2020-02-19 18:52:21 -05:00
|
|
|
def prepare_change_request(args)
|
2020-02-19 22:42:57 -05:00
|
|
|
if changing_staff_user_email?
|
2020-02-19 18:52:21 -05:00
|
|
|
args[:change_state] = EmailChangeRequest.states[:authorizing_old]
|
|
|
|
email_token = @user.email_tokens.create!(email: args[:old_email])
|
|
|
|
args[:old_email_token] = email_token
|
2020-02-19 22:42:57 -05:00
|
|
|
else
|
|
|
|
args[:change_state] = EmailChangeRequest.states[:authorizing_new]
|
|
|
|
email_token = @user.email_tokens.create!(email: args[:new_email])
|
|
|
|
args[:new_email_token] = email_token
|
2020-02-19 18:52:21 -05:00
|
|
|
end
|
|
|
|
[args, email_token]
|
|
|
|
end
|
|
|
|
|
|
|
|
def initiating_admin_changing_another_user_email?
|
|
|
|
@initiating_user&.admin? && @initiating_user != @user
|
|
|
|
end
|
|
|
|
|
|
|
|
def auto_confirm_and_send_password_reset(email_token)
|
|
|
|
confirm(email_token.token)
|
|
|
|
reset_email_token = @user.email_tokens.create(email: @user.email)
|
|
|
|
send_email(:forgot_password, reset_email_token)
|
|
|
|
end
|
2016-03-07 14:40:11 -05:00
|
|
|
end
|