Class: Domain

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Authorization, Taxonomix
Defined in:
app/models/domain.rb

Overview

This models a DNS domain and so represents a site.

Defined Under Namespace

Classes: Jail

Instance Method Summary collapse

Methods included from Taxonomix

included

Methods included from Authorization

#enforce_create_permissions, #enforce_destroy_permissions, #enforce_edit_permissions, included, #permission_failed?

Instance Method Details

#as_json(options = {}) ⇒ Object


43
44
45
46
# File 'app/models/domain.rb', line 43

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

#enforce_permissions(operation) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/domain.rb', line 48

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 constrained
    if current.domains.empty? or current.domains.map(&:id).include? self.id
      return true
    end
  end

  errors.add(:base, _("You do not have permission to %s this domain") % operation)
  false
end

#lookup(query) ⇒ Object


86
87
88
# File 'app/models/domain.rb', line 86

def lookup query
  Net::DNS.lookup query, proxy, resolver
end

#nameserversObject

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


69
70
71
72
73
74
75
# File 'app/models/domain.rb', line 69

def nameservers
  return [] if Setting.query_local_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

#proxyObject


82
83
84
# File 'app/models/domain.rb', line 82

def proxy
  ProxyAPI::DNS.new(:url => dns.url) if dns and !dns.url.blank?
end

#resolverObject


77
78
79
80
# File 'app/models/domain.rb', line 77

def resolver
  ns = nameservers
  Resolv::DNS.new ns.empty? ? nil : {:search => name, :nameserver => ns, :ndots => 1}
end

#to_paramObject


39
40
41
# File 'app/models/domain.rb', line 39

def to_param
  name
end