Replace request_store with ActiveSupport::CurrentAttributes (#39110).

Patch by Takashi Kato.

git-svn-id: https://svn.redmine.org/redmine/trunk@22473 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Marius Balteanu
2023-11-21 06:45:54 +00:00
parent f5d68651a0
commit fd3843d821
7 changed files with 58 additions and 11 deletions

View File

@@ -1,4 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
class ApplicationJob < ActiveJob::Base class ApplicationJob < ActiveJob::Base
include Redmine::JobWrapper
around_enqueue :keep_current_user
end end

View File

@@ -28,6 +28,14 @@ class Mailer < ActionMailer::Base
include Redmine::I18n include Redmine::I18n
include Roadie::Rails::Automatic 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 # Overrides ActionMailer::Base#process in order to set the recipient as the current user
# and his language as the default locale. # and his language as the default locale.
# The first argument of all actions of this Mailer must be a User (the recipient), # The first argument of all actions of this Mailer must be a User (the recipient),

View File

@@ -854,12 +854,16 @@ class User < Principal
self.pref.notify_about_high_priority_issues self.pref.notify_about_high_priority_issues
end end
class CurrentUser < ActiveSupport::CurrentAttributes
attribute :user
end
def self.current=(user) def self.current=(user)
RequestStore.store[:current_user] = user CurrentUser.user = user
end end
def self.current def self.current
RequestStore.store[:current_user] ||= User.anonymous CurrentUser.user ||= User.anonymous
end end
# Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only # Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only

View File

@@ -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

View File

@@ -170,9 +170,13 @@ module Redmine
end end
end end
class CurrentSudoMode < ActiveSupport::CurrentAttributes
attribute :was_used, :active, :disabled
end
# true if the sudo mode state was queried during this request # true if the sudo mode state was queried during this request
def self.was_used? def self.was_used?
!!RequestStore.store[:sudo_mode_was_used] !!CurrentSudoMode.was_used
end end
# true if sudo mode is currently active. # 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 # If you do it wrong, timeout of the sudo mode will happen too late or not at
# all. # all.
def self.active? def self.active?
if !!RequestStore.store[:sudo_mode] if !!CurrentSudoMode.active
RequestStore.store[:sudo_mode_was_used] = true CurrentSudoMode.was_used = true
end end
end end
def self.active! def self.active!
RequestStore.store[:sudo_mode] = true CurrentSudoMode.active = true
end end
def self.possible? def self.possible?
@@ -199,16 +203,16 @@ module Redmine
# Turn off sudo mode (never require password entry). # Turn off sudo mode (never require password entry).
def self.disable! def self.disable!
RequestStore.store[:sudo_mode_disabled] = true CurrentSudoMode.disabled = true
end end
# Turn sudo mode back on # Turn sudo mode back on
def self.enable! def self.enable!
RequestStore.store[:sudo_mode_disabled] = nil CurrentSUdoMode.disabled = nil
end end
def self.enabled? def self.enabled?
Redmine::Configuration['sudo_mode'] && !RequestStore.store[:sudo_mode_disabled] Redmine::Configuration['sudo_mode'] && !CurrentSudoMode.disabled
end end
# Timespan after which sudo mode expires when unused. # Timespan after which sudo mode expires when unused.

View File

@@ -58,7 +58,7 @@ class DestroyProjectJobTest < ActiveJob::TestCase
assert_match /deleted successfully/, m.text_part.to_s assert_match /deleted successfully/, m.text_part.to_s
else else
assert_enqueued_with( assert_enqueued_with(
job: ActionMailer::MailDeliveryJob, job: Mailer::DeliveryJob,
args: ->(job_args){ args: ->(job_args){
job_args[1] == 'security_notification' && job_args[1] == 'security_notification' &&
job_args[3].to_s.include?("mail_destroy_project_with_subprojects_successful") job_args[3].to_s.include?("mail_destroy_project_with_subprojects_successful")

View File

@@ -58,7 +58,7 @@ class DestroyProjectsJobTest < ActiveJob::TestCase
assert_match /deleted successfully/, m.text_part.to_s assert_match /deleted successfully/, m.text_part.to_s
else else
assert_enqueued_with( assert_enqueued_with(
job: ActionMailer::MailDeliveryJob, job: Mailer::DeliveryJob,
args: ->(job_args){ args: ->(job_args){
job_args[1] == 'security_notification' && job_args[1] == 'security_notification' &&
job_args[3].to_s.include?("mail_destroy_project_with_subprojects_successful") job_args[3].to_s.include?("mail_destroy_project_with_subprojects_successful")