2015-12-24 10:12:23 +01:00
|
|
|
class FranceConnect::ParticulierController < ApplicationController
|
2018-01-15 21:18:02 +01:00
|
|
|
before_action :redirect_to_login_if_fc_aborted, only: [:callback]
|
|
|
|
|
2015-12-24 10:12:23 +01:00
|
|
|
def login
|
2018-01-11 15:29:58 +01:00
|
|
|
redirect_to FranceConnectService.authorization_uri
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
def callback
|
2018-01-15 21:09:52 +01:00
|
|
|
fetched_fci = FranceConnectService.retrieve_user_informations_particulier(params[:code])
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2018-01-15 21:09:52 +01:00
|
|
|
fci = FranceConnectInformation
|
|
|
|
.find_by(france_connect_particulier_id: fetched_fci[:france_connect_particulier_id]) ||
|
2018-10-01 14:20:14 +02:00
|
|
|
fetched_fci.tap(&:save)
|
2016-01-21 17:06:09 +01:00
|
|
|
|
2018-01-15 21:11:26 +01:00
|
|
|
if fci.user.nil?
|
2018-01-16 12:08:50 +01:00
|
|
|
user = User.find_or_create_by(email: fci.email_france_connect) do |new_user|
|
|
|
|
new_user.password = Devise.friendly_token[0, 20]
|
2018-10-25 15:07:15 +02:00
|
|
|
new_user.confirmed_at = Time.zone.now
|
2018-01-16 12:08:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
fci.update_attribute('user_id', user.id)
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
2018-01-16 12:08:50 +01:00
|
|
|
|
|
|
|
connect_france_connect_particulier(fci.user)
|
2016-01-21 17:06:09 +01:00
|
|
|
rescue Rack::OAuth2::Client::Error => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
redirect_france_connect_error_connection
|
|
|
|
end
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2018-01-15 14:37:28 +01:00
|
|
|
private
|
|
|
|
|
2018-01-15 21:18:02 +01:00
|
|
|
def redirect_to_login_if_fc_aborted
|
2019-05-09 13:54:50 +02:00
|
|
|
if params[:code].blank?
|
2018-01-15 21:18:02 +01:00
|
|
|
redirect_to new_user_session_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-20 17:47:37 +01:00
|
|
|
def connect_france_connect_particulier(user)
|
2018-10-01 13:24:37 +02:00
|
|
|
if user_signed_in?
|
|
|
|
sign_out :user
|
|
|
|
end
|
|
|
|
|
|
|
|
if gestionnaire_signed_in?
|
|
|
|
sign_out :gestionnaire
|
|
|
|
end
|
|
|
|
|
|
|
|
if administrateur_signed_in?
|
|
|
|
sign_out :administrateur
|
|
|
|
end
|
2016-01-26 16:48:33 +01:00
|
|
|
|
2015-12-24 10:12:23 +01:00
|
|
|
sign_in user
|
|
|
|
|
2018-08-28 11:41:37 +02:00
|
|
|
user.update_attribute('loged_in_with_france_connect', User.loged_in_with_france_connects.fetch(:particulier))
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2018-01-16 15:41:51 +01:00
|
|
|
redirect_to stored_location_for(current_user) || root_path(current_user)
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
2016-01-21 17:06:09 +01:00
|
|
|
|
|
|
|
def redirect_france_connect_error_connection
|
|
|
|
flash.alert = t('errors.messages.france_connect.connexion')
|
|
|
|
redirect_to(new_user_session_path)
|
|
|
|
end
|
2017-04-04 15:27:04 +02:00
|
|
|
end
|