Fixed issue #2685 - Scheduler/Job with current_user in condition is not working.

This commit is contained in:
Martin Edenhofer 2019-07-26 16:31:24 +02:00 committed by Thorsten Eckel
parent c57a320d8d
commit 2d7846e7af
2 changed files with 61 additions and 27 deletions

View file

@ -51,45 +51,50 @@ job.run(true)
def run(force = false, start_at = Time.zone.now) def run(force = false, start_at = Time.zone.now)
logger.debug { "Execute job #{inspect}" } logger.debug { "Execute job #{inspect}" }
if !executable?(start_at) && force == false tickets = nil
if next_run_at && next_run_at <= Time.zone.now Transaction.execute(reset_user_id: true) do
if !executable?(start_at) && force == false
if next_run_at && next_run_at <= Time.zone.now
save!
end
return
end
# find tickets to change
matching = matching_count
if self.matching != matching
self.matching = matching
save! save!
end end
return
end
matching = matching_count if !in_timeplan?(start_at) && force == false
if self.matching != matching if next_run_at && next_run_at <= Time.zone.now
self.matching = matching save!
end
return
end
ticket_count, tickets = Ticket.selectors(condition, limit: 2_000)
logger.debug { "Job #{name} with #{ticket_count} tickets" }
self.processed = ticket_count || 0
self.running = true
self.last_run_at = Time.zone.now
save! save!
end end
if !in_timeplan?(start_at) && force == false
if next_run_at && next_run_at <= Time.zone.now
save!
end
return
end
# find tickets to change
ticket_count, tickets = Ticket.selectors(condition, limit: 2_000)
logger.debug { "Job #{name} with #{ticket_count} tickets" }
self.processed = ticket_count || 0
self.running = true
self.last_run_at = Time.zone.now
save!
tickets&.each do |ticket| tickets&.each do |ticket|
Transaction.execute(disable_notification: disable_notification, reset_user_id: true) do Transaction.execute(disable_notification: disable_notification, reset_user_id: true) do
ticket.perform_changes(perform, 'job') ticket.perform_changes(perform, 'job')
end end
end end
self.running = false Transaction.execute(reset_user_id: true) do
self.last_run_at = Time.zone.now self.running = false
save! self.last_run_at = Time.zone.now
save!
end
end end
def executable?(start_at = Time.zone.now) def executable?(start_at = Time.zone.now)

View file

@ -201,6 +201,35 @@ RSpec.describe Job, type: :model do
end end
end end
end end
context 'when job has pre_condition:current_user.id in selector' do
let!(:matching_ticket) { create(:ticket, owner_id: 1) }
let!(:nonmatching_ticket) { create(:ticket, owner_id: create(:agent_user).id) }
let(:condition) do
{
'ticket.owner_id' => {
'operator' => 'is',
'pre_condition' => 'current_user.id',
'value' => '',
'value_completion' => ''
},
}
end
before do
UserInfo.current_user_id = create(:admin_user).id
job
UserInfo.current_user_id = nil
end
it 'performs changes on matching tickets' do
expect { job.run(true) }
.to change { matching_ticket.reload.state }
.and not_change { nonmatching_ticket.reload.state }
end
end
end end
describe '#executable?' do describe '#executable?' do