Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not change current_group for super admin user when executing Rbac#lookup_user_group #17347

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/rbac/filterer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

found_group = user.try(:current_group)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line needed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it belongs in the missing else before line 583 instead? Then it can be found_group = if... and drop all of the variable setting below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bdunne 👍 it looks much cleaner

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
found_group = detected_group
elsif miq_group_id && user.super_admin_user?
user.current_group = miq_group || MiqGroup.find_by(:id => miq_group_id)
found_group = miq_group || MiqGroup.find_by(:id => miq_group_id)
end
else
miq_group ||= miq_group_id && MiqGroup.find_by(:id => miq_group_id)
found_group = miq_group || (miq_group_id && MiqGroup.find_by(:id => miq_group_id))
end
[user, user.try(:current_group) || miq_group]
[user, found_group]
end

# for reports, user is currently nil, so use the group filter
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/rbac/filterer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down