@@ -22,8 +22,6 @@ class OmniauthCallbacksController < Devise::OmniauthCallbacksController
2222 #
2323 # scheme - The IdentifierScheme for the provider
2424 #
25- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
26- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
2725 def handle_omniauth ( scheme )
2826 user = if request . env [ 'omniauth.auth' ] . nil?
2927 User . from_omniauth ( request . env )
@@ -33,59 +31,66 @@ def handle_omniauth(scheme)
3331
3432 # If the user isn't logged in
3533 if current_user . nil?
36- # If the uid didn't have a match in the system send them to register
37- if user . nil?
38- session [ "devise.#{ scheme . name . downcase } _data" ] = request . env [ 'omniauth.auth' ]
39- redirect_to new_user_registration_url
34+ handle_omniauth_for_signed_out_user ( user , scheme )
35+ # The user is already logged in and just registering the uid with us
36+ else
37+ handle_omniauth_for_signed_in_user ( user , scheme )
38+ end
39+ end
40+
41+ def failure
42+ redirect_to root_path
43+ end
4044
41- # Otherwise sign them in
42- elsif scheme . name == 'shibboleth'
43- # Until ORCID becomes supported as a login method
45+ private
4446
45- # (see app/models/concerns/email_confirmation_handler.rb)
46- return if confirmation_instructions_missing_and_handled? ( user )
47+ def handle_omniauth_for_signed_in_user ( user , scheme )
48+ # If the user could not be found by that uid then attach it to their record
49+ if user . nil?
50+ if Identifier . create ( identifier_scheme : scheme ,
51+ value : request . env [ 'omniauth.auth' ] . uid ,
52+ attrs : request . env [ 'omniauth.auth' ] ,
53+ identifiable : current_user )
54+ flash [ :notice ] =
55+ format ( _ ( 'Your account has been successfully linked to %{scheme}.' ) ,
56+ scheme : scheme . description )
4757
48- set_flash_message ( :notice , :success , kind : scheme . description ) if is_navigational_format?
49- sign_in_and_redirect user , event : :authentication
5058 else
51- flash [ :notice ] = _ ( 'Successfully signed in' )
52- redirect_to new_user_registration_url
59+ flash [ :alert ] = format ( _ ( 'Unable to link your account to %{scheme}.' ) ,
60+ scheme : scheme . description )
5361 end
5462
55- # The user is already logged in and just registering the uid with us
56- else
57- # If the user could not be found by that uid then attach it to their record
58- if user . nil?
59- if Identifier . create ( identifier_scheme : scheme ,
60- value : request . env [ 'omniauth.auth' ] . uid ,
61- attrs : request . env [ 'omniauth.auth' ] ,
62- identifiable : current_user )
63- flash [ :notice ] =
64- format ( _ ( 'Your account has been successfully linked to %{scheme}.' ) ,
65- scheme : scheme . description )
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+ # rubocop:disable Layout/LineLength
67+ flash [ :alert ] = _ ( "The current #{ scheme . description } iD has been already linked to a user with email #{ identifier . user . email } " )
68+ # rubocop:enable Layout/LineLength
69+ end
6670
67- else
68- flash [ :alert ] = format ( _ ( 'Unable to link your account to %{scheme}.' ) ,
69- scheme : scheme . description )
70- end
71+ # Redirect to the User Profile page
72+ redirect_to edit_user_registration_path
73+ end
7174
72- elsif user . id != current_user . id
73- # If a user was found but does NOT match the current user then the identifier has
74- # already been attached to another account (likely the user has 2 accounts)
75- # rubocop:disable Layout/LineLength
76- flash [ :alert ] = _ ( "The current #{ scheme . description } iD has been already linked to a user with email #{ identifier . user . email } " )
77- # rubocop:enable Layout/LineLength
78- end
75+ def handle_omniauth_for_signed_out_user ( user , scheme )
76+ # If the uid didn't have a match in the system send them to register
77+ if user . nil?
78+ session [ "devise.#{ scheme . name . downcase } _data" ] = request . env [ 'omniauth.auth' ]
79+ redirect_to new_user_registration_url
7980
80- # Redirect to the User Profile page
81- redirect_to edit_user_registration_path
82- end
83- end
84- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
85- # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
81+ # Otherwise sign them in
82+ elsif scheme . name == 'shibboleth'
83+ # Until ORCID becomes supported as a login method
8684
87- def failure
88- redirect_to root_path
85+ # (see app/models/concerns/email_confirmation_handler.rb)
86+ return if confirmation_instructions_missing_and_handled? ( user )
87+
88+ set_flash_message ( :notice , :success , kind : scheme . description ) if is_navigational_format?
89+ sign_in_and_redirect user , event : :authentication
90+ else
91+ flash [ :notice ] = _ ( 'Successfully signed in' )
92+ redirect_to new_user_registration_url
93+ end
8994 end
9095 end
9196end
0 commit comments