From 180f0e6715ded984d8f14dab49f4f3f44f03ec53 Mon Sep 17 00:00:00 2001
From: Claire <claire.github-309c@sitedethib.com>
Date: Sun, 2 Jul 2023 16:08:58 +0200
Subject: [PATCH] Fix inefficient query when requesting a new confirmation
 email from a logged-in account (#25669)

---
 .../auth/confirmations_controller.rb           | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/app/controllers/auth/confirmations_controller.rb b/app/controllers/auth/confirmations_controller.rb
index 632b624a37..3c8d09993b 100644
--- a/app/controllers/auth/confirmations_controller.rb
+++ b/app/controllers/auth/confirmations_controller.rb
@@ -28,6 +28,24 @@ class Auth::ConfirmationsController < Devise::ConfirmationsController
     resource.email = current_user.unconfirmed_email || current_user.email if user_signed_in?
   end
 
+  def create
+    # Since we don't allow users to request confirmation emails for other accounts when they
+    # are already logged in, so we can cut on the expensive queries by simply reusing the
+    # current user.
+    if user_signed_in?
+      self.resource = current_user
+      resource.send_confirmation_instructions
+    else
+      self.resource = current_user || User.send_confirmation_instructions(resource_params)
+    end
+
+    if successfully_sent?(resource)
+      respond_with({}, location: after_resending_confirmation_instructions_path_for(resource_name))
+    else
+      respond_with(resource)
+    end
+  end
+
   def confirm_captcha
     check_captcha! do |message|
       flash.now[:alert] = message