mirror of
https://github.com/internetee/registry.git
synced 2025-07-31 23:16:23 +02:00
113 lines
3.1 KiB
Ruby
113 lines
3.1 KiB
Ruby
class Domain < ActiveRecord::Base
|
|
#TODO whois requests ip whitelist for full info for own domains and partial info for other domains
|
|
#TODO most inputs should be trimmed before validatation, probably some global logic?
|
|
|
|
belongs_to :registrar
|
|
belongs_to :owner_contact, class_name: 'Contact'
|
|
|
|
has_many :domain_contacts
|
|
|
|
has_many :tech_contacts, -> {
|
|
where(domain_contacts: {contact_type: Contact::CONTACT_TYPE_TECH})
|
|
}, through: :domain_contacts, source: :contact
|
|
|
|
has_many :admin_contacts, -> {
|
|
where(domain_contacts: {contact_type: Contact::CONTACT_TYPE_ADMIN})
|
|
}, through: :domain_contacts, source: :contact
|
|
|
|
has_and_belongs_to_many :nameservers
|
|
|
|
validates :name_dirty, domain_name: true, uniqueness: { message: :epp_domain_taken }
|
|
validates :period, numericality: { only_integer: true, greater_than: 0, less_than: 100 }
|
|
validates :name, :owner_contact, presence: true
|
|
|
|
def name=(value)
|
|
value.strip!
|
|
write_attribute(:name, SimpleIDN.to_unicode(value))
|
|
write_attribute(:name_puny, SimpleIDN.to_ascii(value))
|
|
write_attribute(:name_dirty, value)
|
|
end
|
|
|
|
def attach_contacts(contacts)
|
|
contacts.each do |k, v|
|
|
v.each do |x|
|
|
contact = Contact.find_by(code: x[:contact])
|
|
attach_contact(k, contact) and next if contact
|
|
|
|
# Detailed error message with value to display in EPP response
|
|
errors.add(:domain_contacts, {
|
|
obj: 'contact',
|
|
val: x[:contact],
|
|
msg: I18n.t('errors.messages.epp_contact_not_found')
|
|
})
|
|
end
|
|
end
|
|
|
|
attach_contact(Contact::CONTACT_TYPE_TECH, owner_contact) if tech_contacts.empty?
|
|
|
|
if owner_contact.citizen?
|
|
attach_contact(Contact::CONTACT_TYPE_ADMIN, owner_contact) if admin_contacts.empty?
|
|
end
|
|
|
|
validate_admin_contacts_count
|
|
|
|
errors.empty?
|
|
end
|
|
|
|
def attach_contact(type, contact)
|
|
domain_contacts.create(
|
|
contact: contact,
|
|
contact_type: type
|
|
)
|
|
end
|
|
|
|
def attach_nameservers(nameservers)
|
|
nameservers.each do |x|
|
|
self.nameservers << Nameserver.find_or_create_by(hostname: x)
|
|
end
|
|
save!
|
|
|
|
validate_nameservers_count
|
|
|
|
errors.empty?
|
|
end
|
|
|
|
def validate_nameservers_count
|
|
errors.add(:nameservers, :blank) if nameservers.empty?
|
|
|
|
if nameservers.count <= 1 || nameservers.count > 13
|
|
errors.add(:nameservers, I18n.t('errors.messages.epp_nameservers_range_fail', min: 1, max: 13))
|
|
end
|
|
end
|
|
|
|
def validate_admin_contacts_count
|
|
errors.add(:admin_contacts, :blank) if admin_contacts.empty?
|
|
end
|
|
|
|
class << self
|
|
def check_availability(domains)
|
|
domains = [domains] if domains.is_a?(String)
|
|
|
|
res = []
|
|
domains.each do |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: I18n.t('errors.messages.epp_domain_reserved')}
|
|
next
|
|
end
|
|
|
|
if Domain.find_by(name: x)
|
|
res << {name: x, avail: 0, reason: 'in use'} #confirm reason with current API
|
|
else
|
|
res << {name: x, avail: 1}
|
|
end
|
|
end
|
|
|
|
res
|
|
end
|
|
end
|
|
end
|