2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
class Profiles::EmailsController < Profiles::ApplicationController
|
2018-03-17 18:26:18 +05:30
|
|
|
before_action :find_email, only: [:destroy, :resend_confirmation_instructions]
|
2020-10-04 03:57:07 +05:30
|
|
|
before_action -> { rate_limit!(:profile_add_new_email) }, only: [:create]
|
|
|
|
before_action -> { rate_limit!(:profile_resend_email_confirmation) }, only: [:resend_confirmation_instructions]
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def index
|
2018-03-17 18:26:18 +05:30
|
|
|
@primary_email = current_user.email
|
|
|
|
@emails = current_user.emails.order_id_desc
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2018-03-17 18:26:18 +05:30
|
|
|
@email = Emails::CreateService.new(current_user, email_params.merge(user: current_user)).execute
|
|
|
|
unless @email.errors.blank?
|
2015-09-11 14:41:01 +05:30
|
|
|
flash[:alert] = @email.errors.full_messages.first
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
redirect_to profile_emails_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2018-03-17 18:26:18 +05:30
|
|
|
Emails::DestroyService.new(current_user, user: current_user).execute(@email)
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
respond_to do |format|
|
2018-11-18 11:00:15 +05:30
|
|
|
format.html { redirect_to profile_emails_url, status: :found }
|
2016-06-02 11:05:42 +05:30
|
|
|
format.js { head :ok }
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def resend_confirmation_instructions
|
|
|
|
if Emails::ConfirmService.new(current_user, user: current_user).execute(@email)
|
2019-09-04 21:01:54 +05:30
|
|
|
flash[:notice] = _("Confirmation email sent to %{email}") % { email: @email.email }
|
2018-03-17 18:26:18 +05:30
|
|
|
else
|
2019-09-04 21:01:54 +05:30
|
|
|
flash[:alert] = _("There was a problem sending the confirmation email")
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to profile_emails_url
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
private
|
|
|
|
|
2020-10-04 03:57:07 +05:30
|
|
|
def rate_limit!(action)
|
|
|
|
rate_limiter = ::Gitlab::ApplicationRateLimiter
|
|
|
|
|
|
|
|
if rate_limiter.throttled?(action, scope: current_user)
|
|
|
|
rate_limiter.log_request(request, action, current_user)
|
|
|
|
|
|
|
|
redirect_back_or_default(options: { alert: _('This action has been performed too many times. Try again later.') })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def email_params
|
|
|
|
params.require(:email).permit(:email)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
def find_email
|
|
|
|
@email = current_user.emails.find(params[:id])
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|