From 271286e5b394f829fd44fb35f89e61896479b61e Mon Sep 17 00:00:00 2001
From: Takashi Kato <tohosaku@users.osdn.me>
Date: Wed, 13 Sep 2023 22:41:40 +0000
Subject: [PATCH] use ActiveSupport::CurrentAttributes instead of RequestStore

---
 Gemfile                                     |  1 -
 app/jobs/application_job.rb                 |  3 +++
 app/models/mailer.rb                        |  8 ++++++
 app/models/user.rb                          |  8 ++++--
 lib/redmine/job_wrapper.rb                  | 28 +++++++++++++++++++++
 lib/redmine/sudo_mode.rb                    | 18 +++++++------
 test/unit/jobs/destroy_project_job_test.rb  |  2 +-
 test/unit/jobs/destroy_projects_job_test.rb |  2 +-
 8 files changed, 58 insertions(+), 12 deletions(-)
 create mode 100644 lib/redmine/job_wrapper.rb

diff --git a/Gemfile b/Gemfile
index a925c8599..201514bc7 100644
--- a/Gemfile
+++ b/Gemfile
@@ -4,7 +4,6 @@ ruby '>= 2.7.0', '< 3.3.0'
 
 gem 'rails', '6.1.7.6'
 gem 'rouge', '~> 4.1.0'
-gem 'request_store', '~> 1.5.0'
 gem 'mini_mime', '~> 1.1.0'
 gem "actionpack-xml_parser"
 gem 'roadie-rails', '~> 3.0.0'
diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb
index d92ffddcb..9d4c32b66 100644
--- a/app/jobs/application_job.rb
+++ b/app/jobs/application_job.rb
@@ -1,4 +1,7 @@
 # frozen_string_literal: true
 
 class ApplicationJob < ActiveJob::Base
+  include Redmine::JobWrapper
+
+  around_enqueue :keep_current_user
 end
diff --git a/app/models/mailer.rb b/app/models/mailer.rb
index 558c280ec..689e31901 100644
--- a/app/models/mailer.rb
+++ b/app/models/mailer.rb
@@ -28,6 +28,14 @@ class Mailer < ActionMailer::Base
   include Redmine::I18n
   include Roadie::Rails::Automatic
 
+  class DeliveryJob < ActionMailer::MailDeliveryJob
+    include Redmine::JobWrapper
+
+    around_enqueue :keep_current_user
+  end
+
+  self.delivery_job = DeliveryJob
+
   # Overrides ActionMailer::Base#process in order to set the recipient as the current user
   # and his language as the default locale.
   # The first argument of all actions of this Mailer must be a User (the recipient),
diff --git a/app/models/user.rb b/app/models/user.rb
index 88e18b8d0..234577841 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -853,12 +853,16 @@ class User < Principal
     self.pref.notify_about_high_priority_issues
   end
 
+  class CurrentUser < ActiveSupport::CurrentAttributes
+    attribute :user
+  end
+
   def self.current=(user)
-    RequestStore.store[:current_user] = user
+    CurrentUser.user = user
   end
 
   def self.current
-    RequestStore.store[:current_user] ||= User.anonymous
+    CurrentUser.user ||= User.anonymous
   end
 
   # Returns the anonymous user.  If the anonymous user does not exist, it is created.  There can be only
diff --git a/lib/redmine/job_wrapper.rb b/lib/redmine/job_wrapper.rb
new file mode 100644
index 000000000..2d20b8e82
--- /dev/null
+++ b/lib/redmine/job_wrapper.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+# Redmine - project management software
+# Copyright (C) 2006-2023  Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+module Redmine
+  module JobWrapper
+    def keep_current_user
+      current_user = User.current
+      yield
+      User.current = current_user
+    end
+  end
+end
diff --git a/lib/redmine/sudo_mode.rb b/lib/redmine/sudo_mode.rb
index 9703f0c77..e4518ca7f 100644
--- a/lib/redmine/sudo_mode.rb
+++ b/lib/redmine/sudo_mode.rb
@@ -170,9 +170,13 @@ module Redmine
       end
     end
 
+    class CurrentSudoMode < ActiveSupport::CurrentAttributes
+      attribute :was_used, :active, :disabled
+    end
+
     # true if the sudo mode state was queried during this request
     def self.was_used?
-      !!RequestStore.store[:sudo_mode_was_used]
+      !!CurrentSudoMode.was_used
     end
 
     # true if sudo mode is currently active.
@@ -184,13 +188,13 @@ module Redmine
     # If you do it wrong, timeout of the sudo mode will happen too late or not at
     # all.
     def self.active?
-      if !!RequestStore.store[:sudo_mode]
-        RequestStore.store[:sudo_mode_was_used] = true
+      if !!CurrentSudoMode.active
+        CurrentSudoMode.was_used = true
       end
     end
 
     def self.active!
-      RequestStore.store[:sudo_mode] = true
+      CurrentSudoMode.active = true
     end
 
     def self.possible?
@@ -199,16 +203,16 @@ module Redmine
 
     # Turn off sudo mode (never require password entry).
     def self.disable!
-      RequestStore.store[:sudo_mode_disabled] = true
+      CurrentSudoMode.disabled = true
     end
 
     # Turn sudo mode back on
     def self.enable!
-      RequestStore.store[:sudo_mode_disabled] = nil
+      CurrentSUdoMode.disabled = nil
     end
 
     def self.enabled?
-      Redmine::Configuration['sudo_mode'] && !RequestStore.store[:sudo_mode_disabled]
+      Redmine::Configuration['sudo_mode'] && !CurrentSudoMode.disabled
     end
 
     # Timespan after which sudo mode expires when unused.
diff --git a/test/unit/jobs/destroy_project_job_test.rb b/test/unit/jobs/destroy_project_job_test.rb
index 83aaa88d3..a8f912efd 100644
--- a/test/unit/jobs/destroy_project_job_test.rb
+++ b/test/unit/jobs/destroy_project_job_test.rb
@@ -58,7 +58,7 @@ class DestroyProjectJobTest < ActiveJob::TestCase
       assert_match /deleted successfully/, m.text_part.to_s
     else
       assert_enqueued_with(
-        job: ActionMailer::MailDeliveryJob,
+        job: Mailer::DeliveryJob,
         args: ->(job_args){
           job_args[1] == 'security_notification' &&
           job_args[3].to_s.include?("mail_destroy_project_with_subprojects_successful")
diff --git a/test/unit/jobs/destroy_projects_job_test.rb b/test/unit/jobs/destroy_projects_job_test.rb
index 9e2f3449e..b6809bb51 100644
--- a/test/unit/jobs/destroy_projects_job_test.rb
+++ b/test/unit/jobs/destroy_projects_job_test.rb
@@ -58,7 +58,7 @@ class DestroyProjectsJobTest < ActiveJob::TestCase
       assert_match /deleted successfully/, m.text_part.to_s
     else
       assert_enqueued_with(
-        job: ActionMailer::MailDeliveryJob,
+        job: Mailer::DeliveryJob,
         args: ->(job_args){
           job_args[1] == 'security_notification' &&
           job_args[3].to_s.include?("mail_destroy_project_with_subprojects_successful")
-- 
2.39.2

