From 27d19ad237adcb858a320cfd38b49b811a015b26 Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Mon, 16 Feb 2015 11:45:27 +0200 Subject: [PATCH] Create domain works with nested attributes --- app/controllers/epp/domains_controller.rb | 111 ++++++++++++++++++++-- app/models/domain.rb | 1 + app/models/epp/epp_domain.rb | 17 ++++ config/locales/en.yml | 1 + 4 files changed, 124 insertions(+), 6 deletions(-) diff --git a/app/controllers/epp/domains_controller.rb b/app/controllers/epp/domains_controller.rb index 26eeb7eb6..c65b14fb1 100644 --- a/app/controllers/epp/domains_controller.rb +++ b/app/controllers/epp/domains_controller.rb @@ -1,9 +1,8 @@ class Epp::DomainsController < EppController def create @domain = Epp::EppDomain.new(domain_create_params) - - @domain.parse_and_attach_domain_dependencies(params[:parsed_frame]) - @domain.parse_and_attach_ds_data(params[:parsed_frame].css('extension create')) + # @domain.parse_and_attach_domain_dependencies(params[:parsed_frame]) + # @domain.parse_and_attach_ds_data(params[:parsed_frame].css('extension create')) if @domain.errors.any? || !@domain.save handle_errors(@domain) @@ -171,18 +170,118 @@ class Epp::DomainsController < EppController end def domain_create_params - name = params[:parsed_frame].css('name').text period = params[:parsed_frame].css('period').text + # registrant = parse_registrant(params) + # domain_contacts = parse_domain_contacts_from_epp(params) + + registrant_code = params[:parsed_frame].css('registrant').text + owner_contact = Contact.find_by(code: registrant_code) + + unless owner_contact + epp_errors << { + code: '2303', + msg: I18n.t('registrant_not_found'), + value: { obj: 'registrant', val: registrant_code } + } + end + { - name: name, + name: params[:parsed_frame].css('name').text, registrar_id: current_user.registrar.try(:id), registered_at: Time.now, period: (period.to_i == 0) ? 1 : period.to_i, - period_unit: Epp::EppDomain.parse_period_unit_from_frame(params[:parsed_frame]) || 'y' + period_unit: Epp::EppDomain.parse_period_unit_from_frame(params[:parsed_frame]) || 'y', + owner_contact_id: owner_contact.try(:id), + nameservers_attributes: Epp::EppDomain.parse_nameservers_from_frame(params[:parsed_frame]), + domain_contacts_attributes: parse_domain_contacts_from_epp, + dnskeys_attributes: parse_dnskeys_from_frame(params[:parsed_frame].css('extension create')), + legal_documents_attributes: parse_legal_document_from_frame(params[:parsed_frame]) } end + def parse_domain_contacts_from_epp + res = [] + params[:parsed_frame].css('contact').each do |x| + c = Contact.find_by(code: x.text).try(:id) + + unless c + epp_errors << { + code: '2303', + msg: I18n.t('contact_not_found'), + value: { obj: 'contact', val: x.text } + } + next + end + + res << { + contact_id: Contact.find_by(code: x.text).try(:id), + contact_type: x['type'], + contact_code_cache: x.text + } + end + + res + end + + def parse_dnskeys_from_frame(parsed_frame) + res = [] + # res = { ds_data: [], key_data: [] } + + # res[:max_sig_life] = parsed_frame.css('maxSigLife').first.try(:text) + + res = parse_ds_data_from_frame(parsed_frame, res) + parse_key_data_from_frame(parsed_frame, res) + end + + def parse_key_data_from_frame(parsed_frame, res) + parsed_frame.xpath('keyData').each do |x| + res << { + flags: x.css('flags').first.try(:text), + protocol: x.css('protocol').first.try(:text), + alg: x.css('alg').first.try(:text), + public_key: x.css('pubKey').first.try(:text), + ds_alg: 3, + ds_digest_type: Setting.ds_algorithm + } + end + + res + end + + def parse_ds_data_from_frame(parsed_frame, res) + parsed_frame.css('dsData').each do |x| + data = { + 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 + data.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 + + res << data + end + + res + end + + def parse_legal_document_from_frame(parsed_frame) + ld = parsed_frame.css('legalDocument').first + return [] unless ld + + [{ + body: ld.text, + document_type: ld['type'] + }] + end + def domain_transfer_params res = {} res[:pw] = params[:parsed_frame].css('pw').first.try(:text) diff --git a/app/models/domain.rb b/app/models/domain.rb index ee70421f0..3ca9e0f51 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -41,6 +41,7 @@ class Domain < ActiveRecord::Base reject_if: proc { |attrs| attrs[:public_key].blank? } has_many :legal_documents, as: :documentable + accepts_nested_attributes_for :legal_documents, reject_if: proc { |attrs| attrs[:body].blank? } delegate :code, to: :owner_contact, prefix: true delegate :email, to: :owner_contact, prefix: true diff --git a/app/models/epp/epp_domain.rb b/app/models/epp/epp_domain.rb index 7332b423f..0cd728499 100644 --- a/app/models/epp/epp_domain.rb +++ b/app/models/epp/epp_domain.rb @@ -2,6 +2,8 @@ class Epp::EppDomain < Domain include EppErrors + accepts_nested_attributes_for :nameservers + def epp_code_map # rubocop:disable Metrics/MethodLength { '2002' => [ @@ -58,6 +60,21 @@ class Epp::EppDomain < Domain } end + def self.new_from_epp(domain_params) + new(domain_params) + end + + + + + + + + + + + + def parse_and_attach_domain_dependencies(parsed_frame) attach_owner_contact(self.class.parse_owner_contact_from_frame(parsed_frame)) attach_contacts(self.class.parse_contacts_from_frame(parsed_frame)) diff --git a/config/locales/en.yml b/config/locales/en.yml index 5ab4ccef9..fc4ffbf3b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -501,3 +501,4 @@ en: address_help: 'Street name, house no - apartment no, city, county, country, zip' download: 'Download' failed_to_create_certificate: 'Failed to create certificate!' + registrant_not_found: 'Registrant not found'