Project

General

Profile

Download (1.89 KB) Statistics
| Branch: | Tag: | Revision:
90b83222 Ohad Levy
require "resolv"
950ddeb3 Ohad Levy
# This models a DNS domain and so represents a site.
class Domain < ActiveRecord::Base
9fd7478e Paul Kelly
include Authorization
950ddeb3 Ohad Levy
has_many :hosts
6e50fa1d Ohad Levy
has_many :subnets
06823dc7 Ohad Levy
belongs_to :dns, :class_name => "SmartProxy"
aa1796f3 Paul Kelly
has_many :domain_parameters, :dependent => :destroy, :foreign_key => :reference_id
9fd7478e Paul Kelly
has_and_belongs_to_many :users, :join_table => "user_domains"

c2ba4ed3 Ohad Levy
accepts_nested_attributes_for :domain_parameters, :reject_if => lambda { |a| a[:value].blank? }, :allow_destroy => true
8b567d39 Ohad Levy
validates_uniqueness_of :name
validates_uniqueness_of :fullname, :allow_blank => true, :allow_nil => true
9eb0e160 Ohad Levy
validates_presence_of :name
950ddeb3 Ohad Levy
f111e0da José Luis Escalante
before_destroy Ensure_not_used_by.new(:hosts, :subnets)
5410421d Ohad Levy
default_scope :order => 'LOWER(domains.name)'
2b4e3e7d Ohad Levy
def to_param
name
end

def as_json(options={})
super({:only => [:name, :id]}.merge(options))
950ddeb3 Ohad Levy
end

9fd7478e Paul Kelly
def enforce_permissions operation
# We get called again with the operation being set to create
return true if operation == "edit" and new_record?

current = User.current

if current.allowed_to?("#{operation}_domains".to_sym)
# If you can create domains then you can create them anywhere
return true if operation == "create"
# However if you are editing or destroying and you have a domain list then you are contrained
if current.domains.empty? or current.domains.map(&:id).include? self.id
return true
end
end

errors.add_to_base "You do not have permission to #{operation} this domain"
false
end

90b83222 Ohad Levy
# return the primary name server for our domain based on DNS lookup
# it first searches for SOA record, if it failed it will search for NS records
def nameservers
dns = Resolv::DNS.new
ns = dns.getresources(name, Resolv::DNS::Resource::IN::SOA).collect {|r| r.mname.to_s}
ns = dns.getresources(name, Resolv::DNS::Resource::IN::NS).collect {|r| r.name.to_s} if ns.empty?
ns.to_a.flatten
end

950ddeb3 Ohad Levy
end