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,6 +51,8 @@ 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}" }
tickets = nil
Transaction.execute(reset_user_id: true) do
if !executable?(start_at) && force == false if !executable?(start_at) && force == false
if next_run_at && next_run_at <= Time.zone.now if next_run_at && next_run_at <= Time.zone.now
save! save!
@ -58,6 +60,7 @@ job.run(true)
return return
end end
# find tickets to change
matching = matching_count matching = matching_count
if self.matching != matching if self.matching != matching
self.matching = matching self.matching = matching
@ -71,7 +74,6 @@ job.run(true)
return return
end end
# find tickets to change
ticket_count, tickets = Ticket.selectors(condition, limit: 2_000) ticket_count, tickets = Ticket.selectors(condition, limit: 2_000)
logger.debug { "Job #{name} with #{ticket_count} tickets" } logger.debug { "Job #{name} with #{ticket_count} tickets" }
@ -80,6 +82,7 @@ job.run(true)
self.running = true self.running = true
self.last_run_at = Time.zone.now self.last_run_at = Time.zone.now
save! save!
end
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
@ -87,10 +90,12 @@ job.run(true)
end end
end end
Transaction.execute(reset_user_id: true) do
self.running = false self.running = false
self.last_run_at = Time.zone.now self.last_run_at = Time.zone.now
save! save!
end end
end
def executable?(start_at = Time.zone.now) def executable?(start_at = Time.zone.now)
return false if !active return false if !active

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