diff --git a/app/controllers/users/sessions_controller.rb b/app/controllers/users/sessions_controller.rb index 979c8887d..31a486234 100644 --- a/app/controllers/users/sessions_controller.rb +++ b/app/controllers/users/sessions_controller.rb @@ -101,6 +101,7 @@ class Users::SessionsController < Sessions::SessionsController if resource = klass.find_for_database_authentication(email: params[:user][:email]) if resource.valid_password?(params[:user][:password]) sign_in resource + resource.force_sync_credentials set_flash_message :notice, :signed_in end end diff --git a/app/models/administrateur.rb b/app/models/administrateur.rb index b2882fb62..ab69d0339 100644 --- a/app/models/administrateur.rb +++ b/app/models/administrateur.rb @@ -6,7 +6,8 @@ class Administrateur < ActiveRecord::Base has_many :procedures before_save :ensure_api_token - after_update :sync_credentials + + include CredentialsSyncableConcern def ensure_api_token if api_token.nil? @@ -27,10 +28,4 @@ class Administrateur < ActiveRecord::Base end end - def sync_credentials - if email_changed? || encrypted_password_changed? - return SyncCredentialsService.new(Administrateur, email_was, email, encrypted_password).change_credentials! - end - true - end end diff --git a/app/models/concerns/credentials_syncable_concern.rb b/app/models/concerns/credentials_syncable_concern.rb new file mode 100644 index 000000000..f1ee62df8 --- /dev/null +++ b/app/models/concerns/credentials_syncable_concern.rb @@ -0,0 +1,19 @@ +module CredentialsSyncableConcern + extend ActiveSupport::Concern + + included do + after_update :sync_credentials + end + + def sync_credentials + if email_changed? || encrypted_password_changed? + return force_sync_credentials + end + true + end + + def force_sync_credentials + SyncCredentialsService.new(self.class, email_was, email, encrypted_password).change_credentials! + end + +end diff --git a/app/models/gestionnaire.rb b/app/models/gestionnaire.rb index 6836c06c3..ada358189 100644 --- a/app/models/gestionnaire.rb +++ b/app/models/gestionnaire.rb @@ -14,7 +14,8 @@ class Gestionnaire < ActiveRecord::Base after_create :build_default_preferences_list_dossier after_create :build_default_preferences_smart_listing_page - after_update :sync_credentials + + include CredentialsSyncableConcern def dossiers_follow @dossiers_follow ||= dossiers.joins(:follows).where("follows.gestionnaire_id = #{id}") @@ -119,11 +120,4 @@ class Gestionnaire < ActiveRecord::Base couples.include?({table: table, column: column}) end - - def sync_credentials - if email_changed? || encrypted_password_changed? - return SyncCredentialsService.new(Gestionnaire, email_was, email, encrypted_password).change_credentials! - end - true - end end diff --git a/app/models/user.rb b/app/models/user.rb index 34d245e48..7ea29d260 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -15,7 +15,8 @@ class User < ActiveRecord::Base delegate :given_name, :family_name, :email_france_connect, :gender, :birthdate, :birthplace, :france_connect_particulier_id, to: :france_connect_information accepts_nested_attributes_for :france_connect_information - after_update :sync_credentials + + include CredentialsSyncableConcern def self.find_for_france_connect email, siret user = User.find_by_email(email) @@ -35,12 +36,4 @@ class User < ActiveRecord::Base invites.pluck(:dossier_id).include?(dossier_id.to_i) end - private - - def sync_credentials - if email_changed? || encrypted_password_changed? - return SyncCredentialsService.new(User, email_was, email, encrypted_password).change_credentials! - end - true - end end diff --git a/app/services/sync_credentials_service.rb b/app/services/sync_credentials_service.rb index 491ec1d55..06d912b29 100644 --- a/app/services/sync_credentials_service.rb +++ b/app/services/sync_credentials_service.rb @@ -11,7 +11,7 @@ class SyncCredentialsService unless @klass == User user = User.find_by(email: @email_was) if user - return user.update_columns( + return false unless user.update_columns( email: @email, encrypted_password: @encrypted_password) end @@ -20,7 +20,7 @@ class SyncCredentialsService unless @klass == Gestionnaire gestionnaire = Gestionnaire.find_by(email: @email_was) if gestionnaire - return gestionnaire.update_columns( + return false unless gestionnaire.update_columns( email: @email, encrypted_password: @encrypted_password) end @@ -29,10 +29,12 @@ class SyncCredentialsService unless @klass == Administrateur administrateur = Administrateur.find_by(email: @email_was) if administrateur - return administrateur.update_columns( + return false unless administrateur.update_columns( email: @email, encrypted_password: @encrypted_password) end end + + true end end diff --git a/spec/controllers/users/sessions_controller_spec.rb b/spec/controllers/users/sessions_controller_spec.rb index ebfb037b7..f3e0072b8 100644 --- a/spec/controllers/users/sessions_controller_spec.rb +++ b/spec/controllers/users/sessions_controller_spec.rb @@ -100,6 +100,24 @@ describe Users::SessionsController, type: :controller do expect(subject.current_gestionnaire).to be(nil) expect(subject.current_administrateur).to be(nil) end + + context 'with different passwords' do + let!(:gestionnaire) { create(:gestionnaire, email: email, password: 'another_password') } + let!(:administrateur) { create(:administrateur, email: email, password: 'another_password') } + + before do + user + end + + it 'should sync passwords on login' do + post :create, params: { user: { email: email, password: password } } + gestionnaire.reload + administrateur.reload + expect(user.valid_password?(password)).to be(true) + expect(gestionnaire.valid_password?(password)).to be(true) + expect(administrateur.valid_password?(password)).to be(true) + end + end end end