Allow instructeur to have multiple agent_connect_information

AC will return two different sub depending of the domain demarches.gouv.fr or ds.
Both agent_connect_information are stored and the corresponding instructeur is found by its email.
We do not store anymore the agent_connect_id on the instructeur as the are many.
This commit is contained in:
simon lehericey 2024-03-19 14:57:19 +01:00
parent 2f6147308c
commit ca17524559
3 changed files with 36 additions and 11 deletions

View file

@ -22,20 +22,16 @@ class AgentConnect::AgentController < ApplicationController
user_info, id_token = AgentConnectService.user_info(params[:code], cookies.encrypted[NONCE_COOKIE_NAME])
cookies.delete NONCE_COOKIE_NAME
instructeur = Instructeur.find_by(agent_connect_id: user_info['sub'])
if instructeur.nil?
instructeur = Instructeur.find_by(users: { email: santized_email(user_info) })
end
instructeur = Instructeur.find_by(users: { email: santized_email(user_info) })
if instructeur.nil?
user = User.create_or_promote_to_instructeur(santized_email(user_info), Devise.friendly_token[0, 20])
instructeur = user.instructeur
end
instructeur.update(agent_connect_id: user_info['sub'], agent_connect_id_token: id_token)
instructeur.update(agent_connect_id_token: id_token)
aci = AgentConnectInformation.find_or_initialize_by(instructeur:)
aci = AgentConnectInformation.find_or_initialize_by(instructeur:, sub: user_info['sub'])
aci.update(user_info.slice('given_name', 'usual_name', 'email', 'sub', 'siret', 'organizational_unit', 'belonging_population', 'phone'))
sign_in(:user, instructeur.user)