2015-12-24 10:12:23 +01:00
|
|
|
class FranceConnect::ParticulierController < ApplicationController
|
|
|
|
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-11 16:00:14 +01:00
|
|
|
if params[:code].nil?
|
|
|
|
return redirect_to new_user_session_path
|
|
|
|
end
|
2016-01-05 12:18:00 +01:00
|
|
|
|
2018-01-11 18:16:38 +01:00
|
|
|
fetched_fc_information = FranceConnectService.retrieve_user_informations_particulier(params[:code])
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2018-01-15 13:19:01 +01:00
|
|
|
france_connect_information = FranceConnectInformation
|
2018-01-15 21:06:47 +01:00
|
|
|
.find_by(france_connect_particulier_id: fetched_fc_information[:france_connect_particulier_id]) ||
|
|
|
|
fetched_fc_information.tap { |object| object.save }
|
2016-01-21 17:06:09 +01:00
|
|
|
|
2018-01-15 13:19:01 +01:00
|
|
|
user = france_connect_information.user
|
|
|
|
salt = FranceConnectSaltService.new(france_connect_information).salt
|
2016-01-21 17:06:09 +01:00
|
|
|
|
2018-01-15 13:19:01 +01:00
|
|
|
if user.nil?
|
2018-01-15 13:20:47 +01:00
|
|
|
redirect_to france_connect_particulier_new_path(fci_id: france_connect_information.id, salt: salt)
|
|
|
|
else
|
|
|
|
connect_france_connect_particulier(user)
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
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
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
def new
|
2018-01-11 19:04:39 +01:00
|
|
|
return redirect_france_connect_error_connection if !valid_salt_and_fci_id_params?
|
2016-01-21 17:06:09 +01:00
|
|
|
|
|
|
|
france_connect_information = FranceConnectInformation.find(params[:fci_id])
|
|
|
|
@user = User.new(france_connect_information: france_connect_information).decorate
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
redirect_france_connect_error_connection
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
|
2016-01-05 12:18:00 +01:00
|
|
|
def check_email
|
2018-01-11 19:04:39 +01:00
|
|
|
return redirect_france_connect_error_connection if !valid_salt_and_fci_id_params?
|
2016-01-21 17:06:09 +01:00
|
|
|
|
|
|
|
user = User.find_by_email(params[:user][:email_france_connect])
|
2016-01-19 17:19:38 +01:00
|
|
|
|
|
|
|
return create if user.nil?
|
2016-01-05 12:18:00 +01:00
|
|
|
|
2018-01-11 19:04:39 +01:00
|
|
|
if params[:user][:password].present?
|
2016-01-05 12:18:00 +01:00
|
|
|
|
2016-01-19 17:19:38 +01:00
|
|
|
if user.valid_password?(params[:user][:password])
|
2016-01-21 17:06:09 +01:00
|
|
|
user.france_connect_information = FranceConnectInformation.find(params[:fci_id])
|
|
|
|
|
2016-01-05 12:18:00 +01:00
|
|
|
return connect_france_connect_particulier user
|
|
|
|
else
|
|
|
|
flash.now.alert = 'Mot de passe invalide'
|
|
|
|
end
|
|
|
|
end
|
2016-01-19 17:19:38 +01:00
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
france_connect_information = FranceConnectInformation.find(params[:fci_id])
|
|
|
|
france_connect_information.update_attribute(:email_france_connect, params[:user][:email_france_connect])
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
@user = User.new(france_connect_information: france_connect_information).decorate
|
|
|
|
end
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2018-01-15 14:37:28 +01:00
|
|
|
private
|
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
def create
|
|
|
|
user = User.new email: params[:user][:email_france_connect]
|
|
|
|
user.password = Devise.friendly_token[0, 20]
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2018-01-11 19:04:39 +01:00
|
|
|
if !user.valid?
|
2016-01-21 17:06:09 +01:00
|
|
|
flash.alert = 'Email non valide'
|
2016-11-15 04:42:32 +01:00
|
|
|
|
|
|
|
return redirect_to france_connect_particulier_new_path fci_id: params[:fci_id], salt: params[:salt], user: {email_france_connect: params[:user]['email_france_connect']}
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
user.save
|
|
|
|
FranceConnectInformation.find(params[:fci_id]).update_attribute(:user, user)
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
connect_france_connect_particulier user
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def connect_france_connect_particulier user
|
2016-01-26 16:48:33 +01:00
|
|
|
sign_out :user if user_signed_in?
|
|
|
|
sign_out :gestionnaire if gestionnaire_signed_in?
|
|
|
|
sign_out :administrateur if administrateur_signed_in?
|
|
|
|
|
2015-12-24 10:12:23 +01:00
|
|
|
sign_in user
|
|
|
|
|
|
|
|
user.loged_in_with_france_connect = 'particulier'
|
|
|
|
user.save
|
|
|
|
|
|
|
|
redirect_to stored_location_for(current_user) || signed_in_root_path(current_user)
|
|
|
|
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
|
|
|
|
|
|
|
|
def valid_salt_and_fci_id_params?
|
|
|
|
france_connect_information = FranceConnectInformation.find(params[:fci_id])
|
|
|
|
FranceConnectSaltService.new(france_connect_information).valid? params[:salt]
|
|
|
|
end
|
2017-04-04 15:27:04 +02:00
|
|
|
end
|