diff --git a/lib/rbac/filterer.rb b/lib/rbac/filterer.rb index 2e170809eaf..442fc2ea085 100644 --- a/lib/rbac/filterer.rb +++ b/lib/rbac/filterer.rb @@ -573,16 +573,18 @@ def lookup_user_group(user, userid, miq_group, miq_group_id) miq_group_id ||= miq_group.try!(:id) return [user, user.current_group] if user && user.current_group_id.to_s == miq_group_id.to_s - if user - if miq_group_id && (detected_group = user.miq_groups.detect { |g| g.id.to_s == miq_group_id.to_s }) - user.current_group = detected_group - elsif miq_group_id && user.super_admin_user? - user.current_group = miq_group || MiqGroup.find_by(:id => miq_group_id) - end - else - miq_group ||= miq_group_id && MiqGroup.find_by(:id => miq_group_id) - end - [user, user.try(:current_group) || miq_group] + group = if user + if miq_group_id && (detected_group = user.miq_groups.detect { |g| g.id.to_s == miq_group_id.to_s }) + user.current_group = detected_group + elsif miq_group_id && user.super_admin_user? + miq_group || MiqGroup.find_by(:id => miq_group_id) + else + user.try(:current_group) + end + else + miq_group || (miq_group_id && MiqGroup.find_by(:id => miq_group_id)) + end + [user, group] end # for reports, user is currently nil, so use the group filter diff --git a/spec/lib/rbac/filterer_spec.rb b/spec/lib/rbac/filterer_spec.rb index 3f97479d19c..85b6a4c6435 100644 --- a/spec/lib/rbac/filterer_spec.rb +++ b/spec/lib/rbac/filterer_spec.rb @@ -2151,6 +2151,14 @@ def get_rbac_results_for_and_expect_objects(klass, expected_objects) _, group = filter.send(:lookup_user_group, admin, nil, nil, random_group.id) expect(group).to eq(random_group) end + + it "does not update user.current_group if user is super admin" do + admin = FactoryGirl.create(:user_admin) + admin_group = admin.current_group + random_group = FactoryGirl.create(:miq_group) + filter.send(:lookup_user_group, admin, nil, nil, random_group.id) + expect(admin.current_group).to eq(admin_group) + end end context "user" do