diff --git a/app/models/mixins/miq_request_mixin.rb b/app/models/mixins/miq_request_mixin.rb index 4d9ec8c9ab0..9ae81ed9012 100644 --- a/app/models/mixins/miq_request_mixin.rb +++ b/app/models/mixins/miq_request_mixin.rb @@ -31,7 +31,7 @@ def get_option_last(key) end def get_user - @user ||= User.find_by(:userid => userid).tap do |u| + @user ||= User.in_my_region.find_by(:userid => userid).tap do |u| u.current_group_by_description = options[:requester_group] if options[:requester_group] end end diff --git a/app/models/user.rb b/app/models/user.rb index 378bf909d26..ea03b6f1082 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -107,7 +107,7 @@ def validate def current_group_by_description=(group_description) if group_description desired_group = miq_groups.detect { |g| g.description == group_description } - desired_group ||= MiqGroup.find_by(:description => group_description) if super_admin_user? + desired_group ||= MiqGroup.in_region(region_id).find_by(:description => group_description) if super_admin_user? self.current_group = desired_group if desired_group end end diff --git a/spec/models/mixins/miq_request_mixin.rb b/spec/models/mixins/miq_request_mixin.rb new file mode 100644 index 00000000000..e63ace579f7 --- /dev/null +++ b/spec/models/mixins/miq_request_mixin.rb @@ -0,0 +1,22 @@ +describe MiqRequestMixin do + let(:test_class) do + Class.new do + attr_accessor :options, :userid + include MiqRequestMixin + + def initialize + @options = {} + end + end + end + + let(:test_instance) { test_class.new } + + it "#get_user only searches users in the current region" do + user = FactoryGirl.create(:user, :userid => "TestUser") + FactoryGirl.create(:user, :userid => "TestUser", :id => ApplicationRecord.id_in_region(1, ApplicationRecord.my_region_number + 1)) + + test_instance.userid = "TestUser" + expect(test_instance.get_user).to eq(user) + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index cfbf667ee43..29184a35474 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -453,6 +453,15 @@ subject.current_group_by_description = g2.description expect(subject.current_group).to eq(g2) end + + it "ignores groups from other regions" do + expect(subject).to be_super_admin_user + + group = FactoryGirl.create(:miq_group, :id => ApplicationRecord.id_in_region(1, ApplicationRecord.my_region_number + 1)) + + subject.current_group_by_description = group.description + expect(subject.current_group.description).not_to eq(group.description) + end end end