move update_email check to before_action

This commit is contained in:
simon lehericey 2021-10-25 16:57:21 +02:00
parent e3b0df09f4
commit 894e8fdd47
2 changed files with 11 additions and 4 deletions

View file

@ -1,5 +1,7 @@
module Users
class ProfilController < UserController
before_action :ensure_update_email_is_authorized, only: :update_email
def show
@waiting_transfers = current_user.dossiers.joins(:transfer).group('dossier_transfers.email').count.to_a
end
@ -11,9 +13,7 @@ module Users
end
def update_email
if current_user.instructeur? && !target_email_allowed?
flash.alert = t('.email_not_allowed', contact_email: CONTACT_EMAIL, requested_email: requested_email)
elsif current_user.update(update_email_params)
if current_user.update(update_email_params)
flash.notice = t('devise.registrations.update_needs_confirmation')
elsif current_user.errors&.details&.dig(:email)&.any? { |e| e[:error] == :taken }
UserMailer.account_already_taken(current_user, requested_email).deliver_later
@ -34,6 +34,13 @@ module Users
private
def ensure_update_email_is_authorized
if current_user.instructeur? && !target_email_allowed?
flash.alert = t('users.profil.ensure_update_email_is_authorized.email_not_allowed', contact_email: CONTACT_EMAIL, requested_email: requested_email)
redirect_to profil_path
end
end
def update_email_params
params.require(:user).permit(:email)
end

View file

@ -21,7 +21,7 @@ fr:
<br>
Si ce n'est pas votre cas, contactez le support&nbsp;:
<a href="mailto:%{contact_email}">%{contact_email}</a>
update_email:
ensure_update_email_is_authorized:
email_not_allowed: "Lemail %{requested_email} ne peut être utilisé, contactez le support : <a href='mailto:%{contact_email}'>%{contact_email}</a>"
transfer_all_dossiers:
new_transfer: