From 51091679c409d2f36043d20d0af7b5a1c5f8d7fc Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Tue, 1 Jul 2014 17:29:21 +0300 Subject: [PATCH] Domain name improvements --- Gemfile | 5 ++++- Gemfile.lock | 2 ++ app/models/domain.rb | 8 +++++++- app/validators/domain_name_validator.rb | 15 ++++++++++++--- .../20140701130945_add_name_dirty_to_domains.rb | 6 ++++++ db/schema.rb | 4 +++- 6 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20140701130945_add_name_dirty_to_domains.rb diff --git a/Gemfile b/Gemfile index 1f7fb3fc5..3c2ce0539 100644 --- a/Gemfile +++ b/Gemfile @@ -31,9 +31,12 @@ gem 'jbuilder', '~> 2.0' # Replacement for erb gem 'haml-rails', '~> 0.5.3' -#For XML parsing +# For XML parsing gem 'nokogiri', '~> 1.6.2.1' +# For punycode +gem 'simpleidn', '~> 0.0.5' + group :assets do # See https://github.com/sstephenson/execjs#readme for more supported runtimes gem 'therubyracer', platforms: :ruby diff --git a/Gemfile.lock b/Gemfile.lock index 5533c3a3b..182f22969 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -141,6 +141,7 @@ GEM rdoc (~> 4.0, < 5.0) shoulda-matchers (2.6.1) activesupport (>= 3.0.0) + simpleidn (0.0.5) slop (3.5.0) spring (1.1.3) sprockets (2.11.0) @@ -195,6 +196,7 @@ DEPENDENCIES sass-rails (~> 4.0.3) sdoc (~> 0.4.0) shoulda-matchers (~> 2.6.1) + simpleidn (~> 0.0.5) spring therubyracer turbolinks diff --git a/app/models/domain.rb b/app/models/domain.rb index 20954f27e..8374a181e 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -1,7 +1,6 @@ class Domain < ActiveRecord::Base #TODO whois requests ip whitelist for full info for own domains and partial info for other domains #TODO most inputs should be trimmed before validatation, probably some global logic? - belongs_to :registrar belongs_to :ns_set @@ -11,6 +10,13 @@ class Domain < ActiveRecord::Base validates :name, domain_name: true + def name=(value) + value.strip! + write_attribute(:name, SimpleIDN.to_unicode(value)) + write_attribute(:name_puny, SimpleIDN.to_ascii(value)) + write_attribute(:name_dirty, value) + end + class << self def check_availability(domains) res = [] diff --git a/app/validators/domain_name_validator.rb b/app/validators/domain_name_validator.rb index e45ea4956..93d87c735 100644 --- a/app/validators/domain_name_validator.rb +++ b/app/validators/domain_name_validator.rb @@ -17,10 +17,19 @@ class DomainNameValidator < ActiveModel::EachValidator value = value.mb_chars.downcase.strip general_domains = /(.pri.ee|.edu.ee|.aip.ee|.org.ee|.med.ee|.riik.ee|.ee)/ #TODO Add more general domains here - unicode_chars = /\u00E4\u00F5\u00F6\u00FC\u0161\u017E/ #äõöüšž - ok = value =~ /\A[a-zA-Z0-9#{unicode_chars}][a-zA-Z0-9#{unicode_chars}-]{0,61}[a-zA-Z0-9#{unicode_chars}]#{general_domains}\z/ - ok &&= !(value[2] == '-' && value[3] == '-') + # it's punycode + if value[2] == '-' && value[3] == '-' + regexp = /\Axn--[a-zA-Z0-9-]{0,61}#{general_domains}\z/ + return false unless value =~ regexp + value = SimpleIDN.to_unicode(value) + end + + unicode_chars = /\u00E4\u00F5\u00F6\u00FC\u0161\u017E/ #äõöüšž + regexp = /\A[a-zA-Z0-9#{unicode_chars}][a-zA-Z0-9#{unicode_chars}-]{0,61}[a-zA-Z0-9#{unicode_chars}]#{general_domains}\z/ + + value =~ regexp end + end end diff --git a/db/migrate/20140701130945_add_name_dirty_to_domains.rb b/db/migrate/20140701130945_add_name_dirty_to_domains.rb new file mode 100644 index 000000000..903f9aa51 --- /dev/null +++ b/db/migrate/20140701130945_add_name_dirty_to_domains.rb @@ -0,0 +1,6 @@ +class AddNameDirtyToDomains < ActiveRecord::Migration + def change + add_column :domains, :name_dirty, :string + add_column :domains, :name_puny, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 50d6413df..8eae54fe0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140627082711) do +ActiveRecord::Schema.define(version: 20140701130945) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -59,6 +59,8 @@ ActiveRecord::Schema.define(version: 20140627082711) do t.string "auth_info" t.datetime "created_at" t.datetime "updated_at" + t.string "name_dirty" + t.string "name_puny" end create_table "epp_sessions", force: true do |t|