2019-07-02 18:15:03 +02:00
|
|
|
module Users
|
|
|
|
class ProfilController < UserController
|
2021-10-25 16:57:21 +02:00
|
|
|
before_action :ensure_update_email_is_authorized, only: :update_email
|
|
|
|
|
2018-08-23 18:53:35 +02:00
|
|
|
def show
|
2021-09-20 13:26:57 +02:00
|
|
|
@waiting_transfers = current_user.dossiers.joins(:transfer).group('dossier_transfers.email').count.to_a
|
2018-08-23 18:53:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def renew_api_token
|
2018-08-24 16:45:43 +02:00
|
|
|
@token = current_administrateur.renew_api_token
|
2018-08-24 14:19:44 +02:00
|
|
|
flash.now.notice = 'Votre jeton a été regénéré.'
|
|
|
|
render :show
|
2018-08-23 18:53:35 +02:00
|
|
|
end
|
2019-07-08 10:40:50 +02:00
|
|
|
|
|
|
|
def update_email
|
2021-10-25 16:57:21 +02:00
|
|
|
if current_user.update(update_email_params)
|
2019-07-08 10:40:50 +02:00
|
|
|
flash.notice = t('devise.registrations.update_needs_confirmation')
|
2019-12-09 16:50:30 +01:00
|
|
|
elsif current_user.errors&.details&.dig(:email)&.any? { |e| e[:error] == :taken }
|
|
|
|
UserMailer.account_already_taken(current_user, requested_email).deliver_later
|
2019-07-09 17:08:27 +02:00
|
|
|
# avoid leaking information about whether an account with this email exists or not
|
2019-07-08 10:40:50 +02:00
|
|
|
flash.notice = t('devise.registrations.update_needs_confirmation')
|
|
|
|
else
|
2019-12-09 16:50:30 +01:00
|
|
|
flash.alert = current_user.errors.full_messages
|
2019-07-08 10:40:50 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to profil_path
|
|
|
|
end
|
|
|
|
|
2021-09-20 13:14:03 +02:00
|
|
|
def transfer_all_dossiers
|
|
|
|
DossierTransfer.initiate(next_owner_email, current_user.dossiers)
|
|
|
|
flash.notice = t('.new_transfer', count: current_user.dossiers.count, email: next_owner_email)
|
|
|
|
redirect_to profil_path
|
|
|
|
end
|
|
|
|
|
2019-07-08 10:40:50 +02:00
|
|
|
private
|
|
|
|
|
2021-10-25 16:57:21 +02:00
|
|
|
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
|
|
|
|
|
2019-07-08 10:40:50 +02:00
|
|
|
def update_email_params
|
|
|
|
params.require(:user).permit(:email)
|
|
|
|
end
|
2019-07-09 17:08:27 +02:00
|
|
|
|
|
|
|
def requested_email
|
|
|
|
update_email_params[:email]
|
|
|
|
end
|
2019-12-09 17:11:12 +01:00
|
|
|
|
2021-10-18 12:03:13 +02:00
|
|
|
def target_email_allowed?
|
|
|
|
LEGIT_ADMIN_DOMAINS.any? { |d| requested_email.end_with?(d) }
|
2019-12-09 17:11:12 +01:00
|
|
|
end
|
2021-09-20 13:14:03 +02:00
|
|
|
|
|
|
|
def next_owner_email
|
|
|
|
params[:next_owner]
|
|
|
|
end
|
2018-08-23 18:53:35 +02:00
|
|
|
end
|
|
|
|
end
|