From 8ce1875484964b3e8fd02017943b323e3fc6d0f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andres=20Keskk=C3=BCla?= Date: Mon, 4 Aug 2014 13:15:54 +0300 Subject: [PATCH 1/3] Added general user stamping on contact --- app/controllers/concerns/shared/user_stamper.rb | 15 +++++++++++++++ app/controllers/epp/commands_controller.rb | 1 + app/helpers/epp/contacts_helper.rb | 2 ++ .../20140804095654_add_user_stamps_to_contact.rb | 6 ++++++ db/schema.rb | 4 +++- spec/epp/contact_spec.rb | 6 ++++++ 6 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 app/controllers/concerns/shared/user_stamper.rb create mode 100644 db/migrate/20140804095654_add_user_stamps_to_contact.rb 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..085c002ea 100644 --- a/app/helpers/epp/contacts_helper.rb +++ b/app/helpers/epp/contacts_helper.rb @@ -21,6 +21,8 @@ module Epp::ContactsHelper zip: ph[:postalInfo][:pc] ) + stamp @contact + @contact.save render '/epp/contacts/create' end diff --git a/db/migrate/20140804095654_add_user_stamps_to_contact.rb b/db/migrate/20140804095654_add_user_stamps_to_contact.rb new file mode 100644 index 000000000..cc274ecdf --- /dev/null +++ b/db/migrate/20140804095654_add_user_stamps_to_contact.rb @@ -0,0 +1,6 @@ +class AddUserStampsToContact < ActiveRecord::Migration + def change + add_column :contacts, :created_by_id, :integer + add_column :contacts, :updated_by_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index e7b2b6dfd..6a44334c7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140801140249) do +ActiveRecord::Schema.define(version: 20140804095654) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -39,6 +39,8 @@ ActiveRecord::Schema.define(version: 20140801140249) do t.string "ident" t.string "ident_type" t.string "org_name" + t.integer "created_by_id" + t.integer "updated_by_id" end create_table "countries", force: true do |t| diff --git a/spec/epp/contact_spec.rb b/spec/epp/contact_spec.rb index 6d3ebd2eb..b7c159b1b 100644 --- a/spec/epp/contact_spec.rb +++ b/spec/epp/contact_spec.rb @@ -12,6 +12,8 @@ describe 'EPP Contact', epp: true do expect(response[:result_code]).to eq('1000') expect(response[:msg]).to eq('Command completed successfully') expect(response[:clTRID]).to eq('ABC-12345') + expect(Contact.first.created_by_id).to be 1 + expect(Contact.first.updated_by_id).to be nil expect(Contact.count).to eq(1) expect(Contact.first.org_name).to eq('Example Inc.') @@ -27,6 +29,10 @@ describe 'EPP Contact', epp: true do expect(Contact.first.name).to eq("John Doe") expect(Contact.first.ident_type).to eq("op") + expect(Contact.first.updated_by_id).to be 1 + #nil because we fabricate, hence stamping in controller won't happen + expect(Contact.first.created_by_id).to be nil + expect(Contact.count).to eq(1) end #TODO tests for missing/invalid/etc ident From 2a6a30c2b24b4c46dccd607530948a27e07b2f73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andres=20Keskk=C3=BCla?= Date: Mon, 4 Aug 2014 14:13:30 +0300 Subject: [PATCH 2/3] Fixed address saving for contact, refactored a bit --- app/helpers/epp/contacts_helper.rb | 56 +++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/app/helpers/epp/contacts_helper.rb b/app/helpers/epp/contacts_helper.rb index 085c002ea..f5813985c 100644 --- a/app/helpers/epp/contacts_helper.rb +++ b/app/helpers/epp/contacts_helper.rb @@ -3,27 +3,15 @@ module Epp::ContactsHelper ph = params_hash['epp']['command']['create']['create'] 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.addresses << Address.new( - country_id: Country.find_by(iso: ph[:postalInfo][:cc]), - street: ph[:postalInfo][:street], - zip: ph[:postalInfo][:pc] - ) + @contact.assign_attributes(new_contact_info ) if @contact.new_record? + @contact.assign_attributes(name_and_ident_type) + @contact.addresses << new_address stamp @contact @contact.save + render '/epp/contacts/create' end @@ -71,6 +59,42 @@ 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 name_and_ident_type + ph = params_hash['epp']['command']['create']['create'] + { + name: ph[:postalInfo][:name], + ident_type: ident_type + } + end + + def new_contact_info + ph = params_hash['epp']['command']['create']['create'] + { + code: ph[:id], + phone: ph[:voice], + ident: ph[:ident], + 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(/(?<=\ Date: Mon, 4 Aug 2014 14:49:17 +0300 Subject: [PATCH 3/3] Removed unneccessary method from contacts helper --- app/helpers/epp/contacts_helper.rb | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/app/helpers/epp/contacts_helper.rb b/app/helpers/epp/contacts_helper.rb index f5813985c..271b75101 100644 --- a/app/helpers/epp/contacts_helper.rb +++ b/app/helpers/epp/contacts_helper.rb @@ -1,11 +1,13 @@ 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 + @contact = Contact.new + @contact = Contact.where(ident: ph[:ident]).first_or_initialize( new_contact_info ) if ph[:ident] - @contact.assign_attributes(new_contact_info ) if @contact.new_record? - @contact.assign_attributes(name_and_ident_type) + @contact.assign_attributes(name: ph[:postalInfo][:name]) @contact.addresses << new_address stamp @contact @@ -69,20 +71,13 @@ module Epp::ContactsHelper ) end - def name_and_ident_type - ph = params_hash['epp']['command']['create']['create'] - { - name: ph[:postalInfo][:name], - ident_type: ident_type - } - 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] }