diff --git a/app/controllers/concerns/shared/user_stamper.rb b/app/controllers/concerns/shared/user_stamper.rb new file mode 100644 index 000000000..f0ee8c6e1 --- /dev/null +++ b/app/controllers/concerns/shared/user_stamper.rb @@ -0,0 +1,15 @@ +module Shared::UserStamper + extend ActiveSupport::Concern + + def stamp obj + return false if obj.nil? || !obj.has_attribute?( :created_by_id && :updated_by_id ) + + if obj.new_record? + obj.created_by_id = current_epp_user.id + else + obj.updated_by_id = current_epp_user.id + end + + return true + end +end diff --git a/app/controllers/epp/commands_controller.rb b/app/controllers/epp/commands_controller.rb index 38b2b065d..c8cb040d6 100644 --- a/app/controllers/epp/commands_controller.rb +++ b/app/controllers/epp/commands_controller.rb @@ -2,6 +2,7 @@ class Epp::CommandsController < ApplicationController include Epp::Common include Epp::DomainsHelper include Epp::ContactsHelper + include Shared::UserStamper private def create diff --git a/app/helpers/epp/contacts_helper.rb b/app/helpers/epp/contacts_helper.rb index 7320a47c6..271b75101 100644 --- a/app/helpers/epp/contacts_helper.rb +++ b/app/helpers/epp/contacts_helper.rb @@ -1,27 +1,19 @@ module Epp::ContactsHelper def create_contact ph = params_hash['epp']['command']['create']['create'] + #todo, remove the first_or_initialize logic, since it's redundant due to + # from EPP api - ph[:ident] ? @contact = Contact.where(ident: ph[:ident]).first_or_initialize : @contact = Contact.new - if @contact.new_record? - @contact.assign_attributes( - code: ph[:id], - phone: ph[:voice], - ident: ph[:ident], - email: ph[:email], - org_name: ph[:postalInfo][:org] - ) - end - @contact.name = ph[:postalInfo][:name] - @contact.ident_type = ident_type + @contact = Contact.new + @contact = Contact.where(ident: ph[:ident]).first_or_initialize( new_contact_info ) if ph[:ident] - @contact.addresses << Address.new( - country_id: Country.find_by(iso: ph[:postalInfo][:cc]), - street: ph[:postalInfo][:street], - zip: ph[:postalInfo][:pc] - ) + @contact.assign_attributes(name: ph[:postalInfo][:name]) + + @contact.addresses << new_address + stamp @contact @contact.save + render '/epp/contacts/create' end @@ -69,6 +61,35 @@ module Epp::ContactsHelper private + def new_address + ph = params_hash['epp']['command']['create']['create'] + + Address.new( + country_id: Country.find_by(iso: ph[:postalInfo][:addr][:cc]), + street: tidy_street, + zip: ph[:postalInfo][:addr][:pc] + ) + end + + def new_contact_info + ph = params_hash['epp']['command']['create']['create'] + { + code: ph[:id], + phone: ph[:voice], + ident: ph[:ident], + ident_type: ident_type, + email: ph[:email], + org_name: ph[:postalInfo][:org] + } + end + + def tidy_street + street = params_hash['epp']['command']['create']['create'][:postalInfo][:addr][:street] + return street if street.is_a? String + return street.join(',') if street.is_a? Array + return nil + end + def ident_type result = params[:frame].slice(/(?<=\