Fixed issue#190 - differ between ticket customer and the user who sends the email.

This commit is contained in:
Martin Edenhofer 2016-08-23 11:49:12 +02:00
parent 4319023ad1
commit 9797dbb1d4
4 changed files with 45 additions and 19 deletions

View file

@ -385,7 +385,7 @@ retrns
File.open(filename, 'wb') { |file| File.open(filename, 'wb') { |file|
file.write msg file.write msg
} }
raise e.inspect raise e.inspect + e.backtrace.inspect
end end
@ -428,18 +428,18 @@ retrns
# use transaction # use transaction
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
# create sender if needed # get sender user
sender_user_id = mail[ 'x-zammad-customer-id'.to_sym ] session_user_id = mail[ 'x-zammad-session-user-id'.to_sym ]
if !sender_user_id if !session_user_id
raise 'No x-zammad-customer-id, no sender set!' raise 'No x-zammad-session-user-id, no sender set!'
end end
user = User.lookup(id: sender_user_id) session_user = User.lookup(id: session_user_id)
if !user if !session_user
raise "No user found for x-zammad-customer-id: #{sender_user_id}!" raise "No user found for x-zammad-session-user-id: #{session_user_id}!"
end end
# set current user # set current user
UserInfo.current_user_id = user.id UserInfo.current_user_id = session_user.id
# get ticket# based on email headers # get ticket# based on email headers
if mail[ 'x-zammad-ticket-id'.to_sym ] if mail[ 'x-zammad-ticket-id'.to_sym ]
@ -468,7 +468,7 @@ retrns
if !mail[ 'x-zammad-ticket-followup-state'.to_sym ] if !mail[ 'x-zammad-ticket-followup-state'.to_sym ]
if state_type.name != 'new' && !mail[ 'x-zammad-out-of-office'.to_sym ] if state_type.name != 'new' && !mail[ 'x-zammad-out-of-office'.to_sym ]
ticket.state = Ticket::State.find_by(name: 'open') ticket.state = Ticket::State.find_by(name: 'open')
ticket.save ticket.save!
end end
end end
end end
@ -496,7 +496,6 @@ retrns
end end
ticket = Ticket.new( ticket = Ticket.new(
group_id: group.id, group_id: group.id,
customer_id: user.id,
title: mail[:subject] || '', title: mail[:subject] || '',
state_id: Ticket::State.find_by(name: 'new').id, state_id: Ticket::State.find_by(name: 'new').id,
priority_id: Ticket::Priority.find_by(name: '2 normal').id, priority_id: Ticket::Priority.find_by(name: '2 normal').id,

View file

@ -4,14 +4,14 @@ module Channel::Filter::IdentifySender
def self.run(_channel, mail) def self.run(_channel, mail)
customer_user_id = mail[ 'x-zammad-customer-id'.to_sym ] customer_user_id = mail[ 'x-zammad-ticket-customer_id'.to_sym ]
customer_user = nil customer_user = nil
if !customer_user_id.empty? if !customer_user_id.empty?
customer_user = User.lookup(id: customer_user_id) customer_user = User.lookup(id: customer_user_id)
if customer_user if customer_user
Rails.logger.debug "Took customer form x-zammad-customer-id header '#{customer_user_id}'." Rails.logger.debug "Took customer form x-zammad-ticket-customer_id header '#{customer_user_id}'."
else else
Rails.logger.debug "Invalid x-zammad-customer-id header '#{customer_user_id}', no such user." Rails.logger.debug "Invalid x-zammad-ticket-customer_id header '#{customer_user_id}', no such user - take user from 'from'-header."
end end
end end
@ -58,10 +58,31 @@ module Channel::Filter::IdentifySender
) )
end end
end end
create_recipients(mail) create_recipients(mail)
mail[ 'x-zammad-ticket-customer_id'.to_sym ] = customer_user.id
mail[ 'x-zammad-customer-id'.to_sym ] = customer_user.id # find session user
session_user_id = mail[ 'x-zammad-session-user-id'.to_sym ]
session_user = nil
if !session_user_id.empty?
session_user = User.lookup(id: session_user_id)
if session_user
Rails.logger.debug "Took session form x-zammad-session-user-id header '#{session_user_id}'."
else
Rails.logger.debug "Invalid x-zammad-session-user-id header '#{session_user_id}', no such user - take user from 'from'-header."
end
end
if !session_user
session_user = user_create(
login: mail[:from_email],
firstname: mail[:from_display_name],
lastname: '',
email: mail[:from_email],
)
end
if session_user
mail[ 'x-zammad-session-user-id'.to_sym ] = session_user.id
end
end end
# create to and cc user # create to and cc user

View file

@ -22,8 +22,8 @@ class Channel::Filter::MonitoringBase
return if !mail[:from] return if !mail[:from]
return if !mail[:body] return if !mail[:body]
sender_user_id = mail[ 'x-zammad-customer-id'.to_sym ] session_user_id = mail[ 'x-zammad-session-user-id'.to_sym ]
return if !sender_user_id return if !session_user_id
# check if sender is monitoring # check if sender is monitoring
return if !mail[:from].match(/#{sender}/i) return if !mail[:from].match(/#{sender}/i)
@ -43,7 +43,7 @@ class Channel::Filter::MonitoringBase
} }
# check if ticket with host is open # check if ticket with host is open
customer = User.lookup(id: sender_user_id) customer = User.lookup(id: session_user_id)
# follow up detection by meta data # follow up detection by meta data
open_states = Ticket::State.by_category('open') open_states = Ticket::State.by_category('open')

View file

@ -138,6 +138,8 @@ Some Text"
assert_equal('Customer', ticket.create_article_sender.name) assert_equal('Customer', ticket.create_article_sender.name)
assert_equal('Customer', article.sender.name) assert_equal('Customer', article.sender.name)
assert_equal('ticket-system-sender-customer1@example.com', ticket.customer.email) assert_equal('ticket-system-sender-customer1@example.com', ticket.customer.email)
assert_equal(customer1.id, ticket.created_by_id)
assert_equal(customer1.id, article.created_by_id)
# process agent email # process agent email
email_raw_string = "From: ticket-system-sender-agent1@example.com email_raw_string = "From: ticket-system-sender-agent1@example.com
@ -154,6 +156,8 @@ Some Text"
assert_equal('Agent', ticket.create_article_sender.name) assert_equal('Agent', ticket.create_article_sender.name)
assert_equal('Agent', article.sender.name) assert_equal('Agent', article.sender.name)
assert_equal('ticket-system-sender-customer1@example.com', ticket.customer.email) assert_equal('ticket-system-sender-customer1@example.com', ticket.customer.email)
assert_equal(agent1.id, ticket.created_by_id)
assert_equal(agent1.id, article.created_by_id)
email_raw_string = "From: ticket-system-sender-agent1@example.com email_raw_string = "From: ticket-system-sender-agent1@example.com
To: myzammad@system.test, ticket-system-sender-customer1@example.com To: myzammad@system.test, ticket-system-sender-customer1@example.com
@ -169,6 +173,8 @@ Some Text"
assert_equal('Agent', ticket.create_article_sender.name) assert_equal('Agent', ticket.create_article_sender.name)
assert_equal('Agent', article.sender.name) assert_equal('Agent', article.sender.name)
assert_equal('ticket-system-sender-customer1@example.com', ticket.customer.email) assert_equal('ticket-system-sender-customer1@example.com', ticket.customer.email)
assert_equal(agent1.id, ticket.created_by_id)
assert_equal(agent1.id, article.created_by_id)
end end
end end