Add XML attribute validator

This commit is contained in:
Martin Lensment 2014-08-08 12:13:51 +03:00
parent 36e36cf616
commit 01cfddee95
5 changed files with 42 additions and 13 deletions

View file

@ -38,6 +38,19 @@ module Epp::Common
render '/epp/error'
end
def xml_attrs_present?(ph, attributes)
attributes.each do |x|
epp_errors << {code: '2003', msg: I18n.t('errors.messages.required_parameter_missing', key: x.last)} unless has_attribute(ph, x)
end
epp_errors.empty?
end
def has_attribute(ph, path)
path.inject(ph) do |location, key|
location.respond_to?(:keys) ? location[key] : nil
end
end
def validate_request
type = OBJECT_TYPES[params_hash['epp']['xmlns:ns2']]
return unless type

View file

@ -1,6 +1,19 @@
module Epp::DomainsHelper
def create_domain
@domain = Domain.new(domain_create_params)
ph = params_hash['epp']['command']['create']['create']
unless xml_attrs_present?(ph, [['name'], ['ns'], ['authInfo'], ['contact'], ['registrant']])
render '/epp/error' and return
end
@domain = Domain.new(domain_create_params(ph))
if owner_contact_id = Contact.find_by(code: ph[:registrant]).try(:id)
@domain.owner_contact_id = owner_contact_id
else
epp_errors << {code: '2303', msg: I18n.t('errors.messages.epp_registrant_not_found'), value: {obj: 'registrant', val: ph[:registrant]}}
render '/epp/error' and return
end
Domain.transaction do
if @domain.save && @domain.attach_contacts(domain_contacts) && @domain.attach_nameservers(domain_nameservers)
@ -23,7 +36,7 @@ module Epp::DomainsHelper
@domain = Domain.find_by(name: ph[:name])
unless @domain
epp_errors << {code: '2303', msg: I18n.t('errors.messages.epp_domain_not_found'), value: {obj: 'domain', val: ph[:name]}}
epp_errors << {code: '2303', msg: I18n.t('errors.messages.epp_domain_not_found'), value: {obj: 'name', val: ph[:name]}}
render '/epp/error' and return
end
@ -37,8 +50,7 @@ module Epp::DomainsHelper
### HELPER METHODS ###
private
def domain_create_params
ph = params_hash['epp']['command']['create']['create']
def domain_create_params(ph)
{
name: ph[:name],
registrar_id: current_epp_user.registrar.try(:id),
@ -46,8 +58,7 @@ module Epp::DomainsHelper
period: ph[:period].to_i,
valid_from: Date.today,
valid_to: Date.today + ph[:period].to_i.years,
auth_info: ph[:authInfo][:pw],
owner_contact_id: Contact.find_by(code: ph[:registrant]).try(:id)
auth_info: ph[:authInfo][:pw]
}
end

View file

@ -123,7 +123,7 @@ class Domain < ActiveRecord::Base
self.period = period
save
else
errors[:base] << {msg: I18n.t('errors.messages.epp_exp_dates_do_not_match'), obj: 'domain', val: cur_exp_date}
errors[:base] << {msg: I18n.t('errors.messages.epp_exp_dates_do_not_match'), obj: 'curExpDate', val: cur_exp_date}
false
end
end