Project

General

Profile

Patch #39110 ยป 0001-use-ActiveSupport-CurrentAttributes-instead-of-Reque.patch

Takashi Kato, 2023-09-24 17:40

View differences:

Gemfile
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'
app/jobs/application_job.rb
# frozen_string_literal: true
class ApplicationJob < ActiveJob::Base
include Redmine::JobWrapper
around_enqueue :keep_current_user
end
app/models/mailer.rb
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),
app/models/user.rb
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
lib/redmine/job_wrapper.rb
# 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
lib/redmine/sudo_mode.rb
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.
......
# 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?
......
# 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.
test/unit/jobs/destroy_project_job_test.rb
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")
test/unit/jobs/destroy_projects_job_test.rb
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")
    (1-1/1)