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
|
2021-11-25 15:34:39 +08:00
|
|
|
attr_reader :change_req
|
2016-03-08 03:40:11 +08:00
|
|
|
|
|
|
|
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
|
2022-02-05 01:34:38 +08:00
|
|
|
Jobs.enqueue(:critical_user_email, type: "account_exists", user_id: existing_user.id)
|
2017-10-04 23:41:08 +08:00
|
|
|
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
|
|
|
|
2021-04-05 23:01:42 +08:00
|
|
|
if add
|
|
|
|
secondary_emails_count = @user.secondary_emails.count
|
|
|
|
if secondary_emails_count >= SiteSetting.max_allowed_secondary_emails
|
|
|
|
errors.add(:base, I18n.t("change_email.max_secondary_emails_error"))
|
|
|
|
end
|
|
|
|
else
|
|
|
|
old_email = @user.email
|
|
|
|
end
|
2016-03-08 03:40:11 +08:00
|
|
|
|
2021-04-05 23:01:42 +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
|
|
|
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-11-25 15:34:39 +08:00
|
|
|
@change_req = EmailChangeRequest.find_or_initialize_by(user_id: @user.id, new_email: email)
|
2021-03-10 20:49:26 +08:00
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
if @change_req.new_record?
|
|
|
|
@change_req.requested_by = @guardian.user
|
|
|
|
@change_req.old_email = old_email
|
|
|
|
@change_req.new_email = email
|
2020-06-11 00:11:49 +08:00
|
|
|
end
|
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
if @change_req.change_state.blank? || @change_req.change_state == EmailChangeRequest.states[:complete]
|
|
|
|
@change_req.change_state = if @user.staff?
|
2020-06-11 00:11:49 +08:00
|
|
|
# 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
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
if @change_req.change_state == EmailChangeRequest.states[:authorizing_old]
|
|
|
|
@change_req.old_email_token = @user.email_tokens.create!(email: @user.email, scope: EmailToken.scopes[:email_update])
|
|
|
|
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, scope: EmailToken.scopes[:email_update])
|
|
|
|
send_email(:confirm_new_email, @change_req.new_email_token)
|
2020-06-11 00:11:49 +08:00
|
|
|
end
|
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
@change_req.save!
|
|
|
|
@change_req
|
2016-03-08 03:40:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def confirm(token)
|
|
|
|
confirm_result = nil
|
|
|
|
|
|
|
|
User.transaction do
|
2021-11-25 15:34:39 +08:00
|
|
|
email_token = EmailToken.confirmable(token, scope: EmailToken.scopes[:email_update])
|
|
|
|
if email_token.blank?
|
2016-03-08 03:40:11 +08:00
|
|
|
errors.add(:base, I18n.t('change_email.already_done'))
|
|
|
|
confirm_result = :error
|
2021-11-25 15:34:39 +08:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
email_token.update!(confirmed: true)
|
|
|
|
|
|
|
|
@user = email_token.user
|
|
|
|
@change_req = @user.email_change_requests
|
|
|
|
.where('old_email_token_id = :token_id OR new_email_token_id = :token_id', token_id: email_token.id)
|
|
|
|
.first
|
|
|
|
|
|
|
|
case @change_req.try(:change_state)
|
|
|
|
when EmailChangeRequest.states[:authorizing_old]
|
|
|
|
@change_req.update!(
|
|
|
|
change_state: EmailChangeRequest.states[:authorizing_new],
|
|
|
|
new_email_token: @user.email_tokens.create!(email: @change_req.new_email, scope: EmailToken.scopes[:email_update])
|
|
|
|
)
|
|
|
|
send_email(:confirm_new_email, @change_req.new_email_token)
|
|
|
|
confirm_result = :authorizing_new
|
|
|
|
when EmailChangeRequest.states[:authorizing_new]
|
|
|
|
@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)
|
|
|
|
confirm_result = :complete
|
2016-03-08 03:40:11 +08:00
|
|
|
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
|