diff --git a/app/models/gestionnaire.rb b/app/models/gestionnaire.rb index 8696d16f1..c053d3f16 100644 --- a/app/models/gestionnaire.rb +++ b/app/models/gestionnaire.rb @@ -14,6 +14,7 @@ class Gestionnaire < ActiveRecord::Base after_create :build_default_preferences_list_dossier after_create :build_default_preferences_smart_listing_page + after_save :sync_credentials def dossiers_follow dossiers.joins(:follows).where("follows.gestionnaire_id = #{id}") @@ -84,4 +85,16 @@ class Gestionnaire < ActiveRecord::Base couples.include?({table: table, column: column}) end + + def sync_credentials + if email_changed? || encrypted_password_changed? + user = User.find_by(email: email_was) + if user + return user.update_columns( + email: email, + encrypted_password: encrypted_password) + end + end + true + end end diff --git a/app/models/user.rb b/app/models/user.rb index 93237e89c..31bc7779e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -16,6 +16,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 + def self.find_for_france_connect email, siret user = User.find_by_email(email) if user.nil? @@ -33,4 +35,18 @@ class User < ActiveRecord::Base def invite? dossier_id invites.pluck(:dossier_id).include?(dossier_id.to_i) end + + private + + def sync_credentials + if email_changed? || encrypted_password_changed? + gestionnaire = Gestionnaire.find_by(email: email_was) + if gestionnaire + return gestionnaire.update_columns( + email: email, + encrypted_password: encrypted_password) + end + end + true + end end diff --git a/spec/models/gestionnaire_spec.rb b/spec/models/gestionnaire_spec.rb index 8ab20339f..21cacaf59 100644 --- a/spec/models/gestionnaire_spec.rb +++ b/spec/models/gestionnaire_spec.rb @@ -184,4 +184,15 @@ describe Gestionnaire, type: :model do end end end + + it 'syncs credentials to associated user' do + gestionnaire = create(:gestionnaire) + user = create(:user, email: gestionnaire.email) + + gestionnaire.update_attributes(email: 'whoami@plop.com', password: 'super secret') + + user.reload + expect(user.email).to eq('whoami@plop.com') + expect(user.valid_password?('super secret')).to be(true) + end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 555fdfcb1..41243be83 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -72,4 +72,15 @@ describe User, type: :model do it { is_expected.to be_falsey } end end + + it 'syncs credentials to associated gestionnaire' do + user = create(:user) + gestionnaire = create(:gestionnaire, email: user.email) + + user.update_attributes(email: 'whoami@plop.com', password: 'super secret') + + gestionnaire.reload + expect(gestionnaire.email).to eq('whoami@plop.com') + expect(gestionnaire.valid_password?('super secret')).to be(true) + end end