diff --git a/app/controllers/epp/commands_controller.rb b/app/controllers/epp/commands_controller.rb index 8e3c1ac19..d99cf383b 100644 --- a/app/controllers/epp/commands_controller.rb +++ b/app/controllers/epp/commands_controller.rb @@ -24,4 +24,8 @@ class Epp::CommandsController < ApplicationController def info send("info_#{OBJECT_TYPES[params_hash['epp']['xmlns:ns2']]}") end + + def update + send("update_#{OBJECT_TYPES[params_hash['epp']['xmlns:ns2']]}") + end end diff --git a/app/helpers/epp/contacts_helper.rb b/app/helpers/epp/contacts_helper.rb index e987425e7..3881f9f1c 100644 --- a/app/helpers/epp/contacts_helper.rb +++ b/app/helpers/epp/contacts_helper.rb @@ -10,6 +10,17 @@ module Epp::ContactsHelper end end + def update_contact + code = params_hash['epp']['command']['update']['update'][:id] + @contact = Contact.where(code: code).first + if @contact.update_attributes(contact_and_address_attributes.delete_if { |k, v| v.nil? }) + render 'epp/contacts/update' + else + handle_contact_errors + render '/epp/error' + end + end + def delete_contact ph = params_hash['epp']['command']['delete']['delete'] @@ -60,21 +71,30 @@ module Epp::ContactsHelper private def contact_and_address_attributes - ph = params_hash['epp']['command']['create']['create'] - { - code: ph[:id], - phone: ph[:voice], - ident: ph[:ident], - ident_type: ident_type, - email: ph[:email], - name: ph[:postalInfo][:name], - org_name: ph[:postalInfo][:org], - address_attributes: { - country_id: Country.find_by(iso: ph[:postalInfo][:addr][:cc]), - street: tidy_street, - zip: ph[:postalInfo][:addr][:pc] - } + ph = params_hash['epp']['command'][params[:command]][params[:command]] + ph = ph[:chg] if params[:command] == 'update' + contact_hash = { + code: ph[:id], + phone: ph[:voice], + ident: ph[:ident], + ident_type: ident_type, + email: ph[:email], } + + contact_hash = contact_hash.merge({ + name: ph[:postalInfo][:name], + org_name: ph[:postalInfo][:org] + }) if ph[:postalInfo].is_a? Hash + + contact_hash = contact_hash.merge({ + address_attributes: { + country_id: Country.find_by(iso: ph[:postalInfo][:addr][:cc]), + street: tidy_street, + zip: ph[:postalInfo][:addr][:pc] + } + }) if ph[:postalInfo].is_a?(Hash) && ph[:postalInfo][:addr].is_a?(Hash) + + contact_hash end def has_rights @@ -85,10 +105,13 @@ module Epp::ContactsHelper end def tidy_street - street = params_hash['epp']['command']['create']['create'][:postalInfo][:addr][:street] + command = params[:command] + street = params_hash['epp']['command'][command][command][:postalInfo][:addr][:street] return street if street.is_a? String return street.join(',') if street.is_a? Array return nil + rescue NoMethodError => e #refactor so wouldn't use rescue for flow control + return nil end def ident_type @@ -103,7 +126,8 @@ module Epp::ContactsHelper def handle_contact_errors # handle_errors conflicted with domain logic handle_epp_errors({ '2302' => ['Contact id already exists'], - '2303' => [:not_found, :epp_obj_does_not_exist] + '2303' => [:not_found, :epp_obj_does_not_exist], + '2005' => ['Phone nr is invalid', 'Email is invalid'] },@contact) end end diff --git a/app/models/contact.rb b/app/models/contact.rb index 23b843c57..1df21195b 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -14,7 +14,10 @@ class Contact < ActiveRecord::Base validates_presence_of :code, :name, :phone, :email, :ident validate :ident_must_be_valid - validates :phone, format: { with: /\+\d{3}\.\d+/, message: "bad format" } + + validates :phone, format: /\+[0-9]{1,3}\.[0-9]{1,14}?/ #/\+\d{3}\.\d+/ + validates :email, format: /@/ + validates_uniqueness_of :code, message: :epp_id_taken IDENT_TYPE_ICO = 'ico' diff --git a/app/views/epp/contacts/update.xml.builder b/app/views/epp/contacts/update.xml.builder new file mode 100644 index 000000000..5377f0fa9 --- /dev/null +++ b/app/views/epp/contacts/update.xml.builder @@ -0,0 +1,16 @@ +xml.epp_head do + xml.response do + xml.result('code' => '1000') do + xml.msg 'Command completed successfully' + end + + xml.resData do + xml.tag!('contact:creData', 'xmlns:contact' => 'http://www.nic.cz/xml/epp/contact-1.6', 'xsi:schemaLocation' => 'http://www.nic.cz/xml/epp/contact-1.6 contact-1.6.xsd') do + xml.tag!('contact:id', @contact.code) + xml.tag!('contact:crDate', @contact.created_at) + end + end + + xml << render('/epp/shared/trID') + end +end diff --git a/config/locales/en.yml b/config/locales/en.yml index e4ce7adda..a0caab90c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -31,8 +31,10 @@ en: blank: "Required parameter missing - name" phone: blank: "Required parameter missing - phone" + invalid: "Phone nr is invalid" email: blank: "Required parameter missing - email" + invalid: "Email is invalid" ident: blank: "Required parameter missing - ident" domain: diff --git a/doc/schemas/contact-1.0.xsd b/doc/schemas/contact-1.0.xsd index 903f28183..dc2b366e6 100644 --- a/doc/schemas/contact-1.0.xsd +++ b/doc/schemas/contact-1.0.xsd @@ -48,7 +48,7 @@ - + @@ -236,6 +236,8 @@ minOccurs="0"/> + + + + + + sh8013 + + + John Doe + + 123 Example Dr. + Suite 100 + Dulles + VA + 20166-6503 + EE + + + +123.7035555555 + +1.7035555556 + jdoe@example.com + 37605030299 + + 2fooBAR + + + + + + + + + ABC-12345 + + diff --git a/spec/epp/requests/contacts/update_with_errors.xml b/spec/epp/requests/contacts/update_with_errors.xml new file mode 100644 index 000000000..e9a9ddb39 --- /dev/null +++ b/spec/epp/requests/contacts/update_with_errors.xml @@ -0,0 +1,16 @@ + + + + + + sh8013 + + 123456798 + faulty + + + + ABC-12345 + + diff --git a/spec/models/contact_spec.rb b/spec/models/contact_spec.rb index bce1b2ccb..fd028f372 100644 --- a/spec/models/contact_spec.rb +++ b/spec/models/contact_spec.rb @@ -23,8 +23,8 @@ describe Contact do expect(@contact.errors.messages).to match_array({ :code=>["Required parameter missing - code"], :name=>["Required parameter missing - name"], - :phone=>["Required parameter missing - phone", "bad format"], - :email=>["Required parameter missing - email"], + :phone=>["Required parameter missing - phone", "Phone nr is invalid"], + :email=>["Required parameter missing - email", "Email is invalid"], :ident=>["Required parameter missing - ident"] }) end