diff --git a/app/models/miq_report.rb b/app/models/miq_report.rb index 208b0e5d90ce..ab03217f1759 100644 --- a/app/models/miq_report.rb +++ b/app/models/miq_report.rb @@ -59,7 +59,7 @@ class MiqReport < ApplicationRecord all else where( - arel_table[:rpt_type].eq('Custom').and(arel_table[:miq_group_id].eq(user.current_group_id)) + arel_table[:rpt_type].eq('Custom').and(arel_table[:miq_group_id].in(user.current_tenant.miq_groups.pluck(:id))) .or( arel_table[:rpt_type].eq('Default') ) diff --git a/spec/models/miq_report_spec.rb b/spec/models/miq_report_spec.rb index 7c91bdfc7c0c..5a61d05f2ddc 100644 --- a/spec/models/miq_report_spec.rb +++ b/spec/models/miq_report_spec.rb @@ -40,6 +40,34 @@ end describe MiqReport do + context ".for_user" do + let(:my_user) { FactoryGirl.create(:user_with_group) } + let(:group_in_my_tenant) { FactoryGirl.create(:miq_group, :tenant => my_user.current_tenant) } + + let(:other_tenant) { FactoryGirl.create(:tenant) } + let(:group_in_other_tenant) { FactoryGirl.create(:miq_group, :tenant => other_tenant) } + + let(:my_report) { FactoryGirl.create(:miq_report, :miq_group => my_user.current_group, :rpt_type => "Custom") } + let(:report_in_my_tenant) { FactoryGirl.create(:miq_report, :miq_group => group_in_my_tenant, :rpt_type => "Custom") } + let(:report_in_another_tenant) { FactoryGirl.create(:miq_report, :miq_group => group_in_other_tenant, :rpt_type => "Custom") } + + it "returns reports created by me or anyone in a group in my tenant" do + my_report + report_in_my_tenant + report_in_another_tenant + User.current_user = my_user + + expect(described_class.for_user(my_user)).to match_array([my_report, report_in_my_tenant] ) + end + + it "does not return reports created by a group in another tenant" do + report_in_another_tenant + User.current_user = my_user + + expect(described_class.for_user(my_user)).to match_array([] ) + end + end + context "report with filtering in Registry" do let(:options) { {:targets_hash => true, :userid => "admin"} } let(:miq_task) { FactoryGirl.create(:miq_task) }