2015-12-09 15:10:11 +01:00
|
|
|
class Users::SessionsController < Sessions::SessionsController
|
2016-11-14 17:33:45 +01:00
|
|
|
# before_action :configure_sign_in_params, only: [:create]
|
2015-09-23 10:02:01 +02:00
|
|
|
|
2016-02-15 17:13:16 +01:00
|
|
|
def demo
|
2016-02-19 15:04:29 +01:00
|
|
|
return redirect_to root_path if Rails.env.production?
|
|
|
|
|
2017-01-03 10:40:09 +01:00
|
|
|
@user = User.new(email: DemoEmails[:user], password: 'password')
|
2016-02-15 17:13:16 +01:00
|
|
|
render 'new'
|
|
|
|
end
|
|
|
|
|
2015-12-24 10:12:23 +01:00
|
|
|
# GET /resource/sign_in
|
2016-02-15 17:13:16 +01:00
|
|
|
def new
|
2017-03-07 10:15:33 +01:00
|
|
|
unless user_return_to_procedure_id.nil? # WTF ?
|
2016-06-09 17:49:38 +02:00
|
|
|
@dossier = Dossier.new(procedure: Procedure.active(user_return_to_procedure_id))
|
2016-05-26 15:59:50 +02:00
|
|
|
end
|
|
|
|
|
2016-02-15 17:13:16 +01:00
|
|
|
@user = User.new
|
2016-05-26 15:59:50 +02:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
error_procedure
|
2016-02-15 17:13:16 +01:00
|
|
|
end
|
2015-09-23 10:02:01 +02:00
|
|
|
|
2015-12-24 10:12:23 +01:00
|
|
|
#POST /resource/sign_in
|
2015-10-07 14:19:16 +02:00
|
|
|
def create
|
2017-03-07 10:15:33 +01:00
|
|
|
remember_me = params[:user][:remember_me] == '1'
|
|
|
|
try_to_authenticate(User, remember_me)
|
|
|
|
try_to_authenticate(Gestionnaire, remember_me)
|
|
|
|
try_to_authenticate(Administrateur, remember_me)
|
2015-10-07 14:19:16 +02:00
|
|
|
|
2016-10-11 11:12:45 +02:00
|
|
|
if user_signed_in?
|
|
|
|
current_user.update_attributes(loged_in_with_france_connect: '')
|
|
|
|
end
|
|
|
|
|
2016-10-20 11:48:40 +02:00
|
|
|
if user_signed_in?
|
2016-10-11 11:12:45 +02:00
|
|
|
redirect_to after_sign_in_path_for(:user)
|
2016-10-20 11:48:40 +02:00
|
|
|
elsif gestionnaire_signed_in?
|
2017-06-12 10:06:41 +02:00
|
|
|
location = stored_location_for(:gestionnaire) || backoffice_path
|
|
|
|
redirect_to location
|
2016-12-07 17:03:36 +01:00
|
|
|
elsif administrateur_signed_in?
|
|
|
|
redirect_to admin_path
|
2016-10-11 11:12:45 +02:00
|
|
|
else
|
2017-01-02 09:38:49 +01:00
|
|
|
flash.alert = 'Mauvais couple login / mot de passe'
|
2016-10-11 11:12:45 +02:00
|
|
|
new
|
|
|
|
render :new, status: 401
|
|
|
|
end
|
2015-10-07 14:19:16 +02:00
|
|
|
end
|
2015-09-23 10:02:01 +02:00
|
|
|
|
2016-02-15 17:13:16 +01:00
|
|
|
# DELETE /resource/sign_out
|
2015-10-07 16:38:29 +02:00
|
|
|
def destroy
|
2016-12-07 17:03:36 +01:00
|
|
|
sign_out :gestionnaire if gestionnaire_signed_in?
|
|
|
|
sign_out :administrateur if administrateur_signed_in?
|
2015-10-07 16:38:29 +02:00
|
|
|
|
2016-10-11 11:12:45 +02:00
|
|
|
if user_signed_in?
|
|
|
|
connected_with_france_connect = current_user.loged_in_with_france_connect
|
|
|
|
current_user.update_attributes(loged_in_with_france_connect: '')
|
2015-10-07 16:38:29 +02:00
|
|
|
|
2016-10-11 11:12:45 +02:00
|
|
|
sign_out :user
|
|
|
|
|
2017-04-05 10:22:37 +02:00
|
|
|
case connected_with_france_connect
|
|
|
|
when 'entreprise'
|
2016-10-11 11:12:45 +02:00
|
|
|
redirect_to FRANCE_CONNECT.entreprise_logout_endpoint
|
|
|
|
return
|
2017-04-05 10:22:37 +02:00
|
|
|
when 'particulier'
|
2016-10-11 11:12:45 +02:00
|
|
|
redirect_to FRANCE_CONNECT.particulier_logout_endpoint
|
|
|
|
return
|
|
|
|
end
|
2015-10-07 16:38:29 +02:00
|
|
|
end
|
2016-10-11 11:12:45 +02:00
|
|
|
|
|
|
|
respond_to_on_destroy
|
2015-10-07 16:38:29 +02:00
|
|
|
end
|
2015-09-23 10:02:01 +02:00
|
|
|
|
2016-05-26 15:59:50 +02:00
|
|
|
def no_procedure
|
|
|
|
session['user_return_to'] = nil
|
|
|
|
redirect_to new_user_session_path
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2015-09-23 10:02:01 +02:00
|
|
|
|
2016-05-26 15:59:50 +02:00
|
|
|
def error_procedure
|
|
|
|
flash.alert = t('errors.messages.procedure_not_found')
|
|
|
|
redirect_to url_for root_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_return_to_procedure_id
|
|
|
|
return nil if session["user_return_to"].nil?
|
|
|
|
|
|
|
|
NumberService.to_number session["user_return_to"].split("?procedure_id=").second
|
|
|
|
end
|
2016-10-11 11:12:45 +02:00
|
|
|
|
2017-03-07 10:15:33 +01:00
|
|
|
def try_to_authenticate(klass, remember_me = false)
|
2016-10-11 11:12:45 +02:00
|
|
|
if resource = klass.find_for_database_authentication(email: params[:user][:email])
|
|
|
|
if resource.valid_password?(params[:user][:password])
|
2017-03-07 10:15:33 +01:00
|
|
|
resource.remember_me = remember_me
|
2016-10-11 11:12:45 +02:00
|
|
|
sign_in resource
|
2017-02-07 16:56:21 +01:00
|
|
|
resource.force_sync_credentials
|
2016-10-11 11:12:45 +02:00
|
|
|
set_flash_message :notice, :signed_in
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-09-23 10:02:01 +02:00
|
|
|
end
|