Project

General

Profile

Feature #29208 » support-idn-email-addresses-v2.diff

Go MAEDA, 2018-07-07 09:27

View differences:

Gemfile (working copy)
14 14
gem "mimemagic"
15 15
gem "mail", "~> 2.6.4"
16 16
gem "csv", "~> 1.0.2" if RUBY_VERSION >= "2.3"
17
gem "simpleidn"
17 18

  
18 19
gem "nokogiri", "~> 1.8.0"
19 20
gem "i18n", "~> 0.7.0"
app/models/email_address.rb (working copy)
15 15
# along with this program; if not, write to the Free Software
16 16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 17

  
18
require 'simpleidn'
19

  
18 20
class EmailAddress < ActiveRecord::Base
19 21
  include Redmine::SafeAttributes
20 22

  
......
25 27
  after_destroy :destroy_tokens, :deliver_security_notification_destroy
26 28

  
27 29
  validates_presence_of :address
28
  validates_format_of :address, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, :allow_blank => true
30
  validates_format_of :address, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+(xn--)?[a-z0-9]{2,})\z/i, :allow_blank => true
29 31
  validates_length_of :address, :maximum => User::MAIL_LENGTH_LIMIT, :allow_nil => true
30 32
  validates_uniqueness_of :address, :case_sensitive => false,
31 33
    :if => Proc.new {|email| email.address_changed? && email.address.present?}
......
33 35
  safe_attributes 'address'
34 36

  
35 37
  def address=(arg)
36
    write_attribute(:address, arg.to_s.strip)
38
    local_part, at, domain = arg.to_s.strip.rpartition('@')
39
    ascii_address = at.empty? ? domain : local_part + at + SimpleIDN.to_ascii(domain)
40
    write_attribute(:address, ascii_address)
37 41
  end
38 42

  
39 43
  def destroy
test/unit/user_test.rb (working copy)
84 84
    assert_equal "foo@bar.com", u.mail
85 85
  end
86 86

  
87
  def test_mail_should_accept_idn
88
    u = User.new
89
    u.mail = "maeda@испытание.test.テスト"
90
    assert_equal "maeda@xn--80akhbyknj4f.test.xn--zckzah", u.mail
91
  end
92

  
87 93
  def test_should_create_email_address
88 94
    u = User.new(:firstname => "new", :lastname => "user")
89 95
    u.login = "create_email_address"
(4-4/9)