diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 318923e3d..5d051377d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -67,8 +67,7 @@ class ApplicationController < ActionController::Base end def user_log_str(user) - return 'public' if user.nil? - "#{user.id}-#{user.class}: #{user.username}" + user.nil? ? 'public' : user.id_role_username end def comma_support_for(parent_key, key) diff --git a/app/models/concerns/user_events.rb b/app/models/concerns/user_events.rb index 12ff18444..25a225e8b 100644 --- a/app/models/concerns/user_events.rb +++ b/app/models/concerns/user_events.rb @@ -1,23 +1,30 @@ module UserEvents extend ActiveSupport::Concern - # TODO: remove old - # module ClassMethods - # def registrar_events(id) - # registrar = Registrar.find(id) - # return [] unless registrar - # @events = [] - # registrar.users.each { |user| @events << user_events(user.id) } - # registrar.epp_users.each { |user| @events << epp_user_events(user.id) } - # @events - # end + included do + # EPP requires a server defined creator ID, which should be registrar code if we have one + def cr_id + # try this, rebuild user for registrar before searching history? really? + registrar = self.creator.try(:registrar) + if registrar.present? # Did creator return a kind of User that has a registrar? + registrar.code + else + if self.versions.first.try(:object).nil? + changes = self.versions.first.try(:object_changes) + cr_registrar_id = changes['registrar_id'].second if changes.present? + else + # untested, expected never to execute + cr_registrar_id = self.versions.first.object['registrar_id'] + end - # def user_events(id) - # where(whodunnit: id.to_s) - # end + if cr_registrar_id.present? + Registrar.find(cr_registrar_id).code + else + # cr_id optional for domain, but required for contact; but we want something here anyway + self.creator_str # Fallback if we failed, maybe we can find a string here + end + end + end + end - # def epp_user_events(id) - # where(whodunnit: "#{id}-EppUser") - # end - # end end diff --git a/app/models/concerns/versions.rb b/app/models/concerns/versions.rb index 2cbdca838..768cf021e 100644 --- a/app/models/concerns/versions.rb +++ b/app/models/concerns/versions.rb @@ -22,32 +22,30 @@ module Versions def creator return nil if creator_str.blank? - - if creator_str =~ /^\d+-AdminUser:/ - creator = AdminUser.find_by(id: creator_str) - elsif creator_str =~ /^\d+-ApiUser:/ - creator = ApiUser.find_by(id: creator_str) - elsif creator_str =~ /^\d+-api-/ # depricated - creator = ApiUser.find_by(id: creator_str) - end - + creator = user_from_id_role_username creator_str creator.present? ? creator : creator_str end def updator return nil if updator_str.blank? - - if updator_str =~ /^\d+-AdminUser:/ - updator = AdminUser.find_by(id: updator_str) - elsif updator_str =~ /^\d+-ApiUser:/ - updator = ApiUser.find_by(id: updator_str) - elsif updator_str =~ /^\d+-api-/ # depricated - updator = ApiUser.find_by(id: updator_str) - end - + updator = user_from_id_role_username updator_str updator.present? ? updator : updator_str end + def user_from_id_role_username(str) + user = ApiUser.find_by(id: $1) if str =~ /^(\d+)-(ApiUser:|api-)/ + unless user.present? + user = AdminUser.find_by(id: $1) if str =~ /^(\d+)-AdminUser:/ + unless user.present? + # on import we copied Registrar name, which may eql code + registrar = Registrar.find_by(name: str) + # assume each registrar has only one user + user = registrar.api_users.first if registrar + end + end + user + end + # callbacks def touch_domain_version domain.try(:touch_with_version) diff --git a/app/models/contact.rb b/app/models/contact.rb index c933c2751..96365d056 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -1,6 +1,7 @@ class Contact < ActiveRecord::Base include Versions # version/contact_version.rb include EppErrors + include UserEvents belongs_to :registrar has_many :domain_contacts diff --git a/app/models/domain.rb b/app/models/domain.rb index 8bfc0166b..988cd43e3 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -1,5 +1,6 @@ # rubocop: disable Metrics/ClassLength class Domain < ActiveRecord::Base + include UserEvents include Versions # version/domain_version.rb include Statuses has_paper_trail class_name: "DomainVersion", meta: { children: :children_log } diff --git a/app/models/epp/domain.rb b/app/models/epp/domain.rb index 739c55bec..40b374003 100644 --- a/app/models/epp/domain.rb +++ b/app/models/epp/domain.rb @@ -506,7 +506,9 @@ class Epp::Domain < Domain frame = Nokogiri::XML(pending_json['frame']) statuses.delete(DomainStatus::PENDING_UPDATE) yield(self) if block_given? # need to skip statuses check here + self.save + ::PaperTrail.whodunnit = user.id_role_username # updator str should be the request originator not the approval user return unless update(frame, user, false) clean_pendings! self.deliver_emails = true # turn on email delivery diff --git a/app/models/user.rb b/app/models/user.rb index 0beb174f3..b69e0250c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,4 +3,9 @@ class User < ActiveRecord::Base devise :trackable, :timeoutable attr_accessor :phone + + def id_role_username + "#{self.id}-#{self.class}: #{self.username}" + end + end diff --git a/app/models/version/contact_version.rb b/app/models/version/contact_version.rb index 987dbc1fd..781813302 100644 --- a/app/models/version/contact_version.rb +++ b/app/models/version/contact_version.rb @@ -2,8 +2,6 @@ class ContactVersion < PaperTrail::Version include VersionSession self.table_name = :log_contacts self.sequence_name = :log_contacts_id_seq - - # include UserEvents - + # scope :deleted, -> { where(event: 'destroy') } end diff --git a/app/models/version/domain_version.rb b/app/models/version/domain_version.rb index 5bc25f1e1..2986d7811 100644 --- a/app/models/version/domain_version.rb +++ b/app/models/version/domain_version.rb @@ -4,7 +4,5 @@ class DomainVersion < PaperTrail::Version self.table_name = :log_domains self.sequence_name = :log_domains_id_seq - include UserEvents - scope :deleted, -> { where(event: 'destroy') } end diff --git a/app/views/epp/contacts/info.xml.builder b/app/views/epp/contacts/info.xml.builder index 18019208a..39aa91b39 100644 --- a/app/views/epp/contacts/info.xml.builder +++ b/app/views/epp/contacts/info.xml.builder @@ -46,15 +46,15 @@ xml.epp_head do xml.tag!('contact:email', 'No access') end - xml.tag!('contact:clID', @contact.registrar.try(:name)) - if @contact.creator.try(:registrar).blank? && Rails.env.test? - xml.tag!('contact:crID', 'TEST-CREATOR') - else - xml.tag!('contact:crID', @contact.creator.try(:registrar)) - end + xml.tag!('contact:clID', @contact.registrar.try(:code)) + + xml.tag!('contact:crID', @contact.cr_id) xml.tag!('contact:crDate', @contact.created_at.try(:iso8601)) + if @contact.updated_at != @contact.created_at - xml.tag!('contact:upID', @contact.updator.try(:registrar)) + upID = @contact.updator.try(:registrar) + upID = upID.code if upID.present? # Did updator return a kind of User that has a registrar? + xml.tag!('contact:upID', upID) if upID.present? # optional upID xml.tag!('contact:upDate', @contact.updated_at.try(:iso8601)) end # xml.tag!('contact:trDate', '123') if false diff --git a/app/views/epp/domains/info.xml.builder b/app/views/epp/domains/info.xml.builder index 9e1779921..ec5947b13 100644 --- a/app/views/epp/domains/info.xml.builder +++ b/app/views/epp/domains/info.xml.builder @@ -36,19 +36,20 @@ xml.epp_head do ## TODO Find out what this domain:host is all about - xml.tag!('domain:clID', @domain.registrar_name) - - xml.tag!('domain:crID', @domain.creator.try(:registrar)) if @domain.creator + xml.tag!('domain:clID', @domain.registrar.code) + xml.tag!('domain:crID', @domain.cr_id) xml.tag!('domain:crDate', @domain.created_at.try(:iso8601)) - xml.tag!('domain:upDate', @domain.updated_at.try(:iso8601)) if @domain.updated_at != @domain.created_at + if @domain.updated_at != @domain.created_at + upID = @domain.updator.try(:registrar) + upID = upID.code if upID.present? # Did updator return a kind of User that has a registrar? + xml.tag!('domain:upID', upID) if upID.present? # optional upID + xml.tag!('domain:upDate', @domain.updated_at.try(:iso8601)) + end xml.tag!('domain:exDate', @domain.valid_to.try(:iso8601)) - # TODO Make domain stampable - #xml.tag!('domain:upID', @domain.updated_by) - # TODO Make domain transferrable #xml.tag!('domain:trDate', @domain.transferred_at) if @domain.transferred_at