Merged branch 111297422-whois_blocked_domains into staging

This commit is contained in:
Georg Kahest 2016-01-21 12:13:58 +02:00
commit 6814a7569f
7 changed files with 460 additions and 139 deletions

View file

@ -2,20 +2,44 @@ class Admin::BlockedDomainsController < AdminController
load_and_authorize_resource
def index
bd = BlockedDomain.first_or_initialize
@blocked_domains = bd.names.join("\n")
bd = BlockedDomain.pluck(:name)
if bd
@blocked_domains = bd.to_yaml.gsub("---\n", '').gsub("-", '').gsub(" ", '')
end
end
def create
names = params[:blocked_domains].split("\r\n").map(&:strip)
@blocked_domains = params[:blocked_domains]
bd = BlockedDomain.first_or_create
begin
params[:blocked_domains] = "---\n" if params[:blocked_domains].blank?
names = YAML.load(params[:blocked_domains])
fail if names == false
rescue
flash.now[:alert] = I18n.t('invalid_yaml')
logger.warn 'Invalid YAML'
render :index and return
end
if bd.update(names: names)
names = names.split(' ')
result = true
BlockedDomain.transaction do
existing = BlockedDomain.any_of_domains(names).pluck(:id)
BlockedDomain.where.not(id: existing).destroy_all
names.each do |name|
rec = BlockedDomain.find_or_initialize_by(name: name)
unless rec.save
result = false
raise ActiveRecord::Rollback
end
end
end
if result
flash[:notice] = I18n.t('record_updated')
redirect_to :back
else
@blocked_domains = params[:blocked_domains]
flash.now[:alert] = I18n.t('failed_to_update_record')
render :index
end

View file

@ -1,5 +1,49 @@
class BlockedDomain < ActiveRecord::Base
include Versions
before_save :generate_data
before_destroy :remove_data
after_initialize -> { self.names = [] if names.nil? }
class << self
def by_domain name
where(name: name)
end
def any_of_domains names
where(name: names)
end
end
def name= val
super SimpleIDN.to_unicode(val)
end
def generate_data
@json = generate_json
@body = generate_body
update_whois_server
end
def update_whois_server
wr = Whois::Record.find_or_initialize_by(name: name)
wr.body = @body
wr.json = @json
wr.save
end
def generate_body
template = Rails.root.join("app/views/for_models/whois_other.erb".freeze)
ERB.new(template.read, nil, "-").result(binding)
end
def generate_json
h = HashWithIndifferentAccess.new
h[:name] = self.name
h[:status] = 'Blocked'
h
end
def remove_data
Whois::Record.where(name: name).delete_all
end
end

View file

@ -38,7 +38,7 @@ class DomainNameValidator < ActiveModel::EachValidator
def validate_blocked(value)
return true unless value
return false if BlockedDomain.where("names @> ?::varchar[]", "{#{value}}").count > 0
return false if BlockedDomain.where(name: value).count > 0
ZonefileSetting.where(origin: value).count == 0
end
end

View file

@ -2,7 +2,7 @@ Estonia .ee Top Level Domain WHOIS server
Domain:
name: <%= @json['name'] %>
status: <%= @json['status'] %>
status: <%= @json['status'] %>
Estonia .ee Top Level Domain WHOIS server
More information at http://internet.ee