From 4959c1bad1005466fc285bc65c6cfe4bbabd7803 Mon Sep 17 00:00:00 2001
From: Ahmad Farhat <ahmad.af.farhat@gmail.com>
Date: Thu, 14 Apr 2022 12:17:29 -0400
Subject: [PATCH] Added env variable needed when switching social_uids (#3393)

---
 app/controllers/concerns/authenticator.rb | 2 ++
 app/controllers/sessions_controller.rb    | 5 ++++-
 config/application.rb                     | 2 ++
 3 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/app/controllers/concerns/authenticator.rb b/app/controllers/concerns/authenticator.rb
index 45150f52..88e5e3d8 100644
--- a/app/controllers/concerns/authenticator.rb
+++ b/app/controllers/concerns/authenticator.rb
@@ -85,6 +85,8 @@ module Authenticator
 
   # Check if the user exists under the same email with no social uid and that social accounts are allowed
   def auth_changed_to_social?(email)
+    return true if Rails.configuration.social_switching
+
     Rails.configuration.loadbalanced_configuration &&
       User.exists?(email: email, provider: @user_domain) &&
       !allow_greenlight_accounts?
diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb
index 65d9cb28..d6dbe8c0 100644
--- a/app/controllers/sessions_controller.rb
+++ b/app/controllers/sessions_controller.rb
@@ -266,7 +266,10 @@ flash: { alert: I18n.t("registration.insecure_password") } unless User.secure_pa
 
   # Set the user's social id to the new id being passed
   def switch_account_to_social
-    user = User.find_by(email: @auth['info']['email'], provider: @user_domain)
+    user = User.find_by({
+      email: @auth['info']['email'],
+      provider: Rails.configuration.loadbalanced_configuration ? @user_domain : nil
+    }.compact)
 
     logger.info "Switching social account for #{user.uid}"
 
diff --git a/config/application.rb b/config/application.rb
index 80ec53b5..d23746fd 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -186,5 +186,7 @@ module Greenlight
 
     # Max avatar image size
     config.max_avatar_size = ENV['MAX_AVATAR_SIZE'].to_i.zero? ? 100_000 : ENV['MAX_AVATAR_SIZE'].to_i
+
+    config.social_switching = ENV['SOCIAL_SWITCHING'] == "true"
   end
 end
-- 
GitLab