@@ -27,26 +27,29 @@ def connect_provider
27
27
provider = auth_hash . provider . to_s
28
28
return head ( :bad_request ) unless AuthenticationOption ::OAUTH_CREDENTIAL_TYPES . include? provider
29
29
30
- # Check if credential is already in use
31
30
existing_credential_holder = User . find_by_credential type : provider , id : auth_hash . uid
31
+
32
+ # Credential is already held by the current user
33
+ # Notify of no-op.
34
+ if existing_credential_holder &.==( current_user )
35
+ flash . notice = I18n . t ( 'auth.already_linked' , provider : I18n . t ( "auth.#{ provider } " ) )
36
+ return redirect_to edit_user_registration_path
37
+ end
38
+
39
+ # Credential is already held by another user with activity
40
+ # Display an error explaining that the credential is already in use.
41
+ if existing_credential_holder &.has_activity?
42
+ flash . alert = I18n . t ( 'auth.already_in_use' , provider : I18n . t ( "auth.#{ provider } " ) )
43
+ return redirect_to edit_user_registration_path
44
+ end
45
+
46
+ # Credential is already held by an unused account.
47
+ # Take over the unused account.
32
48
if existing_credential_holder
33
- if existing_credential_holder == current_user
34
- flash . notice = I18n . t ( 'auth.already_linked' , provider : I18n . t ( "auth.#{ provider } " ) )
35
- return redirect_to edit_user_registration_path
36
- elsif existing_credential_holder . has_activity?
37
- # Linking is not possible and takeover is not possible
38
- # Display a custom error message explaining the credential is already
39
- # tied to an account, and what we can do about it.
40
- flash . alert = I18n . t ( 'auth.already_in_use' , provider : I18n . t ( "auth.#{ provider } " ) )
41
- return redirect_to edit_user_registration_path
42
- else
43
- # The credential is tied to an unused account.
44
- # Destroy the unused account and proceed to link this one.
45
- move_sections_and_destroy_source_user \
46
- source_user : existing_credential_holder ,
47
- destination_user : current_user ,
48
- takeover_type : 'connect_provider'
49
- end
49
+ move_sections_and_destroy_source_user \
50
+ source_user : existing_credential_holder ,
51
+ destination_user : current_user ,
52
+ takeover_type : 'connect_provider'
50
53
end
51
54
52
55
# TODO: some of this won't work right for non-Google providers, because info comes in differently
0 commit comments