From f87c8839beba7b9f36ebc4f4ac2177bbd15da950 Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Wed, 30 Jul 2014 12:13:31 +0300 Subject: [PATCH] Domain reservation validation --- app/helpers/epp/domains_helper.rb | 2 +- app/models/domain.rb | 10 +++++++--- app/validators/domain_name_validator.rb | 9 +++++++-- app/validators/domain_reservation_validator.rb | 13 +++++++++++++ spec/fabricators/reserved_domain_fabricator.rb | 3 +++ spec/models/domain_spec.rb | 5 +++++ 6 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 app/validators/domain_reservation_validator.rb create mode 100644 spec/fabricators/reserved_domain_fabricator.rb diff --git a/app/helpers/epp/domains_helper.rb b/app/helpers/epp/domains_helper.rb index d5dd1b5b4..968a902b4 100644 --- a/app/helpers/epp/domains_helper.rb +++ b/app/helpers/epp/domains_helper.rb @@ -5,7 +5,7 @@ module Epp::DomainsHelper if domain.save render '/epp/domains/create' else - if domain.errors.added?(:name_dirty, :taken) + if domain.errors.added?(:name, :taken) @code = '2302' @msg = 'Domain name already exists' end diff --git a/app/models/domain.rb b/app/models/domain.rb index 7eb97a189..8c5503454 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -8,9 +8,8 @@ class Domain < ActiveRecord::Base belongs_to :technical_contact, class_name: 'Contact' belongs_to :admin_contact, class_name: 'Contact' - validates :name, domain_name: true + validates :name, domain_name: true, uniqueness: true validates :name_puny, domain_name: true - validates :name_dirty, uniqueness: true def name=(value) value.strip! @@ -25,11 +24,16 @@ class Domain < ActiveRecord::Base res = [] domains.each do |x| - if !DomainNameValidator.validate(x) + if !DomainNameValidator.validate_format(x) res << {name: x, avail: 0, reason: 'invalid format'} next end + if !DomainNameValidator.validate_reservation(x) + res << {name: x, avail: 0, reason: 'Domain name is reserved or restricted'} + next + end + if Domain.find_by(name: x) res << {name: x, avail: 0, reason: 'in use'} #confirm reason with current API else diff --git a/app/validators/domain_name_validator.rb b/app/validators/domain_name_validator.rb index e32b95c1b..291e23bc3 100644 --- a/app/validators/domain_name_validator.rb +++ b/app/validators/domain_name_validator.rb @@ -7,13 +7,15 @@ class DomainNameValidator < ActiveModel::EachValidator # lower level domains are fixed for .ee and can add statically into settings def validate_each(record, attribute, value) - unless self.class.validate(value) + if !self.class.validate_format(value) record.errors[attribute] << (options[:message] || 'invalid format') + elsif !self.class.validate_reservation(value) + record.errors[attribute] << (options[:message] || 'Domain name is reserved or restricted') end end class << self - def validate(value) + def validate_format(value) value = value.mb_chars.downcase.strip general_domains = /(.pri.ee|.com.ee|.fie.ee|.med.ee|.ee)/ @@ -31,5 +33,8 @@ class DomainNameValidator < ActiveModel::EachValidator !!(value =~ regexp) end + def validate_reservation(value) + !ReservedDomain.exists?(name: value.mb_chars.downcase.strip) + end end end diff --git a/app/validators/domain_reservation_validator.rb b/app/validators/domain_reservation_validator.rb new file mode 100644 index 000000000..ed596124a --- /dev/null +++ b/app/validators/domain_reservation_validator.rb @@ -0,0 +1,13 @@ +class DomainReservationValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + unless self.class.validate(value) + record.errors[attribute] << (options[:message] || 'Domain name is reserved or restricted') + end + end + + class << self + def validate(value) + !ReservedDomain.exists?(name: value.mb_chars.downcase.strip) + end + end +end diff --git a/spec/fabricators/reserved_domain_fabricator.rb b/spec/fabricators/reserved_domain_fabricator.rb new file mode 100644 index 000000000..f14948902 --- /dev/null +++ b/spec/fabricators/reserved_domain_fabricator.rb @@ -0,0 +1,3 @@ +Fabricator(:reserved_domain) do + name '1162.ee' +end diff --git a/spec/models/domain_spec.rb b/spec/models/domain_spec.rb index 08c077073..f24f68d0b 100644 --- a/spec/models/domain_spec.rb +++ b/spec/models/domain_spec.rb @@ -35,4 +35,9 @@ describe Domain do expect(Fabricate.build(:domain, name: x).valid?).to be true end end + + it 'does not create a reserved domain' do + Fabricate(:reserved_domain) + expect(Fabricate.build(:domain, name: '1162.ee').valid?).to be false + end end