From 5258c43faa86f1dd11baea56eaa5f214afa9f5cd Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Mon, 18 Aug 2014 16:00:34 +0300 Subject: [PATCH] Make epp code map dynamic --- app/models/concerns/epp_errors.rb | 8 +++++-- app/models/contact.rb | 26 ++++++++++++----------- app/models/domain.rb | 35 ++++++++++++++++++++++++------- app/models/nameserver.rb | 14 +++++++++---- config/locales/en.yml | 2 -- 5 files changed, 57 insertions(+), 28 deletions(-) diff --git a/app/models/concerns/epp_errors.rb b/app/models/concerns/epp_errors.rb index e8c56918e..cb7f53dd2 100644 --- a/app/models/concerns/epp_errors.rb +++ b/app/models/concerns/epp_errors.rb @@ -51,8 +51,12 @@ module EppErrors end def find_epp_code(msg) - self.class::EPP_CODE_MAP.each do |code, values| - return code if values.include?(msg) + epp_code_map.each do |code, values| + values.each do |x| + t = errors.generate_message(*x) if x.is_a?(Array) + t = x if x.is_a?(String) + return code if t == msg + end end nil end diff --git a/app/models/contact.rb b/app/models/contact.rb index ecce51574..4c6778183 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -4,12 +4,6 @@ class Contact < ActiveRecord::Base include EppErrors - EPP_CODE_MAP = { - '2302' => ['Contact id already exists'], - '2303' => [:not_found, :epp_obj_does_not_exist], - '2005' => ['Phone nr is invalid', 'Email is invalid'] - } - EPP_ATTR_MAP = {} has_one :address @@ -81,7 +75,7 @@ class Contact < ActiveRecord::Base relation = get_relation(model) return true unless relation.nil? || relation.blank? false - end + end @@ -94,27 +88,35 @@ class Contact < ActiveRecord::Base false end + def epp_code_map + { + '2302' => [[:code, :epp_id_taken]], + '2303' => [:not_found, :epp_obj_does_not_exist], + '2005' => ['Phone nr is invalid', 'Email is invalid'] + } + end + class << self def extract_attributes ph, type=:create - + contact_hash = { phone: ph[:voice], ident: ph[:ident], email: ph[:email] } - + contact_hash = contact_hash.merge({ name: ph[:postalInfo][:name], org_name: ph[:postalInfo][:org] }) if ph[:postalInfo].is_a? Hash - + contact_hash[:code] = ph[:id] if type == :create - + contact_hash.delete_if { |k, v| v.nil? } end - + def check_availability(codes) codes = [codes] if codes.is_a?(String) diff --git a/app/models/domain.rb b/app/models/domain.rb index a3775a244..08f0ff4ce 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -4,14 +4,6 @@ class Domain < ActiveRecord::Base include EppErrors - EPP_CODE_MAP = { - '2302' => ['Domain name already exists', 'Domain name is reserved or restricted'], # Object exists - '2306' => ['Registrant is missing', 'Admin contact is missing', 'Given and current expire dates do not match'], # Parameter policy error - '2004' => ['Nameservers count must be between 1-13', 'Period must add up to 1, 2 or 3 years'], # Parameter value range error - '2303' => ['Registrant not found', 'Contact was not found'], # Object does not exist - '2200' => ['Authentication error'] - } - EPP_ATTR_MAP = { owner_contact: 'registrant', name_dirty: 'name', @@ -159,6 +151,33 @@ class Domain < ActiveRecord::Base }) if cur_exp_date.to_date != valid_to end + def epp_code_map + domain_validation_sg = SettingGroup.find_by(code: SettingGroup::DOMAIN_VALIDATION_CODE) + + { + '2302' => [ # Object exists + [:name_dirty, :taken], + [:name_dirty, :reserved] + ], + '2306' => [ # Parameter policy error + [:owner_contact, :blank], + [:admin_contacts, :blank], + [:valid_to, :epp_exp_dates_do_not_match] + ], + '2004' => [ # Parameter value range error + [:nameservers, :out_of_range, {min: domain_validation_sg.get(:ns_min_count), max: domain_validation_sg.get(:ns_max_count)}], + [:period, :out_of_range] + ], + '2303' => [ # Object does not exist + [:owner_contact, :epp_registrant_not_found], + [:domain_contacts, :not_found] + ], + '2200' => [ + [:auth_info, :wrong_pw] + ] + } + end + ## SHARED # For domain transfer diff --git a/app/models/nameserver.rb b/app/models/nameserver.rb index e033d29b4..e3cf257c8 100644 --- a/app/models/nameserver.rb +++ b/app/models/nameserver.rb @@ -1,10 +1,6 @@ class Nameserver < ActiveRecord::Base include EppErrors - EPP_CODE_MAP = { - '2005' => ['Hostname is invalid', 'IPv4 is invalid', 'IPv6 is invalid'] - } - EPP_ATTR_MAP = { hostname: 'hostName' } @@ -15,4 +11,14 @@ class Nameserver < ActiveRecord::Base validates :hostname, format: { with: /\A(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\z/ } validates :ipv4, format: { with: /\A(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\z/, allow_nil: true } validates :ipv6, format: { with: /(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/, allow_nil: true } + + def epp_code_map + { + '2005' => [ + [:hostname, :invalid], + [:ipv4, :invalid], + [:ipv6, :invalid] + ] + } + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 1fcbfb07b..6ebdc2805 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -50,8 +50,6 @@ en: blank: 'Admin contact is missing' nameservers: out_of_range: 'Nameservers count must be between %{min}-%{max}' - hostname_invalid: 'Hostname is invalid' - ip_invalid: 'IPv4 is invalid' period: out_of_range: 'Period must add up to 1, 2 or 3 years' auth_info: