2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-08 02:45:33 +08:00
|
|
|
class UsersEmailController < ApplicationController
|
2018-02-01 12:17:59 +08:00
|
|
|
requires_login only: %i[index update]
|
2023-01-09 20:20:10 +08:00
|
|
|
|
2024-01-30 18:32:42 +08:00
|
|
|
skip_before_action :check_xhr, only: %i[show_confirm_old_email show_confirm_new_email]
|
2023-01-09 20:20:10 +08:00
|
|
|
|
2019-11-20 15:31:25 +08:00
|
|
|
skip_before_action :redirect_to_login_if_required,
|
2024-06-25 19:32:18 +08:00
|
|
|
:redirect_to_profile_if_required,
|
2019-11-20 15:31:25 +08:00
|
|
|
only: %i[
|
2021-01-20 08:52:25 +08:00
|
|
|
show_confirm_old_email
|
|
|
|
show_confirm_new_email
|
2024-01-30 18:32:42 +08:00
|
|
|
confirm_old_email
|
|
|
|
confirm_new_email
|
2019-11-20 15:31:25 +08:00
|
|
|
]
|
2023-01-09 20:20:10 +08:00
|
|
|
|
2016-03-08 02:45:33 +08:00
|
|
|
def index
|
|
|
|
end
|
|
|
|
|
2020-06-11 00:11:49 +08:00
|
|
|
def create
|
|
|
|
return render json: failed_json, status: 410 if !SiteSetting.enable_secondary_emails
|
|
|
|
|
|
|
|
params.require(:email)
|
|
|
|
user = fetch_user_from_params
|
|
|
|
|
|
|
|
RateLimiter.new(user, "email-hr-#{request.remote_ip}", 6, 1.hour).performed!
|
|
|
|
RateLimiter.new(user, "email-min-#{request.remote_ip}", 3, 1.minute).performed!
|
|
|
|
|
|
|
|
updater = EmailUpdater.new(guardian: guardian, user: user)
|
|
|
|
updater.change_to(params[:email], add: true)
|
|
|
|
|
|
|
|
return render_json_error(updater.errors.full_messages) if updater.errors.present?
|
|
|
|
|
|
|
|
render body: nil
|
|
|
|
rescue RateLimiter::LimitExceeded
|
|
|
|
render_json_error(I18n.t("rate_limiter.slow_down"))
|
|
|
|
end
|
|
|
|
|
2016-03-08 02:45:33 +08:00
|
|
|
def update
|
|
|
|
params.require(:email)
|
|
|
|
user = fetch_user_from_params
|
|
|
|
|
2020-06-11 00:11:49 +08:00
|
|
|
RateLimiter.new(user, "email-hr-#{request.remote_ip}", 6, 1.hour).performed!
|
|
|
|
RateLimiter.new(user, "email-min-#{request.remote_ip}", 3, 1.minute).performed!
|
2016-03-08 02:45:33 +08:00
|
|
|
|
2020-06-04 13:09:10 +08:00
|
|
|
updater = EmailUpdater.new(guardian: guardian, user: user)
|
2016-03-08 03:40:11 +08:00
|
|
|
updater.change_to(params[:email])
|
2016-03-08 02:45:33 +08:00
|
|
|
|
2016-03-08 03:40:11 +08:00
|
|
|
return render_json_error(updater.errors.full_messages) if updater.errors.present?
|
2016-03-08 02:45:33 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil
|
2016-03-08 02:45:33 +08:00
|
|
|
rescue RateLimiter::LimitExceeded
|
|
|
|
render_json_error(I18n.t("rate_limiter.slow_down"))
|
|
|
|
end
|
|
|
|
|
2019-11-20 15:31:25 +08:00
|
|
|
def confirm_new_email
|
2024-01-30 18:32:42 +08:00
|
|
|
change_request = load_change_request(:new)
|
2018-02-20 14:44:51 +08:00
|
|
|
|
2024-01-30 18:32:42 +08:00
|
|
|
result =
|
|
|
|
run_second_factor!(SecondFactor::Actions::ConfirmEmail, target_user: change_request.user)
|
2018-02-20 14:44:51 +08:00
|
|
|
|
2024-01-30 18:32:42 +08:00
|
|
|
if result.no_second_factors_enabled? || result.second_factor_auth_completed?
|
2019-11-20 15:31:25 +08:00
|
|
|
updater = EmailUpdater.new
|
|
|
|
if updater.confirm(params[:token]) == :complete
|
|
|
|
updater.user.user_stat.reset_bounce_score!
|
2024-01-30 18:32:42 +08:00
|
|
|
render json: success_json
|
2019-11-20 15:31:25 +08:00
|
|
|
else
|
2024-01-30 18:32:42 +08:00
|
|
|
render json: { error: I18n.t("change_email.already_done") }, status: 400
|
2018-02-20 14:44:51 +08:00
|
|
|
end
|
2019-11-20 15:31:25 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_confirm_new_email
|
2024-01-30 18:32:42 +08:00
|
|
|
return render "default/empty" if request.format.html?
|
2018-02-20 14:44:51 +08:00
|
|
|
|
2024-01-30 18:32:42 +08:00
|
|
|
change_request = load_change_request(:new)
|
2018-02-20 14:44:51 +08:00
|
|
|
|
2024-01-30 18:32:42 +08:00
|
|
|
render json: {
|
|
|
|
new_email: change_request.new_email,
|
|
|
|
old_email: change_request.old_email,
|
|
|
|
token: params[:token],
|
|
|
|
}
|
2019-11-20 15:31:25 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def confirm_old_email
|
|
|
|
load_change_request(:old)
|
|
|
|
|
2024-01-30 18:32:42 +08:00
|
|
|
updater = EmailUpdater.new
|
|
|
|
if updater.confirm(params[:token]) == :authorizing_new
|
|
|
|
render json: success_json
|
2019-11-20 15:31:25 +08:00
|
|
|
else
|
2024-01-30 18:32:42 +08:00
|
|
|
render json: { error: I18n.t("change_email.already_done") }, status: 400
|
2019-11-20 15:31:25 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_confirm_old_email
|
2024-01-30 18:32:42 +08:00
|
|
|
return render "default/empty" if request.format.html?
|
2019-11-20 15:31:25 +08:00
|
|
|
|
2024-01-30 18:32:42 +08:00
|
|
|
change_request = load_change_request(:old)
|
2019-11-20 15:31:25 +08:00
|
|
|
|
2024-01-30 18:32:42 +08:00
|
|
|
render json: {
|
|
|
|
new_email: change_request.new_email,
|
|
|
|
old_email: change_request.old_email,
|
|
|
|
token: params[:token],
|
|
|
|
}
|
2016-03-08 03:40:11 +08:00
|
|
|
end
|
|
|
|
|
2019-11-20 15:31:25 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def load_change_request(type)
|
|
|
|
expires_now
|
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
token = EmailToken.confirmable(params[:token], scope: EmailToken.scopes[:email_update])
|
2019-11-20 15:31:25 +08:00
|
|
|
|
2024-01-30 18:32:42 +08:00
|
|
|
raise Discourse::NotFound if !token || !token.user
|
|
|
|
|
|
|
|
if current_user && token.user.id != current_user.id
|
|
|
|
raise Discourse::InvalidAccess.new "You are logged in, but this email change link belongs to another user account. Please log out and try again."
|
|
|
|
end
|
|
|
|
|
|
|
|
change_request_params =
|
2019-11-20 15:31:25 +08:00
|
|
|
if type == :old
|
2024-01-30 18:32:42 +08:00
|
|
|
{ old_email_token_id: token.id, change_state: EmailChangeRequest.states[:authorizing_old] }
|
2019-11-20 15:31:25 +08:00
|
|
|
elsif type == :new
|
2024-01-30 18:32:42 +08:00
|
|
|
{ new_email_token_id: token.id, change_state: EmailChangeRequest.states[:authorizing_new] }
|
2019-11-20 15:31:25 +08:00
|
|
|
end
|
|
|
|
|
2024-01-30 18:32:42 +08:00
|
|
|
token.user&.email_change_requests&.find_by!(**change_request_params)
|
2019-11-20 15:31:25 +08:00
|
|
|
end
|
2016-03-08 02:45:33 +08:00
|
|
|
end
|