You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.8 KiB

13 years ago
class Domain < ActiveRecord::Base
12 years ago
self.inheritance_column = :sti_disabled
13 years ago
nilify_blanks
stampable
# optional IP for create form, results in a type A record
attr_accessor :ip
13 years ago
attr_accessor :domain_ownership_failed
13 years ago
# attr_accessible :name, :ip, :soa_record, :ns_records, :apply_subdomains
12 years ago
13 years ago
belongs_to :user, :inverse_of => :domain
has_many :records, :inverse_of => :domain, :dependent => :destroy
has_many :permissions, :inverse_of => :domain, :dependent => :destroy
has_many :permitted_users, :through => :permissions, :source => :user
13 years ago
cattr_reader :types
@@types = ['NATIVE', 'MASTER', 'SLAVE', 'SUPERSLAVE']
has_one :soa_record,
-> { where(type: 'SOA') },
13 years ago
:class_name => 'SOA',
:inverse_of => :domain
13 years ago
Record.types.each do |type|
has_many :"#{type.downcase}_records",
13 years ago
:class_name => type,
:inverse_of => :domain
13 years ago
validates_associated :"#{type.downcase}_records"
end
12 years ago
validates :name, :presence => true, :uniqueness => true,
:domainname => {:require_valid_tld => false},
:exclusion => {:in => BlacklistedDomain}
13 years ago
validates :master, :presence => true, :if => :slave?
13 years ago
validates :master, :ip => true, :allow_nil => true, :if => :slave?
13 years ago
validates :type, :inclusion => { :in => @@types, :message => "Unknown domain type" }
validates :soa_record, :presence => {:unless => :slave?}
validates_associated :soa_record #, :allow_nil => true
validates :ns_records, :on => :create, :presence => true, :length => {
:minimum => 2, :maximum => 10, :message => "must have be at least 2, at most 10"}
13 years ago
validates_associated :records
13 years ago
validates :user_id, :presence => true
13 years ago
validate :max_domains_per_user, :on => :create
delegate :domains_exceeding?, :to => :user
def max_domains_per_user
if domains_exceeding?
errors.add :base, <<-DOC
as a security measure, you cannot have more than
#{Settings.max_domains_per_user} domains on one account
DOC
13 years ago
end
end
13 years ago
before_create do
a_records.build(:content => ip) if ip.present?
end
concerned_with :tree_structure, :name_change_subdomains, :name_change_records
scope :host_domains, -> { where(:name => Settings.host_domains) }
def host_domain?
Settings.host_domains.include?(name)
end
# domains per user limit for DOS protection
def records_exceeding?
records.count >= Settings.max_records_per_domain.to_i
end
13 years ago
# domain.has_ns?('129.168.0.1')
def has_ns?(ns)
ns_records.any? {|ns_record| ns_record.content == ns}
end
def slave?; self.type == 'SLAVE' end
13 years ago
def setup(email)
13 years ago
build_soa_record
13 years ago
soa = soa_record
soa.contact ||= email
13 years ago
ns_records.build
ns_records.build
ns_records.build
ns1, ns2, ns3 = ns_records
13 years ago
ns1.content = Settings.ns.first
ns2.content = Settings.ns.second
ns3.content = Settings.ns.third
13 years ago
end
13 years ago
end