Browse Source

domain blacklist

pull/1/head
Claudius Nicolae 12 years ago
parent
commit
a3298bb920
  1. 8
      app/models/blacklisted_domain.rb
  2. 4
      app/models/domain.rb
  3. 2
      config/initializers/rails_admin.rb
  4. 9
      db/migrate/20130421201234_create_blacklisted_domains.rb
  5. 7
      spec/factories.rb
  6. 6
      spec/factories/admins.rb
  7. 5
      spec/models/blacklisted_domain_spec.rb
  8. 13
      spec/models/domain_spec.rb
  9. 2
      spec/support/shared_context/data.rb

8
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

4
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" }

2
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]

9
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

7
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

6
spec/factories/admins.rb

@ -1,6 +0,0 @@
# Read about factories at http://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :admin do
end
end

5
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

13
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

2
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

Loading…
Cancel
Save