Merge branch 'sync-passwords-on-login' into develop

This commit is contained in:
Xavier J 2017-02-13 13:47:36 +01:00
commit c344c99fbb
7 changed files with 49 additions and 27 deletions

View file

@ -101,6 +101,7 @@ class Users::SessionsController < Sessions::SessionsController
if resource = klass.find_for_database_authentication(email: params[:user][:email]) if resource = klass.find_for_database_authentication(email: params[:user][:email])
if resource.valid_password?(params[:user][:password]) if resource.valid_password?(params[:user][:password])
sign_in resource sign_in resource
resource.force_sync_credentials
set_flash_message :notice, :signed_in set_flash_message :notice, :signed_in
end end
end end

View file

@ -6,7 +6,8 @@ class Administrateur < ActiveRecord::Base
has_many :procedures has_many :procedures
before_save :ensure_api_token before_save :ensure_api_token
after_update :sync_credentials
include CredentialsSyncableConcern
def ensure_api_token def ensure_api_token
if api_token.nil? if api_token.nil?
@ -27,10 +28,4 @@ class Administrateur < ActiveRecord::Base
end end
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 end

View file

@ -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

View file

@ -14,7 +14,8 @@ 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_update :sync_credentials
include CredentialsSyncableConcern
def dossiers_follow def dossiers_follow
@dossiers_follow ||= dossiers.joins(:follows).where("follows.gestionnaire_id = #{id}") @dossiers_follow ||= dossiers.joins(:follows).where("follows.gestionnaire_id = #{id}")
@ -119,11 +120,4 @@ 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?
return SyncCredentialsService.new(Gestionnaire, email_was, email, encrypted_password).change_credentials!
end
true
end
end end

View file

@ -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 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
include CredentialsSyncableConcern
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)
@ -35,12 +36,4 @@ class User < ActiveRecord::Base
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?
return SyncCredentialsService.new(User, email_was, email, encrypted_password).change_credentials!
end
true
end
end end

View file

@ -11,7 +11,7 @@ class SyncCredentialsService
unless @klass == User unless @klass == User
user = User.find_by(email: @email_was) user = User.find_by(email: @email_was)
if user if user
return user.update_columns( return false unless user.update_columns(
email: @email, email: @email,
encrypted_password: @encrypted_password) encrypted_password: @encrypted_password)
end end
@ -20,7 +20,7 @@ class SyncCredentialsService
unless @klass == Gestionnaire unless @klass == Gestionnaire
gestionnaire = Gestionnaire.find_by(email: @email_was) gestionnaire = Gestionnaire.find_by(email: @email_was)
if gestionnaire if gestionnaire
return gestionnaire.update_columns( return false unless gestionnaire.update_columns(
email: @email, email: @email,
encrypted_password: @encrypted_password) encrypted_password: @encrypted_password)
end end
@ -29,10 +29,12 @@ class SyncCredentialsService
unless @klass == Administrateur unless @klass == Administrateur
administrateur = Administrateur.find_by(email: @email_was) administrateur = Administrateur.find_by(email: @email_was)
if administrateur if administrateur
return administrateur.update_columns( return false unless administrateur.update_columns(
email: @email, email: @email,
encrypted_password: @encrypted_password) encrypted_password: @encrypted_password)
end end
end end
true
end end
end end

View file

@ -100,6 +100,24 @@ describe Users::SessionsController, type: :controller do
expect(subject.current_gestionnaire).to be(nil) expect(subject.current_gestionnaire).to be(nil)
expect(subject.current_administrateur).to be(nil) expect(subject.current_administrateur).to be(nil)
end 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
end end