From 301653bb89ce048eb9d7608c9648c4f61df883de Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 9 Dec 2019 16:09:01 +0100 Subject: [PATCH 1/4] Only display email form if the user is not instructeur --- app/views/users/profil/show.html.haml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/views/users/profil/show.html.haml b/app/views/users/profil/show.html.haml index 58187a6ed..837d963e8 100644 --- a/app/views/users/profil/show.html.haml +++ b/app/views/users/profil/show.html.haml @@ -18,9 +18,10 @@ %p Pour finaliser votre changement d’adresse, vérifiez vos emails et cliquez sur le lien de confirmation. - = form_for @current_user, url: update_email_path, method: :patch, html: { class: 'form' } do |f| - = f.email_field :email, value: nil, placeholder: 'Nouvelle adresse email', required: true - = f.submit "Changer mon adresse", class: 'button primary' + - if !instructeur_signed_in? + = form_for @current_user, url: update_email_path, method: :patch, html: { class: 'form' } do |f| + = f.email_field :email, value: nil, placeholder: 'Nouvelle adresse email', required: true + = f.submit "Changer mon adresse", class: 'button primary' - if current_administrateur.present? .card From e06733d01bb18155aee6994170f685a3b87da291 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 9 Dec 2019 16:44:31 +0100 Subject: [PATCH 2/4] Clean spec --- spec/controllers/users/profil_controller_spec.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/spec/controllers/users/profil_controller_spec.rb b/spec/controllers/users/profil_controller_spec.rb index aad9bc371..916935d3c 100644 --- a/spec/controllers/users/profil_controller_spec.rb +++ b/spec/controllers/users/profil_controller_spec.rb @@ -52,8 +52,6 @@ describe Users::ProfilController, type: :controller do end context 'when the mail is incorrect' do - let!(:user2) { create(:user) } - before do patch :update_email, params: { user: { email: 'incorrect' } } user.reload From 21910c959e707e1f285cf8843eb533ca8adfba05 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 9 Dec 2019 16:50:30 +0100 Subject: [PATCH 3/4] @current_user -> current_user --- app/controllers/users/profil_controller.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/users/profil_controller.rb b/app/controllers/users/profil_controller.rb index 13c9c7f20..41cc5b6cf 100644 --- a/app/controllers/users/profil_controller.rb +++ b/app/controllers/users/profil_controller.rb @@ -10,14 +10,14 @@ module Users end def update_email - if @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 + elsif current_user.errors&.details&.dig(:email)&.any? { |e| e[:error] == :taken } + UserMailer.account_already_taken(current_user, requested_email).deliver_later # avoid leaking information about whether an account with this email exists or not flash.notice = t('devise.registrations.update_needs_confirmation') else - flash.alert = @current_user.errors.full_messages + flash.alert = current_user.errors.full_messages end redirect_to profil_path From 058442c84e751be0a930c825aef1361fd2e15a45 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 9 Dec 2019 17:11:12 +0100 Subject: [PATCH 4/4] [link to #4557] An instructeur cannot change its email on its own --- app/controllers/users/profil_controller.rb | 8 ++++++++ spec/controllers/users/profil_controller_spec.rb | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/app/controllers/users/profil_controller.rb b/app/controllers/users/profil_controller.rb index 41cc5b6cf..eb9c35409 100644 --- a/app/controllers/users/profil_controller.rb +++ b/app/controllers/users/profil_controller.rb @@ -1,5 +1,9 @@ module Users class ProfilController < UserController + before_action :redirect_if_instructeur, + only: :update_email, + if: -> { instructeur_signed_in? } + def show end @@ -32,5 +36,9 @@ module Users def requested_email update_email_params[:email] end + + def redirect_if_instructeur + redirect_to profil_path + end end end diff --git a/spec/controllers/users/profil_controller_spec.rb b/spec/controllers/users/profil_controller_spec.rb index 916935d3c..82fe1706e 100644 --- a/spec/controllers/users/profil_controller_spec.rb +++ b/spec/controllers/users/profil_controller_spec.rb @@ -60,5 +60,18 @@ describe Users::ProfilController, type: :controller do it { expect(response).to redirect_to(profil_path) } it { expect(flash.alert).to eq(['Email invalide']) } end + + context 'when the user has an instructeur role' do + let(:instructeur_email) { 'instructeur_email@a.com' } + let!(:user) { create(:instructeur, email: instructeur_email).user } + + before do + patch :update_email, params: { user: { email: 'loulou@lou.com' } } + user.reload + end + + it { expect(user.unconfirmed_email).to be_nil } + it { expect(response).to redirect_to(profil_path) } + end end end