Sync user/gestionnaire credentials (OpenSimplif)

Syncs email and password when either a gestionnaire or a user is
updated and it has an associated user or gestionnaire (same email).
This commit is contained in:
Julien Portalier 2016-10-11 10:31:32 +02:00 committed by Xavier J
parent 49e4d6a9d3
commit 687f87e457
4 changed files with 51 additions and 0 deletions

View file

@ -14,6 +14,7 @@ class Gestionnaire < ActiveRecord::Base
after_create :build_default_preferences_list_dossier after_create :build_default_preferences_list_dossier
after_create :build_default_preferences_smart_listing_page after_create :build_default_preferences_smart_listing_page
after_save :sync_credentials
def dossiers_follow def dossiers_follow
dossiers.joins(:follows).where("follows.gestionnaire_id = #{id}") dossiers.joins(:follows).where("follows.gestionnaire_id = #{id}")
@ -84,4 +85,16 @@ class Gestionnaire < ActiveRecord::Base
couples.include?({table: table, column: column}) couples.include?({table: table, column: column})
end 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 end

View file

@ -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 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 accepts_nested_attributes_for :france_connect_information
after_update :sync_credentials
def self.find_for_france_connect email, siret def self.find_for_france_connect email, siret
user = User.find_by_email(email) user = User.find_by_email(email)
if user.nil? if user.nil?
@ -33,4 +35,18 @@ class User < ActiveRecord::Base
def invite? dossier_id def invite? dossier_id
invites.pluck(:dossier_id).include?(dossier_id.to_i) invites.pluck(:dossier_id).include?(dossier_id.to_i)
end 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 end

View file

@ -184,4 +184,15 @@ describe Gestionnaire, type: :model do
end end
end 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 end

View file

@ -72,4 +72,15 @@ describe User, type: :model do
it { is_expected.to be_falsey } it { is_expected.to be_falsey }
end end
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 end