Create DomainCreate action

This commit is contained in:
Karl Erik Õunapuu 2020-12-08 12:00:04 +02:00
parent b683fe813c
commit ccef1053d9
No known key found for this signature in database
GPG key ID: C9DD647298A34764
6 changed files with 280 additions and 28 deletions

View file

@ -28,6 +28,10 @@ module Epp
def create
authorize! :create, Epp::Domain
domain_params = ::Deserializers::Xml::DomainCreate.new(params[:parsed_frame], current_user.registrar.id)
puts "Ayy lmao"
puts domain_params.call
if Domain.release_to_auction
request_domain_name = params[:parsed_frame].css('name').text.strip.downcase
domain_name = DNS::DomainName.new(SimpleIDN.to_unicode(request_domain_name))

View file

@ -0,0 +1,134 @@
module Actions
class DomainCreate
attr_reader :domain, :params
def initialize(domain, params)
@domain = domain
@params = params
end
def call
validate_domain_integrity
assign_registrant
assign_domain_attributes
assign_nameservers
assign_admin_contacts
assign_tech_contacts
assign_expiry_time
return domain unless domain.save
commit
end
# Check if domain is eligible for new registration
def validate_domain_integrity
return if Domain.release_to_auction
dn = DNS::DomainName.new(SimpleIDN.to_unicode(params[:name]))
domain.add_epp_error(2306, nil, nil, 'Parameter value policy error: domain is at auction') if dn.at_auction?
domain.add_epp_error(2003, nil, nil, 'Required parameter missing; reserved>pw element required for reserved domains') if dn.awaiting_payment?
return unless dn.pending_registration?
domain.add_epp_error(2003, nil, nil, 'Required parameter missing; reserved>pw element is required') if params[:reserved_pw].empty?
domain.add_epp_errpr(2202, nil, nil, 'Invalid authorization information; invalid reserved>pw value') unless dn.available_with_code?(params[:reserved_pw])
end
def assign_registrant
domain.add_epp_error('2306', nil, nil, %i[registrant cannot_be_missing]) and return unless params[:registrant_id]
regt = Registrant.find_by(code: params[:registrant_id])
domain.add_epp_error('2303', 'registrant', code, %i[registrant not_found]) and return unless regt
domain.registrant = regt
end
def assign_domain_attributes
domain.name = params[:name]
domain.registrar = Registrar.find(params[:registrar_id])
domain.period = params[:period]
domain.period_unit = params[:period_unit]
domain.reserved_pw = params[:reserved_pw] if params[:transfer_code]
domain.transfer_code = params[:transfer_code] if params[:transfer_code]
domain.dnskeys_attributes = params[:dnskeys_attributes]
end
def assign_nameservers
domain.nameservers_attributes = params[:nameservers_attributes]
end
def assign_admin_contacts
attrs = []
params[:admin_domain_contacts_attributes].each do |c|
contact = Contact.find_by(code: c)
domain.add_epp_error('2303', 'contact', c, %i[domain_contacts not_found]) unless contact
attrs << { contact_id: contact.id, contact_code_cache: contact.code } if contact
end
domain.admin_domain_contacts_attributes = attrs
end
def assign_tech_contacts
attrs = []
params[:tech_domain_contacts_attributes].each do |c|
contact = Contact.find_by(code: c)
domain.add_epp_error('2303', 'contact', c, %i[domain_contacts not_found]) unless contact
attrs << { contact_id: contact.id, contact_code_cache: contact.code } if contact
end
domain.tech_domain_contacts_attributes = attrs
end
def assign_expiry_time
period = domain.period.to_i
plural_period_unit_name = (domain.period_unit == 'm' ? 'months' : 'years').to_sym
expire_time = (Time.zone.now.advance(plural_period_unit_name => period) + 1.day).beginning_of_day
domain.expire_time = expire_time
end
def debit_registrar
domain_pricelist = domain.pricelist('create', domain.period.try(:to_i), domain.period_unit)
if @domain_pricelist.try(:price) && domain.registrar.balance < domain_pricelist.price.amount
domain.add_epp_error(2104, nil, nil, I18n.t('billing_failure_credit_balance_low'))
return
elsif !@domain_pricelist.try(:price)
domain.add_epp_error(2104, nil, nil, I18n.t(:active_price_missing_for_this_operation))
return
end
domain.registrar.debit!({ sum: @domain_pricelist.price.amount, price: @domain_pricelist,
description: "#{I18n.t('create')} #{@domain.name}",
activity_type: AccountActivity::CREATE })
end
def process_auction_and_disputes
dn = DNS::DomainName.new(SimpleIDN.to_unicode(domain.name))
Dispute.close_by_domain(domain.name)
return unless Domain.release_to_auction && dn.pending_registration?
auction = Auction.find_by(domain: domain.name, status: Auction.statuses[:payment_received])
auction.domain_registered!
end
def maybe_attach_legal_doc
return unless legal_document
doc = LegalDocument.create(
documentable_type: Contact,
document_type: legal_document[:type], body: legal_document[:body]
)
contact.legal_documents = [doc]
contact.legal_document_id = doc.id
end
def commit
ActiveRecord::Base.transaction do
debit_registrar
process_auction_and_disputes
domain.save
end
end
end
end

View file

@ -1,5 +1,6 @@
require 'deserializers/xml/legal_document'
require 'deserializers/xml/nameserver'
require 'deserializers/xml/domain_create'
class Epp::Domain < Domain
include EppErrors
@ -38,6 +39,10 @@ class Epp::Domain < Domain
class << self
def new_from_epp(frame, current_user)
domain_create = ::Deserializers::Xml::DomainCreate.new(frame, current_user.registrar.id)
puts "DOMAIN CREATE ACTION"
puts domain_create
domain = Epp::Domain.new
domain.attributes = domain.attrs_from(frame, current_user)
domain.attach_default_contacts
@ -147,23 +152,19 @@ class Epp::Domain < Domain
end if registrant_frame
at[:name] = frame.css('name').text if new_record?
at[:registrar_id] = current_user.registrar.try(:id)
period = frame.css('period').text
at[:period] = (period.to_i == 0) ? 1 : period.to_i
at[:period_unit] = Epp::Domain.parse_period_unit_from_frame(frame) || 'y'
at[:reserved_pw] = frame.css('reserved > pw').text
at[:name] = frame.css('name').text if new_record? # Done
at[:registrar_id] = current_user.registrar.try(:id) # Done
period = frame.css('period').text # Done
at[:period] = (period.to_i == 0) ? 1 : period.to_i # Done
at[:period_unit] = Epp::Domain.parse_period_unit_from_frame(frame) || 'y' # Done
at[:reserved_pw] = frame.css('reserved > pw').text # Done
pw = frame.css('authInfo > pw').text # Done
at[:transfer_code] = pw if pw.present? # Done
# at[:statuses] = domain_statuses_attrs(frame, action)
at[:nameservers_attributes] = nameservers_attrs(frame, action)
at[:admin_domain_contacts_attributes] = admin_domain_contacts_attrs(frame, action)
at[:tech_domain_contacts_attributes] = tech_domain_contacts_attrs(frame, action)
puts "JHDFHJDGFKDJHF"
pw = frame.css('authInfo > pw').text
at[:transfer_code] = pw if pw.present?
if new_record?
dnskey_frame = frame.css('extension create')
@ -315,6 +316,7 @@ class Epp::Domain < Domain
add_epp_error('2303', nil, nil, [:dnskeys, :not_found]) if keys.include? nil
end
end
errors.any? ? [] : keys
end