mirror of
https://github.com/internetee/registry.git
synced 2025-05-17 17:59:47 +02:00
40 lines
1.5 KiB
Ruby
40 lines
1.5 KiB
Ruby
class DomainNameValidator < ActiveModel::EachValidator
|
|
def validate_each(record, attribute, value)
|
|
if !self.class.validate_format(value)
|
|
record.errors[attribute] << (options[:message] || record.errors.generate_message(attribute, :invalid))
|
|
elsif !self.class.validate_blocked(value)
|
|
record.errors.add(:base, :domain_name_blocked)
|
|
end
|
|
end
|
|
|
|
class << self
|
|
def validate_format(value)
|
|
return true if value == 'ee'
|
|
return true unless value
|
|
value = value.mb_chars.downcase.strip
|
|
|
|
general_domains = /(.pri.ee|.com.ee|.fie.ee|.med.ee|.ee)/
|
|
|
|
# it's punycode
|
|
if value[2] == '-' && value[3] == '-'
|
|
regexp = /\Axn--[a-zA-Z0-9-]{0,59}#{general_domains}\z/
|
|
return false unless value =~ regexp
|
|
value = SimpleIDN.to_unicode(value).mb_chars.downcase.strip
|
|
end
|
|
|
|
# rubocop: disable Metrics/LineLength
|
|
unicode_chars = /\u00E4\u00F5\u00F6\u00FC\u0161\u017E/ # äõöüšž
|
|
regexp = /\A[a-zA-Z0-9#{unicode_chars.source}][a-zA-Z0-9#{unicode_chars.source}-]{0,61}[a-zA-Z0-9#{unicode_chars.source}]#{general_domains.source}\z/
|
|
# rubocop: enable Metrics/LineLength
|
|
# rubocop: disable Style/DoubleNegation
|
|
!!(value =~ regexp)
|
|
# rubocop: enable Style/DoubleNegation
|
|
end
|
|
|
|
def validate_blocked(value)
|
|
return true unless value
|
|
return false if BlockedDomain.where("names @> ?::varchar[]", "{#{value}}").count > 0
|
|
ZonefileSetting.where(origin: value).count == 0
|
|
end
|
|
end
|
|
end
|