diff --git a/app/models/miq_group.rb b/app/models/miq_group.rb index e8c57bdf82b..6bc63dc4d07 100644 --- a/app/models/miq_group.rb +++ b/app/models/miq_group.rb @@ -223,6 +223,14 @@ def ordered_widget_sets end end + def regional_groups + self.class.regional_groups(self) + end + + def self.regional_groups(group) + where(arel_table.grouping(Arel::Nodes::NamedFunction.new("LOWER", [arel_attribute(:description)]).eq(group.description.downcase))) + end + def self.create_tenant_group(tenant) tenant_full_name = (tenant.ancestors.map(&:name) + [tenant.name]).join("/") diff --git a/app/models/miq_request.rb b/app/models/miq_request.rb index 5095e9bf526..5faf92e0d5d 100644 --- a/app/models/miq_request.rb +++ b/app/models/miq_request.rb @@ -122,11 +122,11 @@ def self.user_or_group_owned(user, miq_group) end def self.user_owned(user) - where(:requester_id => user.id) + where(:requester_id => user.regional_users.select(:id)) end def self.group_owned(miq_group) - where(:requester_id => miq_group.user_ids) + where(:requester_id => miq_group.regional_groups.joins(:users).select("users.id")) end # Supports old-style requests where specific request was a seperate table connected as a resource diff --git a/app/models/user.rb b/app/models/user.rb index 7ad2801f280..d04afe2332e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -237,6 +237,14 @@ def accessible_vms end end + def regional_users + self.class.regional_users(self) + end + + def self.regional_users(user) + where(arel_table.grouping(Arel::Nodes::NamedFunction.new("LOWER", [arel_attribute(:userid)]).eq(user.userid.downcase))) + end + def self.super_admin in_my_region.find_by_userid("admin") end diff --git a/spec/models/miq_group_spec.rb b/spec/models/miq_group_spec.rb index cc2199f79a6..c0900da9a10 100644 --- a/spec/models/miq_group_spec.rb +++ b/spec/models/miq_group_spec.rb @@ -546,4 +546,17 @@ expect(User.find_by(:id => user2).current_group.id).to eq(testgroup3.id) end end + + describe "#regional_groups" do + let(:other_id) { ApplicationRecord.id_in_region(1, ApplicationRecord.my_region_number + 1) } + let(:group) { FactoryGirl.create(:miq_group) } + let!(:regional_group) { FactoryGirl.create(:miq_group, :description => group.description.upcase, :id => other_id) } + + it "finds regional groups" do + FactoryGirl.create(:miq_group) # ensure these doen't come back + FactoryGirl.create(:miq_group, :id => other_id + 1) + + expect(group.regional_groups).to match_array([group, regional_group]) + end + end end diff --git a/spec/models/miq_request_spec.rb b/spec/models/miq_request_spec.rb index 051d4ba9ec5..1b400e65777 100644 --- a/spec/models/miq_request_spec.rb +++ b/spec/models/miq_request_spec.rb @@ -476,4 +476,32 @@ expect(request.options[:abc]).to eq(1) end end + + describe ".user_owned" do + let(:regional_id) { ApplicationRecord.id_in_region(1, ApplicationRecord.my_region_number + 1) } + let(:user) { FactoryGirl.create(:user) } + let!(:regional_user) { FactoryGirl.create(:user, :userid => user.userid.upcase, :id => regional_id) } + let!(:request) { FactoryGirl.create(:vm_migrate_request, :requester => user) } + let!(:regional_request) { FactoryGirl.create(:vm_migrate_request, :requester => regional_user) } + it "finds request for cross region users" do + FactoryGirl.create(:vm_migrate_request, :requester => FactoryGirl.create(:user)) + FactoryGirl.create(:vm_migrate_request, :requester => FactoryGirl.create(:user), :id => regional_id + 1) + expect(MiqRequest.user_owned(regional_user)).to match_array([request, regional_request]) + end + end + + describe ".group_owned" do + let(:regional_id) { ApplicationRecord.id_in_region(1, ApplicationRecord.my_region_number + 1) } + let(:group) { FactoryGirl.create(:miq_group) } + let(:regional_group) { FactoryGirl.create(:miq_group, :id => regional_id) } + let(:user) { FactoryGirl.create(:user, :miq_groups => [group]) } + let!(:regional_user) { FactoryGirl.create(:user, :userid => user.userid.upcase, :id => regional_id, :miq_groups => [regional_group]) } + let!(:request) { FactoryGirl.create(:vm_migrate_request, :requester => user) } + let!(:regional_request) { FactoryGirl.create(:vm_migrate_request, :requester => regional_user) } + it "finds request for cross region groups" do + FactoryGirl.create(:vm_migrate_request, :requester => FactoryGirl.create(:user)) + FactoryGirl.create(:vm_migrate_request, :requester => FactoryGirl.create(:user), :id => regional_id + 1) + expect(MiqRequest.user_owned(regional_user)).to match_array([request, regional_request]) + end + end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 06f6bc9d05e..ca9ece3dee1 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -591,4 +591,17 @@ expect(User.authorize_user("admin")).to be_nil end end + + describe "#regional_users" do + let(:other_id) { ApplicationRecord.id_in_region(1, ApplicationRecord.my_region_number + 1) } + let(:user) { FactoryGirl.create(:user) } + let!(:regional_user) { FactoryGirl.create(:user, :userid => user.userid.upcase, :id => other_id) } + + it "finds regional users" do + FactoryGirl.create(:user) # ensure these doen't come back + FactoryGirl.create(:user, :id => other_id + 1) + + expect(user.regional_users).to match_array([user, regional_user]) + end + end end