From 223aaf2e09ae776c0076d8e50eeba00a6802d1c9 Mon Sep 17 00:00:00 2001 From: Nicolae Claudius Date: Tue, 18 Oct 2011 12:27:42 -0700 Subject: [PATCH] hosts --- Gemfile.lock | 8 +- app/controllers/application_controller.rb | 4 + app/controllers/hosts_controller.rb | 32 ++++ app/helpers/hosts_helper.rb | 2 + app/models/a.rb | 9 + app/models/ability.rb | 27 ++- app/models/domain.rb | 5 +- app/models/record.rb | 1 + app/views/fragments/_top.html.erb | 13 +- config/routes.rb | 4 + config/settings.sample.yml | 2 + .../20111018100348_add_user_id_to_records.rb | 5 + db/seeds.rb | 16 ++ spec/controllers/hosts_controller_spec.rb | 157 ++++++++++++++++++ spec/helpers/hosts_helper_spec.rb | 15 ++ spec/requests/hosts_spec.rb | 11 ++ spec/routing/hosts_routing_spec.rb | 35 ++++ spec/views/hosts/edit.html.erb_spec.rb | 15 ++ spec/views/hosts/index.html.erb_spec.rb | 14 ++ spec/views/hosts/new.html.erb_spec.rb | 15 ++ spec/views/hosts/show.html.erb_spec.rb | 11 ++ 21 files changed, 376 insertions(+), 25 deletions(-) create mode 100644 app/controllers/hosts_controller.rb create mode 100644 app/helpers/hosts_helper.rb create mode 100644 db/migrate/20111018100348_add_user_id_to_records.rb create mode 100644 spec/controllers/hosts_controller_spec.rb create mode 100644 spec/helpers/hosts_helper_spec.rb create mode 100644 spec/requests/hosts_spec.rb create mode 100644 spec/routing/hosts_routing_spec.rb create mode 100644 spec/views/hosts/edit.html.erb_spec.rb create mode 100644 spec/views/hosts/index.html.erb_spec.rb create mode 100644 spec/views/hosts/new.html.erb_spec.rb create mode 100644 spec/views/hosts/show.html.erb_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index e0e4b68..075f929 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -94,7 +94,7 @@ GEM i18n (~> 0.4) ffi (1.0.9) fssm (0.2.7) - guard (0.8.5) + guard (0.8.6) thor (~> 0.14.6) guard-rspec (0.4.5) guard (>= 0.4.0) @@ -133,7 +133,7 @@ GEM pjax_rails (0.1.10) jquery-rails polyglot (0.3.2) - rack (1.3.4) + rack (1.3.5) rack-cache (1.1) rack (>= 0.4) rack-mount (0.8.3) @@ -161,7 +161,7 @@ GEM thor (~> 0.14.6) rake (0.9.2) rb-fsevent (0.4.3.1) - rdoc (3.10) + rdoc (3.11) json (~> 1.4) rspec (2.6.0) rspec-core (~> 2.6.0) @@ -194,7 +194,7 @@ GEM sprockets (2.0.3) hike (~> 1.2) rack (~> 1.0) - tilt (~> 1.1, != 1.3.0) + tilt (!= 1.3.0, ~> 1.1) therubyracer (0.9.8) libv8 (~> 3.3.10) thor (0.14.6) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index cbf56fd..a574705 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -29,4 +29,8 @@ class ApplicationController < ActionController::Base end helper_method :client_remote_ip + def current_ability + @current_ability ||= ::Ability.new(:user => current_user) + end + end diff --git a/app/controllers/hosts_controller.rb b/app/controllers/hosts_controller.rb new file mode 100644 index 0000000..376314c --- /dev/null +++ b/app/controllers/hosts_controller.rb @@ -0,0 +1,32 @@ +class HostsController < ApplicationController + active_scaffold :a do |conf| + conf.columns = [:name, :host_domain, :content, :ttl, :change_date, :authentication_token] + conf.list.columns = [:name, :content, :ttl, :change_date, :authentication_token] + conf.create.columns = [:name, :host_domain, :content, :ttl,] + conf.update.columns = [:name, :host_domain, :content, :ttl] + conf.list.label = 'Hosts' + conf.create.link.label = "Add Host" + conf.columns[:host_domain].form_ui = :select + conf.columns[:host_domain].options = {:options => Settings.host_domains} + conf.columns[:name].label = 'Host' + conf.columns[:content].label = 'IP' + conf.columns[:content].description = 'Ex. "10.10.5.12"' + conf.columns[:change_date].list_ui = :timestamp + conf.columns[:ttl].options = {:i18n_number => {:delimiter => ''}} + conf.actions.exclude :show + end + + protected + + def new_model + record = super + record.content = client_remote_ip + before_create_save(record) + record + end + + def before_create_save(record) + record.user = current_user + end + +end diff --git a/app/helpers/hosts_helper.rb b/app/helpers/hosts_helper.rb new file mode 100644 index 0000000..3349e1d --- /dev/null +++ b/app/helpers/hosts_helper.rb @@ -0,0 +1,2 @@ +module HostsHelper +end \ No newline at end of file diff --git a/app/models/a.rb b/app/models/a.rb index 323aaf4..39be01f 100644 --- a/app/models/a.rb +++ b/app/models/a.rb @@ -14,4 +14,13 @@ class A < Record validates :name, :hostname => {:allow_underscore => true, :allow_wildcard_hostname => true} validates :content, :presence => true, :ip => {:ip_type => :v4} # Only accept valid IPv4 addresses + attr_accessor :host_domain + validates :host_domain, :inclusion => {:in => Settings.host_domains} + + before_validation do + if host_domain.present? && Settings.host_domains.include?(host_domain) + self.domain_id = Domain.find_by_name(host_domain).try(:id) + end + end + end diff --git a/app/models/ability.rb b/app/models/ability.rb index 31d39c2..1d15d8a 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -1,27 +1,24 @@ class Ability include CanCan::Ability + attr_accessor :user + attr_accessor :context - def initialize(user) - - user ||= User.new + def initialize(options) + @user = options[:user] || User.new + @context = options[:context] || :application + if user.persisted? + + # can manage his domains and records can :manage, Domain, :user_id => user.id can :manage, Record, :domain => {:user_id => user.id} cannot :delete, SOA # it's deleted with the parent domain + + # can manage his hosts + can :manage, A, :user_id => user.id #, :domain => {:name => Settings.host_domains} + end - # The first argument to `can` is the action you are giving the user permission to do. - # If you pass :manage it will apply to every action. Other common actions here are - # :read, :create, :update and :destroy. - # - # The second argument is the resource the user can perform the action on. If you pass - # :all it will apply to every resource. Otherwise pass a Ruby class of the resource. - # - # The third argument is an optional hash of conditions to further filter the objects. - # For example, here the user can only update published articles. - # - # can :update, Article, :published => true - # # See the wiki for details: https://github.com/ryanb/cancan/wiki/Defining-Abilities end end diff --git a/app/models/domain.rb b/app/models/domain.rb index a1c8089..7d391e6 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -5,8 +5,8 @@ class Domain < ActiveRecord::Base # optional IP for create form, results in a type A record attr_accessor :ip - belongs_to :user - has_many :records, :dependent => :destroy, :inverse_of => :domain + belongs_to :user, :inverse_of => :domain + has_many :records, :inverse_of => :domain, :dependent => :destroy cattr_reader :types @@types = ['NATIVE', 'MASTER', 'SLAVE', 'SUPERSLAVE'] @@ -85,6 +85,7 @@ class Domain < ActiveRecord::Base end end + scope :host_domains, where(:name => Settings.host_domains) def setup(email) build_soa_record diff --git a/app/models/record.rb b/app/models/record.rb index f21e053..ebe202e 100644 --- a/app/models/record.rb +++ b/app/models/record.rb @@ -1,5 +1,6 @@ class Record < ActiveRecord::Base belongs_to :domain, :inverse_of => :records + belongs_to :user, :inverse_of => :records cattr_reader :types @@types = %w(SOA NS A MX TXT CNAME) diff --git a/app/views/fragments/_top.html.erb b/app/views/fragments/_top.html.erb index 2b5532a..c7fabdd 100644 --- a/app/views/fragments/_top.html.erb +++ b/app/views/fragments/_top.html.erb @@ -6,18 +6,23 @@ <% if user_signed_in? %>
  • <%#= link_to('Dashboard', dashboard_path, :data => {:pjax => '#main'}) %>
  • <%= link_to 'Manage Domains', domains_path, :data => {:pjax => '#main'} %>
  • +
  • <%= link_to 'Manage Hosts', hosts_path, :data => {:pjax => '#main'} %>
  • <% else %>
  • <%= link_to('Home', root_path, :data => {:pjax => '#main'}) %>
  • +
  • <%= link_to('About', page_path('about'), :data => {:pjax => '#main'}) %>
  • +
  • <%= link_to('Team', page_path('team'), :data => {:pjax => '#main'}) %>
  • +
  • <%= link_to('Contact', page_path('contact'), :data => {:pjax => '#main'}) %>
  • +
  • <%= link_to('Help & Support', page_path('help'), :data => {:pjax => '#main'}) %>
  • <% end %> -
  • <%= link_to('About', page_path('about'), :data => {:pjax => '#main'}) %>
  • -
  • <%= link_to('Team', page_path('team'), :data => {:pjax => '#main'}) %>
  • -
  • <%= link_to('Contact', page_path('contact'), :data => {:pjax => '#main'}) %>
  • -
  • <%= link_to('Help & Support', page_path('help'), :data => {:pjax => '#main'}) %>