Update sender name if not exists.

This commit is contained in:
Martin Edenhofer 2016-08-22 10:51:08 +02:00
parent 91ab4aef64
commit 6655485649
2 changed files with 60 additions and 9 deletions

View file

@ -33,7 +33,7 @@ class User < ApplicationModel
include User::SearchIndex
before_create :check_name, :check_email, :check_login, :check_password, :check_preferences_default, :validate_roles
before_update :check_password, :check_email, :check_login, :check_preferences_default, :validate_roles
before_update :check_password, :check_name, :check_email, :check_login, :check_preferences_default, :validate_roles
after_create :avatar_for_email_check
after_update :avatar_for_email_check
after_destroy :avatar_destroy
@ -363,12 +363,19 @@ returns
keys = [key]
end
keys.each { |local_key|
cache_key = "User::permissions?:local_key:::#{id}"
if Rails.env.production?
cache = Cache.get(cache_key)
return cache if cache
end
permissions = Object.const_get('Permission').with_parents(local_key)
Object.const_get('Permission').joins(:roles).where('roles.id IN (?) AND permissions.name IN (?)', role_ids, permissions).each { |permission|
next if permission.preferences[:selectable] == false
Cache.write(key, true, expires_in: 20.seconds)
return true
}
}
Cache.write(key, false, expires_in: 20.seconds)
false
end
@ -685,13 +692,18 @@ returns
# delete asset caches
key = "User::authorizations::#{id}"
Cache.delete(key)
# delete permission cache
key = "User::permissions?:local_key:::#{id}"
Cache.delete(key)
end
def check_name
return if !firstname.empty? && !lastname.empty?
if (firstname && !firstname.empty?) && (!lastname || lastname.empty?)
if !firstname.empty? && lastname.empty?
# Lastname, Firstname
# "Lastname, Firstname"
scan = firstname.scan(/, /)
if scan[0]
name = firstname.split(', ', 2)
@ -704,7 +716,7 @@ returns
return
end
# Firstname Lastname
# "Firstname Lastname"
name = firstname.split(' ', 2)
if !name[0].nil?
self.firstname = name[0]
@ -714,8 +726,8 @@ returns
end
return
# -no name- firstname.lastname@example.com
elsif (!firstname || firstname.empty?) && (!lastname || lastname.empty?) && (email && !email.empty?)
# -no name- "firstname.lastname@example.com"
elsif firstname.empty? && lastname.empty? && !email.empty?
scan = email.scan(/^(.+?)\.(.+?)\@.+?$/)
if scan[0]
if !scan[0][0].nil?
@ -729,9 +741,7 @@ returns
end
def check_email
return if !email
self.email = email.downcase
end
@ -779,7 +789,6 @@ returns
end
def avatar_for_email_check
return if !email
return if email.empty?
return if email !~ /@/

View file

@ -0,0 +1,42 @@
# encoding: utf-8
require 'test_helper'
class EmailProcessSenderNameUpdateIfNeeded < ActiveSupport::TestCase
test 'basic' do
email_raw_string = "From: customer@example.com
To: myzammad@example.com
Subject: test sender name update 1
Some Text"
ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
ticket = Ticket.find(ticket_p.id)
article = Ticket::Article.find(article_p.id)
assert_equal('test sender name update 1', ticket.title)
assert_equal('new', ticket.state.name)
assert_equal('Customer', ticket.create_article_sender.name)
assert_equal('Customer', article.sender.name)
assert_equal('customer@example.com', ticket.customer.email)
assert_equal('', ticket.customer.firstname)
assert_equal('', ticket.customer.lastname)
email_raw_string = "From: Max Smith <customer@example.com>
To: myzammad@example.com
Subject: test sender name update 2
Some Text"
ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
ticket = Ticket.find(ticket_p.id)
article = Ticket::Article.find(article_p.id)
assert_equal('test sender name update 2', ticket.title)
assert_equal('new', ticket.state.name)
assert_equal('Customer', ticket.create_article_sender.name)
assert_equal('Customer', article.sender.name)
assert_equal('customer@example.com', ticket.customer.email)
assert_equal('Max', ticket.customer.firstname)
assert_equal('Smith', ticket.customer.lastname)
end
end