From 9f9bffae57fbeccee57d08049ec0716ec142ef41 Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Mon, 16 Mar 2015 12:33:27 +0200 Subject: [PATCH] Always generate new code when transferring contacts --- app/models/contact.rb | 6 ++++-- app/models/epp/domain.rb | 16 +++++++++++++--- spec/epp/domain_spec.rb | 31 +++++++++++++++++++++++++------ 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/app/models/contact.rb b/app/models/contact.rb index a1f2a89ea..e99280c36 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -13,6 +13,8 @@ class Contact < ActiveRecord::Base accepts_nested_attributes_for :address, :disclosure, :legal_documents + attr_accessor :code_overwrite_allowed + validates :name, :phone, :email, :ident, :address, :registrar, :ident_type, presence: true # # Phone nr validation is very minimam in order to support legacy requirements @@ -112,7 +114,7 @@ class Contact < ActiveRecord::Base end def generate_code - self.code = SecureRandom.hex(4) if code.blank? + self.code = SecureRandom.hex(4) if code.blank? || code_overwrite_allowed end def generate_auth_info @@ -129,7 +131,7 @@ class Contact < ActiveRecord::Base end def code=(code) - self[:code] = code if new_record? + self[:code] = code if new_record? || code_overwrite_allowed end # Find a way to use self.domains with contact diff --git a/app/models/epp/domain.rb b/app/models/epp/domain.rb index 6d814d91e..280ee4ef6 100644 --- a/app/models/epp/domain.rb +++ b/app/models/epp/domain.rb @@ -416,7 +416,7 @@ class Epp::Domain < Domain is_other_domains_contact = DomainContact.where('contact_id = ? AND domain_id != ?', owner_contact_id, id).count > 0 if owner_contact.domains_owned.count > 1 || is_other_domains_contact # copy contact - c = Contact.find(owner_contact_id) # n+1 workaround + c = Contact.find(owner_contact_id) oc = c.deep_clone include: [:statuses, :address] oc.code = nil oc.registrar_id = registrar_id @@ -424,7 +424,13 @@ class Epp::Domain < Domain self.owner_contact_id = oc.id else # transfer contact - owner_contact.update_column(:registrar_id, registrar_id) # n+1 workaround + oc = Contact.find(owner_contact_id) # n+1 workaround + oc.code_overwrite_allowed = true + oc.generate_code + oc.registrar_id = registrar_id + oc.save! + + self.owner_contact = oc end end @@ -452,7 +458,11 @@ class Epp::Domain < Domain copied_ids << c.id else # transfer contact - c.update_column(:registrar_id, registrar_id) # n+1 workaround + cnt = Contact.find(c.id) # n+1 workaround + cnt.code_overwrite_allowed = true + cnt.generate_code + cnt.registrar_id = registrar_id + cnt.save! end end end diff --git a/spec/epp/domain_spec.rb b/spec/epp/domain_spec.rb index eaa7d4431..952cbf867 100644 --- a/spec/epp/domain_spec.rb +++ b/spec/epp/domain_spec.rb @@ -872,6 +872,9 @@ describe 'EPP Domain', epp: true do it 'transfers domain with contacts' do original_oc_id = domain.owner_contact.id + original_oc_code = domain.owner_contact.code + + original_contact_codes = domain.contacts.pluck(:code) pw = domain.auth_info xml = domain_transfer_xml({ @@ -890,14 +893,21 @@ describe 'EPP Domain', epp: true do domain.owner_contact.registrar_id.should == @registrar2.id domain.owner_contact.id.should == original_oc_id - domain.contacts.pluck(:registrar_id).each do |reg_id| - reg_id.should == @registrar2.id + # must generate new code + domain.owner_contact.code.should_not == original_oc_code + + domain.contacts.each do |c| + c.registrar_id.should == @registrar2.id + original_contact_codes.include?(c.code).should_not == true end end it 'transfers domain when registrant has more domains' do Fabricate(:domain, owner_contact: domain.owner_contact) original_oc_id = domain.owner_contact.id + original_oc_code = domain.owner_contact.code + + original_contact_codes = domain.contacts.pluck(:code) pw = domain.auth_info xml = domain_transfer_xml({ @@ -916,9 +926,12 @@ describe 'EPP Domain', epp: true do domain.owner_contact.registrar_id.should == @registrar2.id # owner_contact should be a new record domain.owner_contact.id.should_not == original_oc_id + # must generate new code + domain.owner_contact.code.should_not == original_oc_code - domain.contacts.pluck(:registrar_id).each do |reg_id| - reg_id.should == @registrar2.id + domain.contacts.each do |c| + c.registrar_id.should == @registrar2.id + original_contact_codes.include?(c.code).should_not == true end end @@ -927,6 +940,9 @@ describe 'EPP Domain', epp: true do d.tech_contacts << domain.owner_contact original_oc_id = domain.owner_contact.id + original_oc_code = domain.owner_contact.code + + original_contact_codes = domain.contacts.pluck(:code) pw = domain.auth_info xml = domain_transfer_xml({ @@ -945,9 +961,12 @@ describe 'EPP Domain', epp: true do domain.owner_contact.registrar_id.should == @registrar2.id # owner_contact should be a new record domain.owner_contact.id.should_not == original_oc_id + # must generate new code + domain.owner_contact.code.should_not == original_oc_code - domain.contacts.pluck(:registrar_id).each do |reg_id| - reg_id.should == @registrar2.id + domain.contacts.each do |c| + c.registrar_id.should == @registrar2.id + original_contact_codes.include?(c.code).should_not == true end end