mirror of
https://github.com/internetee/registry.git
synced 2025-05-16 17:37:17 +02:00
Domain reservation validation
This commit is contained in:
parent
fb12d056ce
commit
f87c8839be
6 changed files with 36 additions and 6 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
13
app/validators/domain_reservation_validator.rb
Normal file
13
app/validators/domain_reservation_validator.rb
Normal file
|
@ -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
|
3
spec/fabricators/reserved_domain_fabricator.rb
Normal file
3
spec/fabricators/reserved_domain_fabricator.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
Fabricator(:reserved_domain) do
|
||||
name '1162.ee'
|
||||
end
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue