2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-08 03:40:11 +08:00
|
|
|
class EmailUpdater
|
|
|
|
include HasErrors
|
|
|
|
|
|
|
|
attr_reader :user
|
|
|
|
|
|
|
|
def self.human_attribute_name(name, options = {})
|
|
|
|
User.human_attribute_name(name, options)
|
|
|
|
end
|
|
|
|
|
2020-06-11 00:11:49 +08:00
|
|
|
def initialize(guardian: nil, user: nil)
|
|
|
|
@guardian = guardian
|
|
|
|
@user = user
|
2016-03-08 03:40:11 +08:00
|
|
|
end
|
|
|
|
|
2020-06-11 00:11:49 +08:00
|
|
|
def change_to(email, add: false)
|
2016-03-08 03:40:11 +08:00
|
|
|
@guardian.ensure_can_edit_email!(@user)
|
|
|
|
|
2020-06-11 00:11:49 +08:00
|
|
|
email = Email.downcase(email.strip)
|
2016-03-08 03:40:11 +08:00
|
|
|
EmailValidator.new(attributes: :email).validate_each(self, :email, email)
|
2020-06-11 00:11:49 +08:00
|
|
|
return if errors.present?
|
2016-03-08 03:40:11 +08:00
|
|
|
|
2016-03-22 02:36:26 +08:00
|
|
|
if existing_user = User.find_by_email(email)
|
2017-10-04 23:41:08 +08:00
|
|
|
if SiteSetting.hide_email_address_taken
|
|
|
|
Jobs.enqueue(:critical_user_email, type: :account_exists, user_id: existing_user.id)
|
|
|
|
else
|
2019-05-03 06:17:27 +08:00
|
|
|
error_message = +'change_email.error'
|
2017-10-04 23:41:08 +08:00
|
|
|
error_message << '_staged' if existing_user.staged?
|
|
|
|
errors.add(:base, I18n.t(error_message))
|
|
|
|
end
|
2016-03-22 02:36:26 +08:00
|
|
|
end
|
2016-03-08 03:40:11 +08:00
|
|
|
|
2020-06-11 00:11:49 +08:00
|
|
|
return if errors.present? || existing_user.present?
|
2016-03-08 03:40:11 +08:00
|
|
|
|
2020-06-11 00:11:49 +08:00
|
|
|
old_email = @user.email if !add
|
2016-03-08 03:40:11 +08:00
|
|
|
|
2020-06-11 00:11:49 +08:00
|
|
|
if @guardian.is_staff? && @guardian.user != @user
|
|
|
|
StaffActionLogger.new(@guardian.user).log_add_email(@user)
|
|
|
|
else
|
2020-06-16 18:30:58 +08:00
|
|
|
UserHistory.create!(action: UserHistory.actions[:add_email], acting_user_id: @user.id)
|
2020-06-11 00:11:49 +08:00
|
|
|
end
|
2020-02-20 07:52:21 +08:00
|
|
|
|
2021-03-10 20:49:26 +08:00
|
|
|
change_req = EmailChangeRequest.find_or_initialize_by(user_id: @user.id, new_email: email)
|
|
|
|
|
2020-06-11 00:11:49 +08:00
|
|
|
if change_req.new_record?
|
2021-03-10 20:49:26 +08:00
|
|
|
change_req.requested_by = @guardian.user
|
2020-06-11 00:11:49 +08:00
|
|
|
change_req.old_email = old_email
|
|
|
|
change_req.new_email = email
|
|
|
|
end
|
|
|
|
|
2020-06-11 19:53:41 +08:00
|
|
|
if change_req.change_state.blank? || change_req.change_state == EmailChangeRequest.states[:complete]
|
2020-06-11 00:11:49 +08:00
|
|
|
change_req.change_state = if @user.staff?
|
|
|
|
# Staff users must confirm their old email address first.
|
|
|
|
EmailChangeRequest.states[:authorizing_old]
|
|
|
|
else
|
|
|
|
EmailChangeRequest.states[:authorizing_new]
|
2016-03-08 03:40:11 +08:00
|
|
|
end
|
|
|
|
end
|
2020-06-11 00:11:49 +08:00
|
|
|
|
|
|
|
if change_req.change_state == EmailChangeRequest.states[:authorizing_old]
|
|
|
|
change_req.old_email_token = @user.email_tokens.create!(email: @user.email)
|
|
|
|
send_email(add ? :confirm_old_email_add : :confirm_old_email, change_req.old_email_token)
|
|
|
|
elsif change_req.change_state == EmailChangeRequest.states[:authorizing_new]
|
|
|
|
change_req.new_email_token = @user.email_tokens.create!(email: email)
|
|
|
|
send_email(:confirm_new_email, change_req.new_email_token)
|
|
|
|
end
|
|
|
|
|
|
|
|
change_req.save!
|
2016-03-08 03:40:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def confirm(token)
|
|
|
|
confirm_result = nil
|
|
|
|
|
|
|
|
User.transaction do
|
|
|
|
result = EmailToken.atomic_confirm(token)
|
|
|
|
if result[:success]
|
|
|
|
token = result[:email_token]
|
|
|
|
@user = token.user
|
|
|
|
|
2020-06-11 00:11:49 +08:00
|
|
|
change_req = @user.email_change_requests
|
2016-03-08 03:40:11 +08:00
|
|
|
.where('old_email_token_id = :token_id OR new_email_token_id = :token_id', token_id: token.id)
|
|
|
|
.first
|
|
|
|
|
|
|
|
case change_req.try(:change_state)
|
|
|
|
when EmailChangeRequest.states[:authorizing_old]
|
2020-06-11 00:11:49 +08:00
|
|
|
change_req.update!(
|
|
|
|
change_state: EmailChangeRequest.states[:authorizing_new],
|
|
|
|
new_email_token: @user.email_tokens.create(email: change_req.new_email)
|
|
|
|
)
|
|
|
|
send_email(:confirm_new_email, change_req.new_email_token)
|
2016-03-08 03:40:11 +08:00
|
|
|
confirm_result = :authorizing_new
|
|
|
|
when EmailChangeRequest.states[:authorizing_new]
|
2020-06-11 00:11:49 +08:00
|
|
|
change_req.update!(change_state: EmailChangeRequest.states[:complete])
|
|
|
|
if !@user.staff?
|
|
|
|
# Send an email notification only to users who did not confirm old
|
|
|
|
# email.
|
|
|
|
send_email_notification(change_req.old_email, change_req.new_email)
|
|
|
|
end
|
|
|
|
update_user_email(change_req.old_email, change_req.new_email)
|
2016-03-08 03:40:11 +08:00
|
|
|
confirm_result = :complete
|
|
|
|
end
|
|
|
|
else
|
|
|
|
errors.add(:base, I18n.t('change_email.already_done'))
|
|
|
|
confirm_result = :error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
confirm_result || :error
|
|
|
|
end
|
|
|
|
|
2020-06-11 00:11:49 +08:00
|
|
|
def update_user_email(old_email, new_email)
|
|
|
|
if old_email.present?
|
|
|
|
@user.user_emails.find_by(email: old_email).update!(email: new_email)
|
|
|
|
else
|
|
|
|
@user.user_emails.create!(email: new_email)
|
|
|
|
end
|
2020-09-29 07:45:45 +08:00
|
|
|
@user.reload
|
2016-03-08 03:40:11 +08:00
|
|
|
|
2020-07-16 20:51:30 +08:00
|
|
|
DiscourseEvent.trigger(:user_updated, @user)
|
2020-06-11 00:11:49 +08:00
|
|
|
@user.set_automatic_groups
|
2016-03-08 03:40:11 +08:00
|
|
|
end
|
|
|
|
|
2020-06-11 00:11:49 +08:00
|
|
|
protected
|
|
|
|
|
2016-03-08 03:40:11 +08:00
|
|
|
def send_email(type, email_token)
|
2016-04-07 12:38:43 +08:00
|
|
|
Jobs.enqueue :critical_user_email,
|
2016-03-08 03:40:11 +08:00
|
|
|
to_address: email_token.email,
|
|
|
|
type: type,
|
|
|
|
user_id: @user.id,
|
|
|
|
email_token: email_token.token
|
|
|
|
end
|
|
|
|
|
2020-06-11 00:11:49 +08:00
|
|
|
def send_email_notification(old_email, new_email)
|
|
|
|
Jobs.enqueue :critical_user_email,
|
|
|
|
to_address: @user.email,
|
|
|
|
type: old_email ? :notify_old_email : :notify_old_email_add,
|
|
|
|
user_id: @user.id,
|
|
|
|
new_email: new_email
|
2020-02-20 07:52:21 +08:00
|
|
|
end
|
2016-03-08 03:40:11 +08:00
|
|
|
end
|