33module Users
44 # Controller that handles callbacks from OmniAuth integrations (e.g. Shibboleth and ORCID)
55 class OmniauthCallbacksController < Devise ::OmniauthCallbacksController
6+ include EmailConfirmationHandler
67 ##
78 # Dynamically build a handler for each omniauth provider
89 # -------------------------------------------------------------
@@ -21,8 +22,6 @@ class OmniauthCallbacksController < Devise::OmniauthCallbacksController
2122 #
2223 # scheme - The IdentifierScheme for the provider
2324 #
24- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
25- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
2625 def handle_omniauth ( scheme )
2726 user = if request . env [ 'omniauth.auth' ] . nil?
2827 User . from_omniauth ( request . env )
@@ -32,55 +31,68 @@ def handle_omniauth(scheme)
3231
3332 # If the user isn't logged in
3433 if current_user . nil?
35- # If the uid didn't have a match in the system send them to register
36- if user . nil?
37- session [ "devise.#{ scheme . name . downcase } _data" ] = request . env [ 'omniauth.auth' ]
38- redirect_to new_user_registration_url
39-
40- # Otherwise sign them in
41- elsif scheme . name == 'shibboleth'
42- # Until ORCID becomes supported as a login method
43- set_flash_message ( :notice , :success , kind : scheme . description ) if is_navigational_format?
44- sign_in_and_redirect user , event : :authentication
45- else
46- flash [ :notice ] = _ ( 'Successfully signed in' )
47- redirect_to new_user_registration_url
48- end
49-
34+ handle_omniauth_for_signed_out_user ( user , scheme )
5035 # The user is already logged in and just registering the uid with us
5136 else
52- # If the user could not be found by that uid then attach it to their record
53- if user . nil?
54- if Identifier . create ( identifier_scheme : scheme ,
55- value : request . env [ 'omniauth.auth' ] . uid ,
56- attrs : request . env [ 'omniauth.auth' ] ,
57- identifiable : current_user )
58- flash [ :notice ] =
59- format ( _ ( 'Your account has been successfully linked to %{scheme}.' ) ,
60- scheme : scheme . description )
37+ handle_omniauth_for_signed_in_user ( user , scheme )
38+ end
39+ end
40+
41+ def failure
42+ redirect_to root_path
43+ end
6144
62- else
63- flash [ :alert ] = format ( _ ( 'Unable to link your account to %{scheme}.' ) ,
64- scheme : scheme . description )
65- end
45+ private
6646
67- elsif user . id != current_user . id
68- # If a user was found but does NOT match the current user then the identifier has
69- # already been attached to another account (likely the user has 2 accounts)
70- # rubocop:disable Layout/LineLength
71- flash [ :alert ] = _ ( "The current #{ scheme . description } iD has been already linked to a user with email #{ identifier . user . email } " )
72- # rubocop:enable Layout/LineLength
47+ # rubocop:disable Metrics/AbcSize
48+ def handle_omniauth_for_signed_in_user ( user , scheme )
49+ # If the user could not be found by that uid then attach it to their record
50+ if user . nil?
51+ if Identifier . create ( identifier_scheme : scheme ,
52+ value : request . env [ 'omniauth.auth' ] . uid ,
53+ attrs : request . env [ 'omniauth.auth' ] ,
54+ identifiable : current_user )
55+ flash [ :notice ] = format ( _ ( 'Your account has been successfully linked to %{scheme}.' ) ,
56+ scheme : scheme . description )
57+
58+ else
59+ flash [ :alert ] = format ( _ ( 'Unable to link your account to %{scheme}.' ) ,
60+ scheme : scheme . description )
7361 end
7462
75- # Redirect to the User Profile page
76- redirect_to edit_user_registration_path
63+ elsif user . id != current_user . id
64+ # If a user was found but does NOT match the current user then the identifier has
65+ # already been attached to another account (likely the user has 2 accounts)
66+ flash [ :alert ] = _ ( "The current #{ scheme . description } iD has been already linked " \
67+ "to a user with email #{ identifier . user . email } " )
7768 end
69+
70+ # Redirect to the User Profile page
71+ redirect_to edit_user_registration_path
7872 end
79- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
80- # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
73+ # rubocop:enable Metrics/AbcSize
8174
82- def failure
83- redirect_to root_path
75+ # rubocop:disable Metrics/AbcSize
76+ def handle_omniauth_for_signed_out_user ( user , scheme )
77+ # If the uid didn't have a match in the system send them to register
78+ if user . nil?
79+ session [ "devise.#{ scheme . name . downcase } _data" ] = request . env [ 'omniauth.auth' ]
80+ redirect_to new_user_registration_url
81+
82+ # Otherwise sign them in
83+ elsif scheme . name == 'shibboleth'
84+ # Until ORCID becomes supported as a login method
85+
86+ # (see app/models/concerns/email_confirmation_handler.rb)
87+ return if confirmation_instructions_missing_and_handled? ( user )
88+
89+ set_flash_message ( :notice , :success , kind : scheme . description ) if is_navigational_format?
90+ sign_in_and_redirect user , event : :authentication
91+ else
92+ flash [ :notice ] = _ ( 'Successfully signed in' )
93+ redirect_to new_user_registration_url
94+ end
8495 end
96+ # rubocop:enable Metrics/AbcSize
8597 end
8698end
0 commit comments