Merge remote-tracking branch 'origin/master' into 110163446-confirm_emails

This commit is contained in:
Stas 2016-01-04 15:18:11 +02:00
commit 24862ba16d
45 changed files with 794 additions and 388 deletions

View file

@ -1,23 +1,30 @@
module UserEvents
extend ActiveSupport::Concern
# TODO: remove old
# module ClassMethods
# def registrar_events(id)
# registrar = Registrar.find(id)
# return [] unless registrar
# @events = []
# registrar.users.each { |user| @events << user_events(user.id) }
# registrar.epp_users.each { |user| @events << epp_user_events(user.id) }
# @events
# end
included do
# EPP requires a server defined creator ID, which should be registrar code if we have one
def cr_id
# try this, rebuild user for registrar before searching history? really?
registrar = self.creator.try(:registrar)
if registrar.present? # Did creator return a kind of User that has a registrar?
registrar.code
else
if self.versions.first.try(:object).nil?
changes = self.versions.first.try(:object_changes)
cr_registrar_id = changes['registrar_id'].second if changes.present?
else
# untested, expected never to execute
cr_registrar_id = self.versions.first.object['registrar_id']
end
# def user_events(id)
# where(whodunnit: id.to_s)
# end
if cr_registrar_id.present?
Registrar.find(cr_registrar_id).code
else
# cr_id optional for domain, but required for contact; but we want something here anyway
self.creator_str # Fallback if we failed, maybe we can find a string here
end
end
end
end
# def epp_user_events(id)
# where(whodunnit: "#{id}-EppUser")
# end
# end
end

View file

@ -22,32 +22,30 @@ module Versions
def creator
return nil if creator_str.blank?
if creator_str =~ /^\d+-AdminUser:/
creator = AdminUser.find_by(id: creator_str)
elsif creator_str =~ /^\d+-ApiUser:/
creator = ApiUser.find_by(id: creator_str)
elsif creator_str =~ /^\d+-api-/ # depricated
creator = ApiUser.find_by(id: creator_str)
end
creator = user_from_id_role_username creator_str
creator.present? ? creator : creator_str
end
def updator
return nil if updator_str.blank?
if updator_str =~ /^\d+-AdminUser:/
updator = AdminUser.find_by(id: updator_str)
elsif updator_str =~ /^\d+-ApiUser:/
updator = ApiUser.find_by(id: updator_str)
elsif updator_str =~ /^\d+-api-/ # depricated
updator = ApiUser.find_by(id: updator_str)
end
updator = user_from_id_role_username updator_str
updator.present? ? updator : updator_str
end
def user_from_id_role_username(str)
user = ApiUser.find_by(id: $1) if str =~ /^(\d+)-(ApiUser:|api-)/
unless user.present?
user = AdminUser.find_by(id: $1) if str =~ /^(\d+)-AdminUser:/
unless user.present?
# on import we copied Registrar name, which may eql code
registrar = Registrar.find_by(name: str)
# assume each registrar has only one user
user = registrar.api_users.first if registrar
end
end
user
end
# callbacks
def touch_domain_version
domain.try(:touch_with_version)

View file

@ -1,6 +1,7 @@
class Contact < ActiveRecord::Base
include Versions # version/contact_version.rb
include EppErrors
include UserEvents
belongs_to :registrar
has_many :domain_contacts
@ -57,6 +58,11 @@ class Contact < ActiveRecord::Base
before_save :manage_statuses
def manage_statuses
if domain_transfer # very ugly but need better workflow
self.statuses = statuses | [OK, LINKED]
return
end
manage_linked
manage_ok
end
@ -80,6 +86,7 @@ class Contact < ActiveRecord::Base
]
attr_accessor :deliver_emails
attr_accessor :domain_transfer # hack but solves problem faster
#
# STATUSES
@ -203,6 +210,21 @@ class Contact < ActiveRecord::Base
['DeleteProhibited', SERVER_DELETE_PROHIBITED]
]
end
def to_csv
CSV.generate do |csv|
csv << column_names
all.each do |contact|
csv << contact.attributes.values_at(*column_names)
end
end
end
def pdf(html)
kit = PDFKit.new(html)
kit.to_pdf
end
end
def roid
@ -472,7 +494,7 @@ class Contact < ActiveRecord::Base
end
def update_related_whois_records
related_domain_descriptions.each{ |x, y| WhoisRecord.find_by(name: x).save}
related_domain_descriptions.each{ |x, y| WhoisRecord.find_by(name: x).try(:save) }
end
end

View file

@ -152,23 +152,17 @@ module Depp
}
end
data.css('dsData').each_with_index do |x, i|
ds = {
ds_key_tag: x.css('keyTag').first.try(:text),
ds_alg: x.css('alg').first.try(:text),
ds_digest_type: x.css('digestType').first.try(:text),
ds_digest: x.css('digest').first.try(:text)
data.css('keyData').each_with_index do |x, i|
ret[:dnskeys_attributes][i] = {
flags: x.css('flags').text,
protocol: x.css('protocol').text,
alg: x.css('alg').text,
public_key: x.css('pubKey').text,
ds_key_tag: x.css('keyTag').first.try(:text),
ds_alg: x.css('alg').first.try(:text),
ds_digest_type: x.css('digestType').first.try(:text),
ds_digest: x.css('digest').first.try(:text)
}
kd = x.css('keyData').first
ds.merge!({
flags: kd.css('flags').first.try(:text),
protocol: kd.css('protocol').first.try(:text),
alg: kd.css('alg').first.try(:text),
public_key: kd.css('pubKey').first.try(:text)
}) if kd
ret[:dnskeys_attributes][i] = ds
end
data.css('status').each_with_index do |x, i|

View file

@ -1,5 +1,6 @@
# rubocop: disable Metrics/ClassLength
class Domain < ActiveRecord::Base
include UserEvents
include Versions # version/domain_version.rb
include Statuses
has_paper_trail class_name: "DomainVersion", meta: { children: :children_log }
@ -238,10 +239,10 @@ class Domain < ActiveRecord::Base
end
count += 1
if domain.pending_update?
DomainMailer.pending_update_expired_notification_for_new_registrant(domain.id).deliver
domain.send_mail :pending_update_expired_notification_for_new_registrant
end
if domain.pending_delete? || domain.pending_delete_confirmation?
DomainMailer.pending_delete_expired_notification(domain.id, deliver_emails).deliver
DomainMailer.pending_delete_expired_notification(domain.id, true).deliver
end
domain.clean_pendings!
unless Rails.env.test?
@ -307,7 +308,7 @@ class Domain < ActiveRecord::Base
c = 0
Domain.where("statuses @> '{deleteCandidate}'::varchar[]").each do |x|
Whois::Record.where('domain_id = ?', x.id).try(':destroy')
WhoisRecord.where(domain_id: x.id).destroy_all
destroy_with_message x
STDOUT << "#{Time.zone.now.utc} Domain.destroy_delete_candidates: by deleteCandidate ##{x.id} (#{x.name})\n" unless Rails.env.test?
@ -315,7 +316,7 @@ class Domain < ActiveRecord::Base
end
Domain.where('force_delete_at <= ?', Time.zone.now).each do |x|
Whois::Record.where('domain_id = ?', x.id).try(':destroy')
WhoisRecord.where(domain_id: x.id).destroy_all
destroy_with_message x
STDOUT << "#{Time.zone.now.utc} Domain.destroy_delete_candidates: by force delete time ##{x.id} (#{x.name})\n" unless Rails.env.test?
c += 1
@ -439,7 +440,6 @@ class Domain < ActiveRecord::Base
end
def pending_update!
old_registrant_id = registrant_id_was
return true if pending_update?
self.epp_pending_update = true # for epp
@ -451,8 +451,8 @@ class Domain < ActiveRecord::Base
new_registrant_email = registrant.email
new_registrant_name = registrant.name
DomainMailer.pending_update_request_for_old_registrant(id, old_registrant_id, deliver_emails).deliver
DomainMailer.pending_update_notification_for_new_registrant(id, old_registrant_id, deliver_emails).deliver
send_mail :pending_update_request_for_old_registrant
send_mail :pending_update_notification_for_new_registrant
reload # revert back to original
@ -818,5 +818,10 @@ class Domain < ActiveRecord::Base
status_notes[status] = notes[i]
end
end
def send_mail(action)
DomainMailer.send(action, DomainMailModel.new(self).send(action)).deliver
end
end
# rubocop: enable Metrics/ClassLength

View file

@ -0,0 +1,180 @@
class DomainMailModel
# Capture current values used in app/views/mailers/domain_mailer/* and app/mailers/domain_mailer will send later
def initialize(domain)
@domain = domain
@params = {errors: [], deliver_emails: domain.deliver_emails, id: domain.id}
end
def pending_update_request_for_old_registrant
registrant_old
subject(:pending_update_request_for_old_registrant_subject)
confirm_update
domain_info
compose
end
def pending_update_notification_for_new_registrant
registrant # new registrant at this point
subject(:pending_update_notification_for_new_registrant_subject)
domain_info
compose
end
def registrant_updated_notification_for_new_registrant
registrant
subject(:registrant_updated_notification_for_new_registrant_subject)
domain_info
compose
end
def registrant_updated_notification_for_old_registrant
registrant_pending
registrant_old
subject(:registrant_updated_notification_for_old_registrant_subject)
new_registrant = Registrant.find @domain.pending_json['new_registrant_id']
@params[:registrant_name] = new_registrant.name
@params[:registrant_ident] = new_registrant.ident
@params[:registrant_priv] = new_registrant.priv?
@params[:registrant_email] = new_registrant.email
@params[:registrant_street] = new_registrant.street
@params[:registrant_city] = new_registrant.city
@params[:registrant_country] = new_registrant.country.name
compose
end
def pending_update_rejected_notification_for_new_registrant
registrant_pending
subject(:pending_update_rejected_notification_for_new_registrant_subject)
@params[:deliver_emails] = true # triggered from que
@params[:registrar_name] = @domain.registrar.name
compose
end
def pending_update_expired_notification_for_new_registrant
registrant_pending
subject(:pending_update_expired_notification_for_new_registrant_subject)
domain_info
compose
end
def pending_deleted
registrant
subject(:domain_pending_deleted_subject)
confirm_delete
compose
end
def pending_delete_rejected_notification
registrant
subject(:pending_delete_rejected_notification_subject)
compose
end
def pending_delete_expired_notification
registrant
subject(:pending_delete_expired_notification_subject)
compose
end
def delete_confirmation
registrant
subject(:delete_confirmation_subject)
compose
end
def force_delete
admins
subject(:force_delete_subject)
compose
end
private
def registrant_old
@params[:recipient] = format Registrant.find(@domain.registrant_id_was).email
end
def registrant
@params[:recipient] = format @domain.registrant.email
end
def registrant_pending
@params[:recipient] = format @domain.pending_json['new_registrant_email']
@params[:new_registrant_name] = @domain.pending_json['new_registrant_name']
@params[:old_registrant_name] = @domain.registrant.name
end
# registrant and domain admin contacts
def admins
emails = ([@domain.registrant.email] + @domain.admin_contacts.map { |x| format(x.email) })
@params[:recipient] = emails.uniq.map { |x| format(x) }
end
# puny internet domain name, TODO: username<email>
def format(email)
return warn_no_email if email.nil?
user, host = email.split('@')
host = SimpleIDN.to_ascii(host)
"#{user}@#{host}"
end
def subject(subject)
@params[:name] = @domain.name
@params[:subject] = "#{I18n.t(subject, name: @domain.name)}, [#{@domain.name}]"
end
def confirm_update
verification_url('domain_update_confirms')
end
def confirm_delete
verification_url('domain_delete_confirms')
end
def compose
@params
end
def verification_url(path)
token = verification_token or return
@params[:verification_url] = "#{ENV['registrant_url']}/registrant/#{path}/#{@domain.id}?token=#{token}"
end
def verification_token
return warn_missing(:registrant_verification_token) if @domain.registrant_verification_token.blank?
return warn_missing(:registrant_verification_asked_at) if @domain.registrant_verification_asked_at.blank?
@domain.registrant_verification_token
end
def domain_info
[:name, :registrar_name,
:registrant_name, :registrant_ident, :registrant_email,
:registrant_street,:registrant_city
].each do |attr|
@params.store attr, @domain.send(attr)
end
@params.store :registrant_country, @domain.registrant_country.name
@params.store :registrant_priv, @domain.registrant.priv?
@params.store :old_registrant_name, Registrant.find(@domain.registrant_id_was).name
@params
end
def warn_no_email(item = 'email')
warn_missing item
nil
end
def warn_missing(item)
warn_not_delivered "#{item.to_s} is missing for #{@domain.name}"
end
def warn_not_delivered(reason)
message = "EMAIL NOT DELIVERED: #{reason}"
@params[:errors] << message
# Rails.logger.warn message
nil
end
end

View file

@ -3,10 +3,12 @@ class Epp::Domain < Domain
include EppErrors
# TODO: remove this spagetti once data in production is correct.
attr_accessor :is_renewal
attr_accessor :is_renewal, :is_transfer
before_validation :manage_permissions
def manage_permissions
return if is_admin # this bad hack for 109086524, refactor later
return true if is_transfer
return unless update_prohibited? || delete_prohibited?
add_epp_error('2304', nil, nil, I18n.t(:object_status_prohibits_operation))
false
@ -14,7 +16,7 @@ class Epp::Domain < Domain
after_validation :validate_contacts
def validate_contacts
return true if is_renewal
return true if is_renewal || is_transfer
ok = true
active_admins = admin_domain_contacts.select { |x| !x.marked_for_destruction? }
@ -500,18 +502,23 @@ class Epp::Domain < Domain
# rubocop: enable Metrics/CyclomaticComplexity
def apply_pending_update!
old_registrant_email = DomainMailer.registrant_updated_notification_for_old_registrant(id, deliver_emails)
preclean_pendings
user = ApiUser.find(pending_json['current_user_id'])
frame = Nokogiri::XML(pending_json['frame'])
self.deliver_emails = true # turn on email delivery
send_mail :registrant_updated_notification_for_old_registrant
statuses.delete(DomainStatus::PENDING_UPDATE)
yield(self) if block_given? # need to skip statuses check here
self.save
::PaperTrail.whodunnit = user.id_role_username # updator str should be the request originator not the approval user
return unless update(frame, user, false)
clean_pendings!
self.deliver_emails = true # turn on email delivery
DomainMailer.registrant_updated_notification_for_new_registrant(id, deliver_emails).deliver
old_registrant_email.deliver
send_mail :registrant_updated_notification_for_new_registrant
update_whois_record
true
end
@ -592,6 +599,8 @@ class Epp::Domain < Domain
# rubocop: disable Metrics/CyclomaticComplexity
def transfer(frame, action, current_user)
@is_transfer = true
case action
when 'query'
return domain_transfers.last if domain_transfers.any?
@ -619,6 +628,7 @@ class Epp::Domain < Domain
oc.registrar_id = registrar_id
oc.copy_from_id = c.id
oc.prefix_code
oc.domain_transfer = true
oc.save!(validate: false)
oc
end

View file

@ -17,7 +17,8 @@ class Invoice < ActiveRecord::Base
validates :invoice_type, :due_date, :currency, :seller_name,
:seller_iban, :buyer_name, :invoice_items, :vat_prc, presence: true
before_create :set_invoice_number
before_create :set_invoice_number, :check_vat
def set_invoice_number
last_no = Invoice.order(number: :desc).where('number IS NOT NULL').limit(1).pluck(:number).first
@ -34,6 +35,12 @@ class Invoice < ActiveRecord::Base
false
end
def check_vat
if buyer.country_code != 'EE' && buyer.vat_no.present?
self.vat_prc = 0
end
end
before_save -> { self.sum_cache = sum }
class << self

View file

@ -31,7 +31,7 @@ class Pricelist < ActiveRecord::Base
def pricelist_for(zone, operation, period)
lists = valid.where(category: zone, operation_category: operation, duration: period)
return lists.first if lists.count == 1
lists.where('valid_to IS NOT NULL').order(valid_from: :desc).first
lists.order(valid_from: :desc).first
end
end
end

View file

@ -12,7 +12,6 @@ class RegistrantVerification < ActiveRecord::Base
belongs_to :domain
validates :verification_token, :domain_name, :domain, :action, :action_type, presence: true
validates :domain, uniqueness: { scope: [:domain_id, :verification_token] }
def domain_registrant_change_confirm!
self.action_type = DOMAIN_REGISTRANT_CHANGE

View file

@ -3,4 +3,9 @@ class User < ActiveRecord::Base
devise :trackable, :timeoutable
attr_accessor :phone
def id_role_username
"#{self.id}-#{self.class}: #{self.username}"
end
end

View file

@ -2,8 +2,6 @@ class ContactVersion < PaperTrail::Version
include VersionSession
self.table_name = :log_contacts
self.sequence_name = :log_contacts_id_seq
# include UserEvents
# scope :deleted, -> { where(event: 'destroy') }
end

View file

@ -4,7 +4,5 @@ class DomainVersion < PaperTrail::Version
self.table_name = :log_domains
self.sequence_name = :log_domains_id_seq
include UserEvents
scope :deleted, -> { where(event: 'destroy') }
end

View file

@ -23,6 +23,10 @@ class WhoisRecord < ActiveRecord::Base
end
end
def self.find_by_name(name)
WhoisRecord.where("lower(name) = ?", name.downcase)
end
def generated_json
@generated_json ||= generate_json
end
@ -44,7 +48,7 @@ class WhoisRecord < ActiveRecord::Base
h[:changed] = domain.updated_at.try(:to_s, :iso8601)
h[:expire] = domain.valid_to.try(:to_date).try(:to_s)
h[:outzone] = domain.outzone_at.try(:to_date).try(:to_s)
h[:delete] = domain.delete_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] = domain.registrant.name
@ -113,6 +117,6 @@ class WhoisRecord < ActiveRecord::Base
end
def destroy_whois_record
Whois::Record.where(name: name).delete_all()
Whois::Record.where(name: name).delete_all
end
end