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
|
|
|
|
|
|
|
|
def self.human_attribute_name(name, options = {})
|
|
|
|
User.human_attribute_name(name, options)
|
|
|
|
end
|
|
|
|
|
2020-06-10 12:11:49 -04:00
|
|
|
def initialize(guardian: nil, user: nil)
|
|
|
|
@guardian = guardian
|
|
|
|
@user = user
|
2016-03-07 14:40:11 -05:00
|
|
|
end
|
|
|
|
|
2020-06-10 12:11:49 -04:00
|
|
|
def change_to(email, add: false)
|
2016-03-07 14:40:11 -05:00
|
|
|
@guardian.ensure_can_edit_email!(@user)
|
|
|
|
|
2020-06-10 12:11:49 -04:00
|
|
|
email = Email.downcase(email.strip)
|
2016-03-07 14:40:11 -05:00
|
|
|
EmailValidator.new(attributes: :email).validate_each(self, :email, email)
|
2020-06-10 12:11:49 -04:00
|
|
|
return if errors.present?
|
2016-03-07 14:40:11 -05:00
|
|
|
|
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
|
|
|
|
2021-04-05 11:01:42 -04: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-07 14:40:11 -05:00
|
|
|
|
2021-04-05 11:01:42 -04:00
|
|
|
return if errors.present? || existing_user.present?
|
2016-03-07 14:40:11 -05:00
|
|
|
|
2020-06-10 12:11:49 -04:00
|
|
|
if @guardian.is_staff? && @guardian.user != @user
|
|
|
|
StaffActionLogger.new(@guardian.user).log_add_email(@user)
|
|
|
|
else
|
2020-06-16 06:30:58 -04:00
|
|
|
UserHistory.create!(action: UserHistory.actions[:add_email], acting_user_id: @user.id)
|
2020-06-10 12:11:49 -04:00
|
|
|
end
|
2020-02-19 18:52:21 -05:00
|
|
|
|
2021-03-10 07:49:26 -05:00
|
|
|
change_req = EmailChangeRequest.find_or_initialize_by(user_id: @user.id, new_email: email)
|
|
|
|
|
2020-06-10 12:11:49 -04:00
|
|
|
if change_req.new_record?
|
2021-03-10 07:49:26 -05:00
|
|
|
change_req.requested_by = @guardian.user
|
2020-06-10 12:11:49 -04:00
|
|
|
change_req.old_email = old_email
|
|
|
|
change_req.new_email = email
|
|
|
|
end
|
|
|
|
|
2020-06-11 07:53:41 -04:00
|
|
|
if change_req.change_state.blank? || change_req.change_state == EmailChangeRequest.states[:complete]
|
2020-06-10 12:11:49 -04: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-07 14:40:11 -05:00
|
|
|
end
|
|
|
|
end
|
2020-06-10 12:11:49 -04: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-07 14:40:11 -05: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-10 12:11:49 -04:00
|
|
|
change_req = @user.email_change_requests
|
2016-03-07 14:40:11 -05: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-10 12:11:49 -04: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-07 14:40:11 -05:00
|
|
|
confirm_result = :authorizing_new
|
|
|
|
when EmailChangeRequest.states[:authorizing_new]
|
2020-06-10 12:11:49 -04: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-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
|
|
|
|
|
|
|
|
confirm_result || :error
|
|
|
|
end
|
|
|
|
|
2020-06-10 12:11:49 -04: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-28 19:45:45 -04:00
|
|
|
@user.reload
|
2016-03-07 14:40:11 -05:00
|
|
|
|
2020-07-16 08:51:30 -04:00
|
|
|
DiscourseEvent.trigger(:user_updated, @user)
|
2020-06-10 12:11:49 -04:00
|
|
|
@user.set_automatic_groups
|
2016-03-07 14:40:11 -05:00
|
|
|
end
|
|
|
|
|
2020-06-10 12:11:49 -04:00
|
|
|
protected
|
|
|
|
|
2016-03-07 14:40:11 -05:00
|
|
|
def send_email(type, email_token)
|
2016-04-07 00:38:43 -04:00
|
|
|
Jobs.enqueue :critical_user_email,
|
2016-03-07 14:40:11 -05:00
|
|
|
to_address: email_token.email,
|
|
|
|
type: type,
|
|
|
|
user_id: @user.id,
|
|
|
|
email_token: email_token.token
|
|
|
|
end
|
|
|
|
|
2020-06-10 12:11:49 -04: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-19 18:52:21 -05:00
|
|
|
end
|
2016-03-07 14:40:11 -05:00
|
|
|
end
|