diff --git a/app/controllers/admin/domains_controller.rb b/app/controllers/admin/domains_controller.rb index f2c562d42..7b6cbe511 100644 --- a/app/controllers/admin/domains_controller.rb +++ b/app/controllers/admin/domains_controller.rb @@ -8,7 +8,7 @@ class Admin::DomainsController < AdminController end def show - @domain.all_dependencies_valid? + @domain.valid? end def edit diff --git a/app/models/domain.rb b/app/models/domain.rb index f372b4bd6..280577f5c 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -53,11 +53,32 @@ class Domain < ActiveRecord::Base validates :owner_contact, :registrar, presence: true validate :validate_period - validate :validate_nameservers_count - validate :validate_admin_contacts_count - validate :validate_dnskeys_count - validates :nameservers, uniqueness_multi: { attribute: 'hostname' } + validates :nameservers, object_count: { + min: -> { Setting.ns_min_count }, + max: -> { Setting.ns_max_count } + } + + validates :dnskeys, object_count: { + min: -> { Setting.dnskeys_min_count }, + max: -> { Setting.dnskeys_max_count } + } + + validates :admin_domain_contacts, object_count: { + association: 'admin_contacts', + min: -> { Setting.admin_contacts_min_count }, + max: -> { Setting.admin_contacts_max_count } + } + + validates :tech_domain_contacts, object_count: { + association: 'tech_contacts', + min: -> { Setting.tech_contacts_min_count }, + max: -> { Setting.tech_contacts_max_count } + } + + validates :nameservers, uniqueness_multi: { + attribute: 'hostname' + } validates :tech_domain_contacts, uniqueness_multi: { association: 'domain_contacts', @@ -69,8 +90,14 @@ class Domain < ActiveRecord::Base attribute: 'contact_code_cache' } - validates :domain_statuses, uniqueness_multi: { attribute: 'value' } - validates :dnskeys, uniqueness_multi: { attribute: 'public_key' } + validates :domain_statuses, uniqueness_multi: { + attribute: 'value' + } + + validates :dnskeys, uniqueness_multi: { + attribute: 'public_key' + } + validate :validate_nameserver_ips attr_accessor :owner_contact_typeahead, :update_me @@ -151,21 +178,6 @@ class Domain < ActiveRecord::Base end ### VALIDATIONS ### - def validate_nameservers_count - min, max = Setting.ns_min_count, Setting.ns_max_count - return if nameservers.reject(&:marked_for_destruction?).length.between?(min, max) - errors.add(:nameservers, :out_of_range, { min: min, max: max }) - end - - def validate_admin_contacts_count - errors.add(:admin_contacts, :out_of_range) if admin_contacts_count.zero? - end - - def validate_dnskeys_count - min, max = Setting.dnskeys_min_count, Setting.dnskeys_max_count - return if dnskeys.reject(&:marked_for_destruction?).length.between?(min, max) - errors.add(:dnskeys, :out_of_range, { min: min, max: max }) - end def validate_nameserver_ips nameservers.each do |ns| @@ -189,14 +201,6 @@ class Domain < ActiveRecord::Base errors.add(:period, :out_of_range) unless valid_values.include?(period.to_s) end - def all_dependencies_valid? - validate_nameservers_count - validate_dnskeys_count - validate_admin_contacts_count - - errors.empty? - end - # used for highlighting form tabs def parent_valid? assoc_errors = errors.keys.select { |x| x.match(/\./) } diff --git a/app/models/epp/epp_domain.rb b/app/models/epp/epp_domain.rb index b5c7e0b61..be8484f04 100644 --- a/app/models/epp/epp_domain.rb +++ b/app/models/epp/epp_domain.rb @@ -2,9 +2,6 @@ class Epp::EppDomain < Domain include EppErrors - validate :validate_nameservers_count - validate :validate_admin_contacts_count - def epp_code_map # rubocop:disable Metrics/MethodLength { '2002' => [ @@ -19,7 +16,6 @@ class Epp::EppDomain < Domain ], '2306' => [ # Parameter policy error [:owner_contact, :blank], - [:admin_contacts, :out_of_range], [:base, :ds_data_with_key_not_allowed], [:base, :ds_data_not_allowed], [:base, :key_data_not_allowed], @@ -27,17 +23,29 @@ class Epp::EppDomain < Domain ], '2004' => [ # Parameter value range error [:nameservers, :out_of_range, - { - min: Setting.ns_min_count, - max: Setting.ns_max_count - } + { + min: Setting.ns_min_count, + max: Setting.ns_max_count + } ], [:period, :out_of_range, { value: { obj: 'period', val: period } }], [:dnskeys, :out_of_range, - { - min: Setting.dnskeys_min_count, - max: Setting.dnskeys_max_count - } + { + min: Setting.dnskeys_min_count, + max: Setting.dnskeys_max_count + } + ], + [:admin_contacts, :out_of_range, + { + min: Setting.admin_contacts_min_count, + max: Setting.admin_contacts_max_count + } + ], + [:tech_contacts, :out_of_range, + { + min: Setting.tech_contacts_min_count, + max: Setting.tech_contacts_max_count + } ] ], '2005' => [ diff --git a/app/validators/object_count_validator.rb b/app/validators/object_count_validator.rb new file mode 100644 index 000000000..b442a360a --- /dev/null +++ b/app/validators/object_count_validator.rb @@ -0,0 +1,8 @@ +class ObjectCountValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + min, max = options[:min].call, options[:max].call + return if value.reject(&:marked_for_destruction?).length.between?(min, max) + association = options[:association] || attribute + record.errors.add(association, :out_of_range, { min: min, max: max }) + end +end diff --git a/config/initializers/initial_settings.rb b/config/initializers/initial_settings.rb index 7a00d673c..39517c12a 100644 --- a/config/initializers/initial_settings.rb +++ b/config/initializers/initial_settings.rb @@ -6,4 +6,21 @@ if ActiveRecord::Base.connection.table_exists? 'settings' # otherwise rake not w Setting.disclosure_phone = false if Setting.disclosure_phone.nil? Setting.disclosure_fax = false if Setting.disclosure_fax.nil? Setting.disclosure_address = false if Setting.disclosure_address.nil? + + Setting.save_default(:admin_contacts_min_count, 1) + Setting.save_default(:admin_contacts_max_count, 10) + Setting.save_default(:tech_contacts_min_count, 1) + Setting.save_default(:tech_contacts_max_count, 10) + + Setting.save_default(:ds_algorithm, 2) + Setting.save_default(:ds_data_allowed, true) + Setting.save_default(:ds_data_with_key_allowed, true) + Setting.save_default(:key_data_allowed, true) + + Setting.save_default(:dnskeys_min_count, 0) + Setting.save_default(:dnskeys_max_count, 9) + Setting.save_default(:ns_min_count, 2) + Setting.save_default(:ns_max_count, 11) + + Setting.save_default(:transfer_wait_time, 0) end diff --git a/config/locales/en.yml b/config/locales/en.yml index 8730c7df8..eda413bef 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -89,9 +89,11 @@ en: invalid: 'Contacts are invalid' not_found: 'Contact was not found' admin_contacts: - out_of_range: 'Admin contacts count must be between 1 - infinity' + out_of_range: 'Admin contacts count must be between %{min}-%{max}' less_than_or_equal_to: 'Admin contacts count must be less than or equal to %{count}' greater_than_or_equal_to: 'Admin contacts count must be greater than or equal to %{count}' + tech_contacts: + out_of_range: 'Tech contacts count must be between %{min}-%{max}' nameservers: invalid: 'Nameservers are invalid' out_of_range: 'Nameservers count must be between %{min}-%{max}' @@ -472,3 +474,7 @@ en: created_at: 'Created at' type: 'Type' legal_documents: 'Legal documents' + admin_contacts_min_count: 'Admin contacts minimum count' + admin_contacts_max_count: 'Admin contacts maximum count' + tech_contacts_min_count: 'Tech contacts minimum count' + tech_contacts_max_count: 'Tech contacts maximum count' diff --git a/spec/epp/domain_spec.rb b/spec/epp/domain_spec.rb index 274c2b921..5e7a9ff01 100644 --- a/spec/epp/domain_spec.rb +++ b/spec/epp/domain_spec.rb @@ -747,8 +747,8 @@ describe 'EPP Domain', epp: true do }) response = epp_request(xml, :xml) - expect(response[:result_code]).to eq('2306') - expect(response[:msg]).to eq('Admin contacts count must be between 1 - infinity') + expect(response[:result_code]).to eq('2004') + expect(response[:msg]).to eq('Admin contacts count must be between 1-10') expect(response[:clTRID]).to eq('ABC-12345') expect(Domain.count).to eq 0 diff --git a/spec/support/general.rb b/spec/support/general.rb index 03b3bbd03..646389000 100644 --- a/spec/support/general.rb +++ b/spec/support/general.rb @@ -11,6 +11,11 @@ module General Setting.ns_max_count = 11 Setting.transfer_wait_time = 0 + + Setting.admin_contacts_min_count = 1 + Setting.admin_contacts_max_count = 10 + Setting.tech_contacts_min_count = 0 + Setting.tech_contacts_max_count = 10 end def create_disclosure_settings