Project

General

Profile

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

Takashi Kato, 2023-09-24 17:40

View differences:

Gemfile
4 4

  
5 5
gem 'rails', '6.1.7.6'
6 6
gem 'rouge', '~> 4.1.0'
7
gem 'request_store', '~> 1.5.0'
8 7
gem 'mini_mime', '~> 1.1.0'
9 8
gem "actionpack-xml_parser"
10 9
gem 'roadie-rails', '~> 3.0.0'
app/jobs/application_job.rb
1 1
# frozen_string_literal: true
2 2

  
3 3
class ApplicationJob < ActiveJob::Base
4
  include Redmine::JobWrapper
5

  
6
  around_enqueue :keep_current_user
4 7
end
app/models/mailer.rb
28 28
  include Redmine::I18n
29 29
  include Roadie::Rails::Automatic
30 30

  
31
  class DeliveryJob < ActionMailer::MailDeliveryJob
32
    include Redmine::JobWrapper
33

  
34
    around_enqueue :keep_current_user
35
  end
36

  
37
  self.delivery_job = DeliveryJob
38

  
31 39
  # Overrides ActionMailer::Base#process in order to set the recipient as the current user
32 40
  # and his language as the default locale.
33 41
  # The first argument of all actions of this Mailer must be a User (the recipient),
app/models/user.rb
853 853
    self.pref.notify_about_high_priority_issues
854 854
  end
855 855

  
856
  class CurrentUser < ActiveSupport::CurrentAttributes
857
    attribute :user
858
  end
859

  
856 860
  def self.current=(user)
857
    RequestStore.store[:current_user] = user
861
    CurrentUser.user = user
858 862
  end
859 863

  
860 864
  def self.current
861
    RequestStore.store[:current_user] ||= User.anonymous
865
    CurrentUser.user ||= User.anonymous
862 866
  end
863 867

  
864 868
  # Returns the anonymous user.  If the anonymous user does not exist, it is created.  There can be only
lib/redmine/job_wrapper.rb
1
# frozen_string_literal: true
2

  
3
# Redmine - project management software
4
# Copyright (C) 2006-2023  Jean-Philippe Lang
5
#
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU General Public License
8
# as published by the Free Software Foundation; either version 2
9
# of the License, or (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19

  
20
module Redmine
21
  module JobWrapper
22
    def keep_current_user
23
      current_user = User.current
24
      yield
25
      User.current = current_user
26
    end
27
  end
28
end
lib/redmine/sudo_mode.rb
170 170
      end
171 171
    end
172 172

  
173
    class CurrentSudoMode < ActiveSupport::CurrentAttributes
174
      attribute :was_used, :active, :disabled
175
    end
176

  
173 177
    # true if the sudo mode state was queried during this request
174 178
    def self.was_used?
175
      !!RequestStore.store[:sudo_mode_was_used]
179
      !!CurrentSudoMode.was_used
176 180
    end
177 181

  
178 182
    # true if sudo mode is currently active.
......
184 188
    # If you do it wrong, timeout of the sudo mode will happen too late or not at
185 189
    # all.
186 190
    def self.active?
187
      if !!RequestStore.store[:sudo_mode]
188
        RequestStore.store[:sudo_mode_was_used] = true
191
      if !!CurrentSudoMode.active
192
        CurrentSudoMode.was_used = true
189 193
      end
190 194
    end
191 195

  
192 196
    def self.active!
193
      RequestStore.store[:sudo_mode] = true
197
      CurrentSudoMode.active = true
194 198
    end
195 199

  
196 200
    def self.possible?
......
199 203

  
200 204
    # Turn off sudo mode (never require password entry).
201 205
    def self.disable!
202
      RequestStore.store[:sudo_mode_disabled] = true
206
      CurrentSudoMode.disabled = true
203 207
    end
204 208

  
205 209
    # Turn sudo mode back on
206 210
    def self.enable!
207
      RequestStore.store[:sudo_mode_disabled] = nil
211
      CurrentSUdoMode.disabled = nil
208 212
    end
209 213

  
210 214
    def self.enabled?
211
      Redmine::Configuration['sudo_mode'] && !RequestStore.store[:sudo_mode_disabled]
215
      Redmine::Configuration['sudo_mode'] && !CurrentSudoMode.disabled
212 216
    end
213 217

  
214 218
    # Timespan after which sudo mode expires when unused.
test/unit/jobs/destroy_project_job_test.rb
58 58
      assert_match /deleted successfully/, m.text_part.to_s
59 59
    else
60 60
      assert_enqueued_with(
61
        job: ActionMailer::MailDeliveryJob,
61
        job: Mailer::DeliveryJob,
62 62
        args: ->(job_args){
63 63
          job_args[1] == 'security_notification' &&
64 64
          job_args[3].to_s.include?("mail_destroy_project_with_subprojects_successful")
test/unit/jobs/destroy_projects_job_test.rb
58 58
      assert_match /deleted successfully/, m.text_part.to_s
59 59
    else
60 60
      assert_enqueued_with(
61
        job: ActionMailer::MailDeliveryJob,
61
        job: Mailer::DeliveryJob,
62 62
        args: ->(job_args){
63 63
          job_args[1] == 'security_notification' &&
64 64
          job_args[3].to_s.include?("mail_destroy_project_with_subprojects_successful")
    (1-1/1)