diff --git a/app/models/concerns/foreman_puppet/extensions/user.rb b/app/models/concerns/foreman_puppet/extensions/user.rb index a34bd234..c35a48cb 100644 --- a/app/models/concerns/foreman_puppet/extensions/user.rb +++ b/app/models/concerns/foreman_puppet/extensions/user.rb @@ -9,19 +9,7 @@ module User module PatchedMethods def visible_environments - authorized_scope = ForemanPuppet::Environment.unscoped.authorized(:view_environments) - authorized_scope = authorized_scope - .joins(:taxable_taxonomies) - .where('taxable_taxonomies.taxonomy_id' => taxonomy_ids[:organizations] + taxonomy_ids[:locations]) - result = authorized_scope.distinct.pluck(:name) - if ::User.current.admin? - # Admin users can also see Environments that do not have any organization or location, even when - # organizations and locations are enabled. - untaxed_env_ids = TaxableTaxonomy.where(taxable_type: 'ForemanPuppet::Environment').distinct.select(:taxable_id) - untaxed_environments = ForemanPuppet::Environment.unscoped.where.not(id: untaxed_env_ids).pluck(:name) - result += untaxed_environments - end - result + ForemanPuppet::Environment.authorized(:view_environments).pluck(:name) end end end diff --git a/lib/foreman_puppet/register.rb b/lib/foreman_puppet/register.rb index 5d8c751e..0dec86ea 100644 --- a/lib/foreman_puppet/register.rb +++ b/lib/foreman_puppet/register.rb @@ -1,5 +1,5 @@ Foreman::Plugin.register :foreman_puppet do - requires_foreman '>= 3.13' + requires_foreman '>= 3.19' register_gettext # Add Global JS file for extending foreman-core components and routes diff --git a/test/models/foreman_puppet/user_test.rb b/test/models/foreman_puppet/user_test.rb index 98533ab8..6648b6a5 100644 --- a/test/models/foreman_puppet/user_test.rb +++ b/test/models/foreman_puppet/user_test.rb @@ -24,8 +24,12 @@ class UserTest < ActiveSupport::TestCase end test 'should show the list of environments visible as non-admin user' do - # Non-admin user only sees environments in a taxonomy at least + # Non-admin user only sees environments in the same taxonomies at least setup_user 'view', 'environments' + # The user is in the same location as the environment, but in a + # different org. The org mismatch would make the environment invisible + # to the user. + User.current.organizations << environment.organizations.first assert_equal [environment.name], ::User.current.visible_environments end end