2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-07 12:45:33 -06:00
|
|
|
class UsersEmailController < ApplicationController
|
|
|
|
|
2018-01-31 22:17:59 -06:00
|
|
|
requires_login only: [:index, :update]
|
2016-03-07 13:40:11 -06:00
|
|
|
|
2019-11-20 01:31:25 -06:00
|
|
|
skip_before_action :check_xhr, only: [
|
|
|
|
:confirm_old_email,
|
|
|
|
:show_confirm_old_email,
|
|
|
|
:confirm_new_email,
|
|
|
|
:show_confirm_new_email
|
|
|
|
]
|
|
|
|
|
|
|
|
skip_before_action :redirect_to_login_if_required, only: [
|
|
|
|
:confirm_old_email,
|
|
|
|
:show_confirm_old_email,
|
|
|
|
:confirm_new_email,
|
|
|
|
:show_confirm_new_email
|
|
|
|
]
|
|
|
|
|
|
|
|
before_action :require_login, only: [
|
|
|
|
:confirm_old_email,
|
|
|
|
:show_confirm_old_email,
|
|
|
|
:confirm_new_email,
|
|
|
|
:show_confirm_new_email
|
|
|
|
]
|
2016-03-07 12:45:33 -06:00
|
|
|
|
|
|
|
def index
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
params.require(:email)
|
|
|
|
user = fetch_user_from_params
|
|
|
|
|
|
|
|
RateLimiter.new(user, "change-email-hr-#{request.remote_ip}", 6, 1.hour).performed!
|
|
|
|
RateLimiter.new(user, "change-email-min-#{request.remote_ip}", 3, 1.minute).performed!
|
|
|
|
|
2016-03-07 13:40:11 -06:00
|
|
|
updater = EmailUpdater.new(guardian, user)
|
|
|
|
updater.change_to(params[:email])
|
2016-03-07 12:45:33 -06:00
|
|
|
|
2016-03-07 13:40:11 -06:00
|
|
|
if updater.errors.present?
|
|
|
|
return render_json_error(updater.errors.full_messages)
|
|
|
|
end
|
2016-03-07 12:45:33 -06:00
|
|
|
|
2017-08-30 23:06:56 -05:00
|
|
|
render body: nil
|
2016-03-07 12:45:33 -06:00
|
|
|
rescue RateLimiter::LimitExceeded
|
|
|
|
render_json_error(I18n.t("rate_limiter.slow_down"))
|
|
|
|
end
|
|
|
|
|
2019-11-20 01:31:25 -06:00
|
|
|
def confirm_new_email
|
|
|
|
load_change_request(:new)
|
|
|
|
|
|
|
|
if @change_request&.change_state != EmailChangeRequest.states[:authorizing_new]
|
|
|
|
@error = I18n.t("change_email.already_done")
|
|
|
|
end
|
2018-02-20 00:44:51 -06:00
|
|
|
|
2019-11-20 01:31:25 -06:00
|
|
|
redirect_url = path("/u/confirm-new-email/#{params[:token]}")
|
2018-02-20 00:44:51 -06:00
|
|
|
|
2019-11-20 01:31:25 -06:00
|
|
|
if !@error && @user.totp_enabled? && !@user.authenticate_second_factor(params[:second_factor_token], params[:second_factor_method].to_i)
|
|
|
|
RateLimiter.new(nil, "second-factor-min-#{request.remote_ip}", 3, 1.minute).performed!
|
|
|
|
flash[:invalid_second_factor] = true
|
|
|
|
redirect_to redirect_url
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if !@error
|
|
|
|
updater = EmailUpdater.new
|
|
|
|
if updater.confirm(params[:token]) == :complete
|
|
|
|
updater.user.user_stat.reset_bounce_score!
|
|
|
|
else
|
|
|
|
@error = I18n.t("change_email.already_done")
|
2018-02-20 00:44:51 -06:00
|
|
|
end
|
2019-11-20 01:31:25 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
if @error
|
|
|
|
flash[:error] = @error
|
|
|
|
redirect_to redirect_url
|
|
|
|
else
|
|
|
|
redirect_to "#{redirect_url}?done=true"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_confirm_new_email
|
|
|
|
load_change_request(:new)
|
|
|
|
|
|
|
|
if params[:done].to_s == "true"
|
|
|
|
@done = true
|
|
|
|
end
|
2018-02-20 00:44:51 -06:00
|
|
|
|
2019-11-20 01:31:25 -06:00
|
|
|
if @change_request&.change_state != EmailChangeRequest.states[:authorizing_new]
|
|
|
|
@error = I18n.t("change_email.already_done")
|
|
|
|
end
|
2018-02-20 00:44:51 -06:00
|
|
|
|
2019-11-20 01:31:25 -06:00
|
|
|
@show_invalid_second_factor_error = flash[:invalid_second_factor]
|
2018-02-20 00:44:51 -06:00
|
|
|
|
2019-11-20 01:31:25 -06:00
|
|
|
if !@error
|
|
|
|
if @user.totp_enabled?
|
|
|
|
@backup_codes_enabled = @user.backup_codes_enabled?
|
|
|
|
if params[:show_backup].to_s == "true" && @backup_codes_enabled
|
|
|
|
@show_backup_codes = true
|
|
|
|
else
|
|
|
|
@show_second_factor = true
|
|
|
|
end
|
2017-12-21 19:18:12 -06:00
|
|
|
end
|
|
|
|
|
2019-11-20 01:31:25 -06:00
|
|
|
@to_email = @change_request.new_email
|
|
|
|
end
|
|
|
|
|
|
|
|
render layout: 'no_ember'
|
|
|
|
end
|
|
|
|
|
|
|
|
def confirm_old_email
|
|
|
|
load_change_request(:old)
|
|
|
|
|
|
|
|
if @change_request&.change_state != EmailChangeRequest.states[:authorizing_old]
|
|
|
|
@error = I18n.t("change_email.already_done")
|
|
|
|
end
|
|
|
|
|
|
|
|
redirect_url = path("/u/confirm-old-email/#{params[:token]}")
|
|
|
|
|
|
|
|
if !@error
|
|
|
|
updater = EmailUpdater.new
|
|
|
|
if updater.confirm(params[:token]) != :authorizing_new
|
|
|
|
@error = I18n.t("change_email.already_done")
|
2018-02-20 00:44:51 -06:00
|
|
|
end
|
2017-02-20 03:37:01 -06:00
|
|
|
end
|
2016-03-07 13:40:11 -06:00
|
|
|
|
2019-11-20 01:31:25 -06:00
|
|
|
if @error
|
|
|
|
flash[:error] = @error
|
|
|
|
redirect_to redirect_url
|
|
|
|
else
|
|
|
|
redirect_to "#{redirect_url}?done=true"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_confirm_old_email
|
|
|
|
load_change_request(:old)
|
|
|
|
|
|
|
|
if @change_request&.change_state != EmailChangeRequest.states[:authorizing_old]
|
|
|
|
@error = I18n.t("change_email.already_done")
|
|
|
|
end
|
|
|
|
|
|
|
|
if params[:done].to_s == "true"
|
|
|
|
@almost_done = true
|
|
|
|
end
|
|
|
|
|
|
|
|
if !@error
|
|
|
|
@from_email = @user.email
|
|
|
|
@to_email = @change_request.new_email
|
|
|
|
end
|
|
|
|
|
2016-03-07 13:40:11 -06:00
|
|
|
render layout: 'no_ember'
|
|
|
|
end
|
|
|
|
|
2019-11-20 01:31:25 -06:00
|
|
|
private
|
|
|
|
|
|
|
|
def load_change_request(type)
|
|
|
|
expires_now
|
|
|
|
|
|
|
|
@token = EmailToken.confirmable(params[:token])
|
|
|
|
|
|
|
|
if @token
|
|
|
|
if type == :old
|
|
|
|
@change_request = @token.user&.email_change_requests.where(old_email_token_id: @token.id).first
|
|
|
|
elsif type == :new
|
|
|
|
@change_request = @token.user&.email_change_requests.where(new_email_token_id: @token.id).first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@user = @token&.user
|
|
|
|
|
|
|
|
if (!@user || !@change_request)
|
|
|
|
@error = I18n.t("change_email.already_done")
|
|
|
|
end
|
|
|
|
|
|
|
|
if current_user.id != @user&.id
|
|
|
|
@error = I18n.t 'change_email.wrong_account_error'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def require_login
|
|
|
|
if !current_user
|
|
|
|
redirect_to_login
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-07 12:45:33 -06:00
|
|
|
end
|