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
|
2021-11-25 02:34:39 -05:00
|
|
|
attr_reader :change_req
|
2016-03-07 14:40:11 -05:00
|
|
|
|
|
|
|
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
|
2022-02-04 12:34:38 -05:00
|
|
|
Jobs.enqueue(:critical_user_email, type: "account_exists", user_id: existing_user.id)
|
2017-10-04 11:41:08 -04:00
|
|
|
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-11-25 02:34:39 -05:00
|
|
|
@change_req = EmailChangeRequest.find_or_initialize_by(user_id: @user.id, new_email: email)
|
2021-03-10 07:49:26 -05:00
|
|
|
|
2021-11-25 02:34:39 -05: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-10 12:11:49 -04:00
|
|
|
end
|
|
|
|
|
2021-11-25 02:34:39 -05:00
|
|
|
if @change_req.change_state.blank? || @change_req.change_state == EmailChangeRequest.states[:complete]
|
2022-09-29 17:49:17 -04:00
|
|
|
@change_req.change_state = if SiteSetting.require_change_email_confirmation || @user.staff?
|
2020-06-10 12:11:49 -04:00
|
|
|
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
|
|
|
|
2021-11-25 02:34:39 -05: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])
|
2022-02-04 18:43:53 -05:00
|
|
|
send_email(add ? "confirm_old_email_add" : "confirm_old_email", @change_req.old_email_token)
|
2021-11-25 02:34:39 -05:00
|
|
|
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])
|
2022-02-04 18:43:53 -05:00
|
|
|
send_email("confirm_new_email", @change_req.new_email_token)
|
2020-06-10 12:11:49 -04:00
|
|
|
end
|
|
|
|
|
2021-11-25 02:34:39 -05:00
|
|
|
@change_req.save!
|
|
|
|
@change_req
|
2016-03-07 14:40:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def confirm(token)
|
|
|
|
confirm_result = nil
|
|
|
|
|
|
|
|
User.transaction do
|
2021-11-25 02:34:39 -05:00
|
|
|
email_token = EmailToken.confirmable(token, scope: EmailToken.scopes[:email_update])
|
|
|
|
if email_token.blank?
|
2016-03-07 14:40:11 -05:00
|
|
|
errors.add(:base, I18n.t('change_email.already_done'))
|
|
|
|
confirm_result = :error
|
2021-11-25 02:34:39 -05: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])
|
|
|
|
)
|
2022-02-04 18:43:53 -05:00
|
|
|
send_email("confirm_new_email", @change_req.new_email_token)
|
2021-11-25 02:34:39 -05:00
|
|
|
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-07 14:40:11 -05:00
|
|
|
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,
|
2022-02-04 18:43:53 -05:00
|
|
|
type: old_email ? "notify_old_email" : "notify_old_email_add",
|
2020-06-10 12:11:49 -04:00
|
|
|
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
|