commit a525db23e8ae9e12f95f4e7fce1377fa270efb46
Author: Stephen Benjamin <stephen@redhat.com>
Date:   Wed Apr 25 09:10:30 2018 -0400

    fixes #23232 - fix error when domain not in taxonomy
    
    This ensures that if domain_id is set, but domain not found (for example
    when domain is in another taxonomy), the shortname still removes the
    domain from the host's FQDN.

diff --git a/app/models/nic/base.rb b/app/models/nic/base.rb
index 2309e8529..4ce9286a9 100644
--- a/app/models/nic/base.rb
+++ b/app/models/nic/base.rb
@@ -116,7 +116,15 @@ module Nic
     end
 
     def shortname
-      domain.nil? ? name : name.to_s.chomp("." + domain.name)
+      if domain
+        name.to_s.chomp("." + domain.name)
+      elsif domain_id && (unscoped_domain = Domain.unscoped.find_by(id: domain_id))
+        # If domain is nil, but domain_id is set, domain could be
+        # in another taxonomy.  Don't fail to create a correct shortname.
+        name.to_s.chomp("." + unscoped_domain.name)
+      else
+        name
+      end
     end
 
     def validated?
diff --git a/app/models/nic/interface.rb b/app/models/nic/interface.rb
index 0698e5f32..c27a9335f 100644
--- a/app/models/nic/interface.rb
+++ b/app/models/nic/interface.rb
@@ -78,7 +78,7 @@ module Nic
     end
 
     def fqdn_before_last_save
-      domain_before_last_save = Domain.find(domain_id_before_last_save) if domain_id_before_last_save.present?
+      domain_before_last_save = Domain.unscoped.find(domain_id_before_last_save) if domain_id_before_last_save.present?
       return name_before_last_save if name_before_last_save.blank? || domain_before_last_save.blank?
       name_before_last_save.include?('.') ? name_before_last_save : "#{name_before_last_save}.#{domain_before_last_save}"
     end
@@ -124,10 +124,10 @@ module Nic
       return if name.empty?
       if domain_id.nil? && name.include?('.') && changed_attributes['domain_id'].blank?
         # try to assign the domain automatically based on our existing domains from the host FQDN
-        self.domain = Domain.find_by(:name => name.partition('.')[2])
+        self.domain = Domain.unscoped.find_by(:name => name.partition('.')[2])
       elsif persisted? && changed_attributes['domain_id'].present?
         # if we've just updated the domain name, strip off the old one
-        old_domain = Domain.find(changed_attributes["domain_id"])
+        old_domain = Domain.unscoped.find(changed_attributes["domain_id"])
         # Remove the old domain, until fqdn will be set as the full name
         self.name = self.name.chomp('.' + old_domain.to_s)
       end
diff --git a/test/models/hosts/base_test.rb b/test/models/hosts/base_test.rb
index a6efaf640..2e2cf74a8 100644
--- a/test/models/hosts/base_test.rb
+++ b/test/models/hosts/base_test.rb
@@ -75,5 +75,17 @@ module Host
       assert host.primary_interface
       assert_equal 1, host.interfaces.size
     end
+
+    test 'shortname periods check considers domain outside taxonomy scope' do
+      host_org = FactoryBot.create(:organization)
+      other_org = FactoryBot.create(:organization)
+
+      domain = FactoryBot.create(:domain, organizations: [other_org])
+      refute domain.organizations.include?(host_org)
+
+      host = FactoryBot.create(:host, organization: host_org, domain_id: domain.id)
+      host = Host.find(host.id)
+      assert host.valid?
+    end
   end
 end
diff --git a/test/models/nic_test.rb b/test/models/nic_test.rb
index 85f3dad28..fefaa221a 100644
--- a/test/models/nic_test.rb
+++ b/test/models/nic_test.rb
@@ -465,7 +465,6 @@ class NicTest < ActiveSupport::TestCase
     nic_name = 'hostname.sub.bigdomain'
     interface = FactoryBot.build_stubbed(:nic_managed, :name => nic_name)
     subdomain = FactoryBot.create(:domain, :name => 'sub.bigdomain')
-    Domain.expects(:find_by).with(:name => subdomain.name).returns(subdomain)
     interface.send(:normalize_name)
     assert_equal subdomain, interface.domain
   end
@@ -474,9 +473,6 @@ class NicTest < ActiveSupport::TestCase
     nic_name = 'hostname.undefined-subdomain.bigdomain'
     FactoryBot.create(:domain, :name => 'bigdomain')
     interface = FactoryBot.build_stubbed(:nic_managed, :name => nic_name)
-    Domain.expects(:find_by).
-      with(:name => 'undefined-subdomain.bigdomain').
-      returns(nil)
     interface.send(:normalize_name)
     refute interface.domain
   end
