82 lines
2.2 KiB
Ruby
82 lines
2.2 KiB
Ruby
class FranceConnect::ParticulierController < ApplicationController
|
|
before_action :redirect_to_login_if_fc_aborted, only: [:callback]
|
|
before_action :securely_retrieve_fci, only: [:merge]
|
|
|
|
def login
|
|
if FranceConnectService.enabled?
|
|
redirect_to FranceConnectService.authorization_uri
|
|
else
|
|
redirect_to new_user_session_path
|
|
end
|
|
end
|
|
|
|
def callback
|
|
fci = FranceConnectService.find_or_retrieve_france_connect_information(params[:code])
|
|
|
|
if fci.user.nil?
|
|
preexisting_unlinked_user = User.find_by(email: fci.email_france_connect.downcase)
|
|
|
|
if preexisting_unlinked_user.nil?
|
|
fci.associate_user!(fci.email_france_connect)
|
|
connect_france_connect_particulier(fci.user)
|
|
else
|
|
redirect_to france_connect_particulier_merge_path(fci.create_merge_token!)
|
|
end
|
|
else
|
|
user = fci.user
|
|
|
|
if user.can_france_connect?
|
|
connect_france_connect_particulier(user)
|
|
else
|
|
fci.destroy
|
|
redirect_to new_user_session_path, alert: t('errors.messages.france_connect.forbidden_html', reset_link: new_user_password_path)
|
|
end
|
|
end
|
|
|
|
rescue Rack::OAuth2::Client::Error => e
|
|
Rails.logger.error e.message
|
|
redirect_france_connect_error_connection
|
|
end
|
|
|
|
def merge
|
|
end
|
|
|
|
private
|
|
|
|
def securely_retrieve_fci
|
|
@fci = FranceConnectInformation.find_by(merge_token: merge_token_params)
|
|
|
|
if @fci.nil? || !@fci.valid_for_merge?
|
|
flash.alert = 'Votre compte FranceConnect a expiré, veuillez recommencer.'
|
|
|
|
redirect_to root_path
|
|
end
|
|
end
|
|
|
|
def redirect_to_login_if_fc_aborted
|
|
if params[:code].blank?
|
|
redirect_to new_user_session_path
|
|
end
|
|
end
|
|
|
|
def connect_france_connect_particulier(user)
|
|
if user_signed_in?
|
|
sign_out :user
|
|
end
|
|
|
|
sign_in user
|
|
|
|
user.update_attribute('loged_in_with_france_connect', User.loged_in_with_france_connects.fetch(:particulier))
|
|
|
|
redirect_to stored_location_for(current_user) || root_path(current_user)
|
|
end
|
|
|
|
def redirect_france_connect_error_connection
|
|
flash.alert = t('errors.messages.france_connect.connexion')
|
|
redirect_to(new_user_session_path)
|
|
end
|
|
|
|
def merge_token_params
|
|
params[:merge_token]
|
|
end
|
|
end
|