Merge branch 'improve-registrar' into registry-267

# Conflicts:
#	db/schema-read-only.rb
#	db/structure.sql
This commit is contained in:
Artur Beljajev 2016-12-28 15:31:04 +02:00
commit 5c6a0034f6
15 changed files with 316 additions and 22 deletions

View file

@ -17,10 +17,15 @@ class Admin::RegistrarsController < AdminController
def create
@registrar = Registrar.new(registrar_params)
if @registrar.save
begin
@registrar.transaction do
@registrar.save!
@registrar.accounts.create!(account_type: Account::CASH, currency: 'EUR')
end
flash[:notice] = I18n.t('registrar_added')
redirect_to [:admin, @registrar]
else
rescue ActiveRecord::RecordInvalid
flash.now[:alert] = I18n.t('failed_to_add_registrar')
render 'new'
end

View file

@ -15,6 +15,7 @@ class Contact < ActiveRecord::Base
has_paper_trail class_name: "ContactVersion", meta: { children: :children_log }
attr_accessor :legal_document_id
alias_attribute :kind, :ident_type
accepts_nested_attributes_for :legal_documents
@ -583,4 +584,9 @@ class Contact < ActiveRecord::Base
self[attr_name.to_sym] = nil
end
end
def reg_no
return if priv?
ident
end
end

View file

@ -142,4 +142,9 @@ class DomainCron
)
end
def self.delete_legal_doc_duplicates
Rake::Task['legal_doc:remove_duplicates'].reenable
Rake::Task['legal_doc:remove_duplicates'].invoke
end
end

View file

@ -1,4 +1,5 @@
class LegalDocument < ActiveRecord::Base
cattr_accessor :explicitly_write_file
include EppErrors
MIN_BODY_SIZE = (1.37 * 3.kilobytes).ceil
@ -16,7 +17,7 @@ class LegalDocument < ActiveRecord::Base
validate :val_body_length, if: ->(file){ file.path.blank? && !Rails.env.staging?}
before_create :add_creator
before_save :save_to_filesystem
before_save :save_to_filesystem, if: :body
def epp_code_map
{
@ -32,22 +33,82 @@ class LegalDocument < ActiveRecord::Base
def save_to_filesystem
loop do
rand = SecureRandom.random_number.to_s.last(4)
next if rand.to_i == 0 || rand.length < 4
binary = Base64.decode64(body)
digest = Digest::SHA1.new.update(binary).to_s
dir = "#{ENV['legal_documents_dir']}/#{Time.zone.now.strftime('%Y/%m/%d')}"
FileUtils.mkdir_p(dir, mode: 0775)
self.path = "#{dir}/#{Time.zone.now.to_formatted_s(:number)}_#{rand}.#{document_type}"
break unless File.file?(path)
loop do
rand = SecureRandom.random_number.to_s.last(4)
next if rand.to_i == 0 || rand.length < 4
dir = "#{ENV['legal_documents_dir']}/#{Time.zone.now.strftime('%Y/%m/%d')}"
FileUtils.mkdir_p(dir, mode: 0775)
self.path = "#{dir}/#{Time.zone.now.to_formatted_s(:number)}_#{rand}.#{document_type}"
break unless File.file?(path)
end
File.open(path, 'wb') { |f| f.write(Base64.decode64(body)) } unless Rails.env.test?
File.open(path, 'wb') { |f| f.write(binary) } if !Rails.env.test? || self.class.explicitly_write_file
self.path = path
self.checksum = digest
end
def calc_checksum
digest = Digest::SHA1.new
digest.update File.binread(path)
digest.to_s
end
def add_creator
self.creator_str = ::PaperTrail.whodunnit
true
end
def self.remove_duplicates
start = Time.zone.now.to_f
puts '-----> Removing legal documents duplicates'
count = 0
modified = Array.new
LegalDocument.where(documentable_type: "Domain").where.not(checksum: [nil, ""]).find_each do |orig_legal|
next if modified.include?(orig_legal.checksum)
next if !File.exist?(orig_legal.path)
modified.push(orig_legal.checksum)
LegalDocument.where(documentable_type: "Domain", documentable_id: orig_legal.documentable_id).
where(checksum: orig_legal.checksum).
where.not(id: orig_legal.id).where.not(path: orig_legal.path).each do |new_legal|
unless modified.include?(orig_legal.id)
File.delete(new_legal.path) if File.exist?(new_legal.path)
new_legal.update(path: orig_legal.path)
count += 1
puts "File #{new_legal.path} has been removed by Domain #{new_legal.documentable_id}. Document id: #{new_legal.id}"
end
end
contact_ids = DomainVersion.where(item_id: orig_legal.documentable_id).distinct.
pluck("object->>'registrant_id'", "object_changes->>'registrant_id'",
"children->>'tech_contacts'", "children->>'admin_contacts'",
"tech_contact_ids", "admin_contact_ids").flatten.uniq
contact_ids = contact_ids.map{|id|
case id
when Hash
id["id"]
when String
JSON.parse(id) rescue id.to_i
else
id
end
}.flatten.compact.uniq
LegalDocument.where(documentable_type: "Contact", documentable_id: contact_ids).
where(checksum: orig_legal.checksum).where.not(path: orig_legal.path).each do |new_legal|
unless modified.include?(orig_legal.id)
File.delete(new_legal.path) if File.exist?(new_legal.path)
new_legal.update(path: orig_legal.path)
count += 1
puts "File #{new_legal.path} has been removed by Contact #{new_legal.documentable_id}. Document id: #{new_legal.id}"
end
end
end
puts "-----> Duplicates fixed for #{count} rows in #{(Time.zone.now.to_f - start).round(2)} seconds"
end
end

View file

@ -57,11 +57,6 @@ class Registrar < ActiveRecord::Base
RegenerateRegistrarWhoisesJob.enqueue id
end
after_create :create_cash_account
def create_cash_account
accounts.create(account_type: Account::CASH, currency: 'EUR')
end
class << self
def search_by_query(query)
res = search(name_or_reg_no_cont: query).result

View file

@ -41,6 +41,8 @@ class WhoisRecord < ActiveRecord::Base
'ok' => 'ok (paid and in zone)'
}
registrant = domain.registrant
@disclosed = []
h[:name] = domain.name
h[:status] = domain.statuses.map { |x| status_map[x] || x }
@ -50,11 +52,17 @@ class WhoisRecord < ActiveRecord::Base
h[:outzone] = domain.outzone_at.try(:to_date).try(:to_s)
h[:delete] = [domain.delete_at, domain.force_delete_at].compact.min.try(:to_date).try(:to_s)
h[:registrant] = registrant.name
h[:registrant_kind] = registrant.kind
h[:registrant] = domain.registrant.name
h[:email] = domain.registrant.email
@disclosed << [:email, domain.registrant.email]
h[:registrant_changed] = domain.registrant.updated_at.try(:to_s, :iso8601)
if registrant.org?
h[:registrant_reg_no] = registrant.reg_no
h[:registrant_ident_country_code] = registrant.ident_country_code
end
h[:email] = registrant.email
@disclosed << [:email, registrant.email]
h[:registrant_changed] = registrant.updated_at.try(:to_s, :iso8601)
h[:admin_contacts] = []
domain.admin_contacts.each do |ac|
@ -82,14 +90,14 @@ class WhoisRecord < ActiveRecord::Base
h[:registrar_address] = domain.registrar.address
h[:registrar_changed] = domain.registrar.updated_at.try(:to_s, :iso8601)
h[:nameservers] = domain.nameservers.pluck(:hostname).uniq.select(&:present?)
h[:nameservers] = domain.nameservers.hostnames.uniq.select(&:present?)
h[:nameservers_changed] = domain.nameservers.pluck(:updated_at).max.try(:to_s, :iso8601)
h[:dnssec_keys] = domain.dnskeys.map{|key| "#{key.flags} #{key.protocol} #{key.alg} #{key.public_key}" }
h[:dnssec_changed] = domain.dnskeys.pluck(:updated_at).max.try(:to_s, :iso8601) rescue nil
h[:disclosed] = @disclosed # later we can replace
h[:disclosed] = @disclosed
h
end

View file

@ -13,6 +13,8 @@ delete: <%= json['delete'].to_s.tr('T',' ').sub('+', ' +') %>
Registrant:
name: <%= json['registrant'] %>
org id: <%= json['registrant_reg_no'] %>
country: <%= json['registrant_ident_country_code'] %>
email: Not Disclosed - Visit www.internet.ee for webbased WHOIS
changed: <%= json['registrant_changed'].to_s.tr('T',' ').sub('+', ' +') %>