]> source.dussan.org Git - redmine.git/commitdiff
Add support for IDN (internationalized domain name) email addresses in user accounts...
authorGo MAEDA <maeda@farend.jp>
Thu, 1 Feb 2024 08:46:41 +0000 (08:46 +0000)
committerGo MAEDA <maeda@farend.jp>
Thu, 1 Feb 2024 08:46:41 +0000 (08:46 +0000)
Patch by Go MAEDA (@maeda).

git-svn-id: https://svn.redmine.org/redmine/trunk@22667 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/email_address.rb
test/unit/email_address_test.rb

index a62ac9cf9566e77c179d70b7363cea4d5d2bc60f..9f06c3b62e646b5b2daff7547457ef625d47aef7 100644 (file)
@@ -39,7 +39,17 @@ class EmailAddress < ApplicationRecord
   safe_attributes 'address'
 
   def address=(arg)
-    write_attribute(:address, arg.to_s.strip)
+    normalized_address = arg.to_s.strip
+
+    # Convert internationalized domain name (IDN) to Punycode
+    # e.g. 'marie@société.example' => 'marie@xn--socit-esab.example'
+    local_part, _at, domain = normalized_address.partition('@')
+    if domain.present?
+      ascii_domain = Addressable::IDNA.to_ascii(domain)
+      normalized_address = "#{local_part}@#{ascii_domain}"
+    end
+
+    write_attribute(:address, normalized_address)
   end
 
   def destroy
index 6fa1d78829fde5b9a7b06e5f590e738b4e640b89..03d249403dc35ae99c5fdf8aa3e777063b289e3a 100644 (file)
@@ -68,4 +68,10 @@ class EmailAddressTest < ActiveSupport::TestCase
   def test_should_reject_invalid_email
     assert_not EmailAddress.new(address: 'invalid,email@example.com').valid?
   end
+
+  def test_should_normalize_idn_email_address
+    email = EmailAddress.new(address: 'marie@société.example')
+    assert_equal 'marie@xn--socit-esab.example', email.address
+    assert email.valid?
+  end
 end