readapt name and logic
This commit is contained in:
parent
94be599401
commit
eaef5c7e39
14 changed files with 210 additions and 512 deletions
|
@ -2,8 +2,8 @@
|
|||
|
||||
class FranceConnect::ParticulierController < ApplicationController
|
||||
before_action :redirect_to_login_if_fc_aborted, only: [:callback]
|
||||
before_action :securely_retrieve_fci, only: [:merge, :merge_with_existing_account, :merge_with_new_account, :resend_and_renew_merge_confirmation, :associate_user]
|
||||
before_action :securely_retrieve_fci_from_email_merge_token, only: [:mail_merge_with_existing_account]
|
||||
before_action :securely_retrieve_fci, only: [:merge_using_fc_email, :merge_using_password, :send_email_merge_request]
|
||||
before_action :securely_retrieve_fci_from_email_merge_token, only: [:merge_using_email_link]
|
||||
before_action :set_user_by_confirmation_token, only: [:confirm_email]
|
||||
|
||||
def login
|
||||
|
@ -15,130 +15,98 @@ class FranceConnect::ParticulierController < ApplicationController
|
|||
end
|
||||
|
||||
def callback
|
||||
fci = FranceConnectService.find_or_retrieve_france_connect_information(params[:code])
|
||||
@fci = FranceConnectService.find_or_retrieve_france_connect_information(params[:code])
|
||||
|
||||
if fci.user.nil?
|
||||
preexisting_unlinked_user = User.find_by(email: sanitize(fci.email_france_connect))
|
||||
if @fci.user.nil?
|
||||
preexisting_unlinked_user = User.find_by(email: sanitize(@fci.email_france_connect))
|
||||
|
||||
if preexisting_unlinked_user.nil?
|
||||
merge_token = fci.create_merge_token!
|
||||
render :choose_email, locals: { france_connect_email: fci.email_france_connect, merge_token: }
|
||||
|
||||
elsif !preexisting_unlinked_user.can_france_connect?
|
||||
fci.destroy
|
||||
redirect_to new_user_session_path, alert: t('errors.messages.france_connect.forbidden_html', reset_link: new_user_password_path)
|
||||
@fci.create_merge_token!
|
||||
render :choose_email
|
||||
elsif preexisting_unlinked_user.can_france_connect?
|
||||
@fci.create_merge_token!
|
||||
render :merge
|
||||
else
|
||||
merge_token = fci.create_merge_token!
|
||||
redirect_to france_connect_particulier_merge_path(merge_token)
|
||||
destroy_fci_and_redirect_to_login(@fci)
|
||||
end
|
||||
else
|
||||
user = fci.user
|
||||
|
||||
if user.can_france_connect?
|
||||
fci.update(updated_at: Time.zone.now)
|
||||
connect_france_connect_particulier(user)
|
||||
if @fci.user.can_france_connect?
|
||||
@fci.update(updated_at: Time.zone.now)
|
||||
connect_france_connect_particulier(@fci.user)
|
||||
else
|
||||
fci.destroy
|
||||
redirect_to new_user_session_path, alert: t('errors.messages.france_connect.forbidden_html', reset_link: new_user_password_path)
|
||||
destroy_fci_and_redirect_to_login(@fci)
|
||||
end
|
||||
end
|
||||
|
||||
rescue Rack::OAuth2::Client::Error => e
|
||||
Rails.logger.error e.message
|
||||
redirect_france_connect_error_connection
|
||||
redirect_to(new_user_session_path, alert: t('errors.messages.france_connect.connexion'))
|
||||
end
|
||||
|
||||
def associate_user
|
||||
email = use_fc_email? ? @fci.email_france_connect : params[:alternative_email]
|
||||
def send_email_merge_request
|
||||
@fci.update(requested_email: sanitized_email_params)
|
||||
|
||||
@fci.associate_user!(email)
|
||||
user = @fci.user
|
||||
|
||||
@fci.send_custom_confirmation_instructions(user)
|
||||
@fci.delete_merge_token!
|
||||
sign_only(user)
|
||||
|
||||
render :confirmation_sent, locals: { email:, destination_path: destination_path(user) }
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
if e.record.errors.where(:email, :taken)
|
||||
user = User.find_by(email: e.record.email)
|
||||
@fci.send_custom_confirmation_instructions(user)
|
||||
@fci.delete_merge_token!
|
||||
render :confirmation_sent, locals: { email: user.email, destination_path: destination_path(user) }
|
||||
else
|
||||
redirect_to new_user_session_path, alert: t('errors.messages.france_connect.unknown_error')
|
||||
end
|
||||
end
|
||||
|
||||
def merge
|
||||
end
|
||||
|
||||
def merge_with_existing_account
|
||||
user = User.find_by(email: sanitized_email_params)
|
||||
|
||||
if user.present? && user.valid_for_authentication? { user.valid_password?(password_params) }
|
||||
if !user.can_france_connect?
|
||||
flash.alert = t('errors.messages.france_connect.forbidden_html', reset_link: new_user_password_path)
|
||||
|
||||
redirect_to root_path
|
||||
else
|
||||
@fci.update(user: user)
|
||||
@fci.delete_merge_token!
|
||||
@fci.delete_email_merge_token!
|
||||
|
||||
flash.notice = t('france_connect.particulier.flash.connection_done', application_name: Current.application_name)
|
||||
connect_france_connect_particulier(user)
|
||||
end
|
||||
else
|
||||
flash.alert = t('france_connect.particulier.flash.invalid_password')
|
||||
end
|
||||
end
|
||||
|
||||
def mail_merge_with_existing_account
|
||||
user = User.find_by(email: sanitize(@fci.email_france_connect.downcase))
|
||||
if user.can_france_connect?
|
||||
@fci.update(user: user)
|
||||
@fci.delete_merge_token!
|
||||
user.update(email_verified_at: Time.zone.now)
|
||||
flash.notice = t('france_connect.particulier.flash.connection_done', application_name: Current.application_name)
|
||||
connect_france_connect_particulier(user)
|
||||
else # same behaviour as redirect nicely with message when instructeur/administrateur
|
||||
@fci.destroy
|
||||
redirect_to new_user_session_path, alert: t('errors.messages.france_connect.forbidden_html', reset_link: new_user_password_path)
|
||||
end
|
||||
end
|
||||
|
||||
def merge_with_new_account
|
||||
user = User.find_by(email: sanitized_email_params)
|
||||
|
||||
if user.nil?
|
||||
@fci.associate_user!(sanitized_email_params)
|
||||
@fci.delete_merge_token!
|
||||
@fci.send_custom_confirmation_instructions(@fci.user)
|
||||
flash.notice = t('france_connect.particulier.flash.connection_done_verify_email', application_name: Current.application_name)
|
||||
connect_france_connect_particulier(@fci.user)
|
||||
else
|
||||
@email = sanitized_email_params
|
||||
@merge_token = merge_token_params
|
||||
end
|
||||
end
|
||||
|
||||
def resend_and_renew_merge_confirmation
|
||||
@fci.create_email_merge_token!
|
||||
UserMailer.france_connect_merge_confirmation(
|
||||
@fci.email_france_connect,
|
||||
sanitized_email_params,
|
||||
@fci.email_merge_token,
|
||||
@fci.email_merge_token_created_at
|
||||
)
|
||||
.deliver_later
|
||||
|
||||
merge_token = @fci.create_merge_token!
|
||||
redirect_to france_connect_particulier_merge_path(merge_token),
|
||||
notice: t('france_connect.particulier.flash.confirmation_mail_sent')
|
||||
redirect_to root_path, notice: t('france_connect.particulier.flash.confirmation_mail_sent')
|
||||
end
|
||||
|
||||
def merge_using_fc_email
|
||||
@fci.safely_associate_user!(@fci.email_france_connect)
|
||||
|
||||
sign_in(@fci.user)
|
||||
|
||||
@fci.send_custom_confirmation_instructions
|
||||
|
||||
render :confirmation_sent, locals: { email: @fci.email_france_connect, destination_path: destination_path(@fci.user) }
|
||||
end
|
||||
|
||||
def merge_using_password
|
||||
user = User.find_by(email: sanitize(@fci.email_france_connect))
|
||||
|
||||
if user.present? && !user.can_france_connect?
|
||||
return destroy_fci_and_redirect_to_login(@fci)
|
||||
end
|
||||
|
||||
if user.present? && user.valid_for_authentication? { user.valid_password?(params[:password]) }
|
||||
@fci.safely_update_user(user:)
|
||||
|
||||
flash.notice = t('france_connect.particulier.flash.connection_done', application_name: Current.application_name)
|
||||
connect_france_connect_particulier(user)
|
||||
else
|
||||
flash.alert = t('france_connect.particulier.flash.invalid_password')
|
||||
end
|
||||
end
|
||||
|
||||
def merge_using_email_link
|
||||
user = User.find_by(email: @fci.requested_email)
|
||||
|
||||
if user.present? && !user.can_france_connect?
|
||||
return destroy_fci_and_redirect_to_login(@fci)
|
||||
end
|
||||
|
||||
if user.nil?
|
||||
@fci.safely_associate_user!(@fci.requested_email)
|
||||
else
|
||||
@fci.safely_update_user(user:)
|
||||
end
|
||||
|
||||
@fci.user.update(email_verified_at: Time.zone.now)
|
||||
|
||||
flash.notice = t('france_connect.particulier.flash.connection_done', application_name: Current.application_name)
|
||||
connect_france_connect_particulier(@fci.user)
|
||||
end
|
||||
|
||||
# TODO mutualiser avec le controller Users::ActivateController
|
||||
# pour toute la partie de confirmation de compte
|
||||
def confirm_email
|
||||
if @user.confirmation_sent_at && @user.confirmation_sent_at > 2.days.ago
|
||||
if @user.confirmation_sent_at && 2.days.ago < @user.confirmation_sent_at
|
||||
@user.update(email_verified_at: Time.zone.now, confirmation_token: nil)
|
||||
@user.after_confirmation
|
||||
redirect_to destination_path(@user), notice: I18n.t('france_connect.particulier.flash.email_confirmed')
|
||||
|
@ -148,7 +116,7 @@ class FranceConnect::ParticulierController < ApplicationController
|
|||
fci = FranceConnectInformation.find_by(user: @user)
|
||||
|
||||
if fci
|
||||
fci.send_custom_confirmation_instructions(@user)
|
||||
fci.send_custom_confirmation_instructions
|
||||
redirect_to root_path, notice: I18n.t('france_connect.particulier.flash.confirmation_mail_resent')
|
||||
else
|
||||
redirect_to root_path, alert: I18n.t('france_connect.particulier.flash.confirmation_mail_resent_error')
|
||||
|
@ -161,26 +129,19 @@ class FranceConnect::ParticulierController < ApplicationController
|
|||
@user = User.find_by(confirmation_token: params[:token])
|
||||
|
||||
if @user.nil?
|
||||
redirect_to root_path, alert: I18n.t('france_connect.particulier.flash.user_not_found') and return
|
||||
return redirect_to root_path, alert: I18n.t('france_connect.particulier.flash.user_not_found')
|
||||
end
|
||||
|
||||
if user_signed_in? && current_user != @user
|
||||
sign_out current_user
|
||||
sign_out :user
|
||||
redirect_to new_user_session_path, alert: I18n.t('france_connect.particulier.flash.redirect_new_user_session')
|
||||
end
|
||||
end
|
||||
|
||||
def use_fc_email? = cast_bool(params[:use_france_connect_email])
|
||||
|
||||
def sign_only(user)
|
||||
sign_out(user) if user_signed_in?
|
||||
sign_in(user)
|
||||
end
|
||||
|
||||
def destination_path(user) = stored_location_for(user) || root_path(user)
|
||||
|
||||
def securely_retrieve_fci_from_email_merge_token
|
||||
@fci = FranceConnectInformation.find_by(email_merge_token: email_merge_token_params)
|
||||
@fci = FranceConnectInformation.find_by(email_merge_token: params[:email_merge_token])
|
||||
|
||||
if @fci.nil? || !@fci.valid_for_email_merge?
|
||||
flash.alert = I18n.t('france_connect.particulier.flash.merger_token_expired', application_name: Current.application_name)
|
||||
|
@ -192,7 +153,7 @@ class FranceConnect::ParticulierController < ApplicationController
|
|||
end
|
||||
|
||||
def securely_retrieve_fci
|
||||
@fci = FranceConnectInformation.find_by(merge_token: merge_token_params)
|
||||
@fci = FranceConnectInformation.find_by(merge_token: params[:merge_token])
|
||||
|
||||
if @fci.nil? || !@fci.valid_for_merge?
|
||||
flash.alert = I18n.t('france_connect.particulier.flash.merger_token_expired', application_name: Current.application_name)
|
||||
|
@ -207,11 +168,13 @@ class FranceConnect::ParticulierController < ApplicationController
|
|||
end
|
||||
end
|
||||
|
||||
def connect_france_connect_particulier(user)
|
||||
if user_signed_in?
|
||||
sign_out :user
|
||||
end
|
||||
def destroy_fci_and_redirect_to_login(fci)
|
||||
fci.destroy
|
||||
redirect_to new_user_session_path, alert: t('errors.messages.france_connect.forbidden_html', reset_link: new_user_password_path)
|
||||
end
|
||||
|
||||
def connect_france_connect_particulier(user)
|
||||
sign_out :user if user_signed_in?
|
||||
sign_in user
|
||||
|
||||
user.update_attribute('loged_in_with_france_connect', User.loged_in_with_france_connects.fetch(:particulier))
|
||||
|
@ -219,23 +182,6 @@ class FranceConnect::ParticulierController < ApplicationController
|
|||
redirect_to destination_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
|
||||
|
||||
def email_merge_token_params
|
||||
params[:email_merge_token]
|
||||
end
|
||||
|
||||
def password_params
|
||||
params[:password]
|
||||
end
|
||||
|
||||
def sanitized_email_params
|
||||
sanitize(params[:email])
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue