From a3298bb92075706579781897f26e1853aa122272 Mon Sep 17 00:00:00 2001 From: Claudius Nicolae Date: Mon, 22 Apr 2013 01:05:15 +0300 Subject: [PATCH] domain blacklist --- app/models/blacklisted_domain.rb | 8 ++++++++ app/models/domain.rb | 4 +++- config/initializers/rails_admin.rb | 2 +- .../20130421201234_create_blacklisted_domains.rb | 9 +++++++++ spec/factories.rb | 7 +++++++ spec/factories/admins.rb | 6 ------ spec/models/blacklisted_domain_spec.rb | 5 +++++ spec/models/domain_spec.rb | 13 +++++++++++++ spec/support/shared_context/data.rb | 2 ++ 9 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 app/models/blacklisted_domain.rb create mode 100644 db/migrate/20130421201234_create_blacklisted_domains.rb delete mode 100644 spec/factories/admins.rb create mode 100644 spec/models/blacklisted_domain_spec.rb diff --git a/app/models/blacklisted_domain.rb b/app/models/blacklisted_domain.rb new file mode 100644 index 0000000..0397dc8 --- /dev/null +++ b/app/models/blacklisted_domain.rb @@ -0,0 +1,8 @@ +class BlacklistedDomain < ActiveRecord::Base + attr_accessible :name + + def self.include?(name) + where("blacklisted_domains.name = ? OR ? LIKE CONCAT('%.', blacklisted_domains.name)", + name, name).exists? + end +end diff --git a/app/models/domain.rb b/app/models/domain.rb index 11a4d0c..da11fd2 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -32,7 +32,9 @@ class Domain < ActiveRecord::Base validates_associated :"#{type.downcase}_records" end - validates :name, :presence => true, :uniqueness => true, :domainname => {:require_valid_tld => false} + validates :name, :presence => true, :uniqueness => true, + :domainname => {:require_valid_tld => false}, + :exclusion => {:in => BlacklistedDomain} validates :master, :presence => true, :if => :slave? validates :master, :ip => true, :allow_nil => true, :if => :slave? validates :type, :inclusion => { :in => @@types, :message => "Unknown domain type" } diff --git a/config/initializers/rails_admin.rb b/config/initializers/rails_admin.rb index c87ec71..1c0ca41 100644 --- a/config/initializers/rails_admin.rb +++ b/config/initializers/rails_admin.rb @@ -35,7 +35,7 @@ RailsAdmin.config do |config| # Add models here if you want to go 'whitelist mode': config.included_models = [A, AAAA, Admin, CNAME, Domain, MX, NS, Permission, - Record, SOA, SRV, TXT, User, Audit] + Record, SOA, SRV, TXT, User, Audit, BlacklistedDomain] # Application wide tried label methods for models' instances # config.label_methods << :description # Default is [:name, :title] diff --git a/db/migrate/20130421201234_create_blacklisted_domains.rb b/db/migrate/20130421201234_create_blacklisted_domains.rb new file mode 100644 index 0000000..d58ce34 --- /dev/null +++ b/db/migrate/20130421201234_create_blacklisted_domains.rb @@ -0,0 +1,9 @@ +class CreateBlacklistedDomains < ActiveRecord::Migration + def change + create_table :blacklisted_domains do |t| + t.string :name + + t.timestamps + end + end +end diff --git a/spec/factories.rb b/spec/factories.rb index 190cc08..2f24032 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -40,4 +40,11 @@ FactoryGirl.define do factory :permission do end + factory :admin do + end + + factory :blacklisted_domain do + name {FactoryGirl.generate(:domain_name)} + end + end \ No newline at end of file diff --git a/spec/factories/admins.rb b/spec/factories/admins.rb deleted file mode 100644 index e531067..0000000 --- a/spec/factories/admins.rb +++ /dev/null @@ -1,6 +0,0 @@ -# Read about factories at http://github.com/thoughtbot/factory_girl - -FactoryGirl.define do - factory :admin do - end -end diff --git a/spec/models/blacklisted_domain_spec.rb b/spec/models/blacklisted_domain_spec.rb new file mode 100644 index 0000000..520c27d --- /dev/null +++ b/spec/models/blacklisted_domain_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe BlacklistedDomain do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/domain_spec.rb b/spec/models/domain_spec.rb index c9f9cc6..7838606 100644 --- a/spec/models/domain_spec.rb +++ b/spec/models/domain_spec.rb @@ -77,6 +77,19 @@ describe Domain do domain.should have(1).errors_on(:name) end end + + it "validates blacklist" do + blacklisted_domain + + domain.name = blacklisted_domain.name + domain.should have(1).errors_on(:name) + + domain.name = "www.#{blacklisted_domain.name}" + domain.should have(1).errors_on(:name) + + domain.name = "pre#{blacklisted_domain.name}" + domain.should have(0).errors_on(:name) + end it "queries domains corectly in index" do permission3 diff --git a/spec/support/shared_context/data.rb b/spec/support/shared_context/data.rb index 10de6ff..d209655 100644 --- a/spec/support/shared_context/data.rb +++ b/spec/support/shared_context/data.rb @@ -31,5 +31,7 @@ shared_context "data" do let(:permission){create(:permission, :domain => domain, :user => user2)} let(:permission3){create(:permission, :domain => domain3, :user => user)} + + let(:blacklisted_domain){create(:blacklisted_domain)} end