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

Reused obsolete RBAC method for fetching single record #1636

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions app/controllers/application_controller/performance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,8 @@ def date_time_running_msg(typ, timestamp)

# Send error message if record is found and authorized, else return the record
def perf_menu_record_valid(model, id)
record = find_records_with_rbac(model.constantize, id)
if record.empty?
record = find_record_with_rbac(model.constantize, id)
if record.present?
add_flash(_("Can't access selected record"))
end
unless @flash_array.blank?
Expand Down
18 changes: 9 additions & 9 deletions app/controllers/ems_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,31 +349,31 @@ def button
elsif params[:pressed] == "host_aggregate_edit"
javascript_redirect :controller => "host_aggregate",
:action => "edit",
:id => find_records_with_rbac(HostAggregate, checked_or_params).first
:id => find_record_with_rbac(HostAggregate, checked_or_params)
elsif params[:pressed] == "cloud_tenant_edit"
javascript_redirect :controller => "cloud_tenant",
:action => "edit",
:id => find_records_with_rbac(CloudTenant, checked_or_params).first
:id => find_record_with_rbac(CloudTenant, checked_or_params)
elsif params[:pressed] == "cloud_volume_new"
javascript_redirect :controller => "cloud_volume",
:action => "new",
:storage_manager_id => params[:id]
elsif params[:pressed] == "cloud_volume_snapshot_create"
javascript_redirect :controller => "cloud_volume",
:action => "snapshot_new",
:id => find_records_with_rbac(CloudVolume, checked_or_params).first
:id => find_record_with_rbac(CloudVolume, checked_or_params)
elsif params[:pressed] == "cloud_volume_attach"
javascript_redirect :controller => "cloud_volume",
:action => "attach",
:id => find_records_with_rbac(CloudVolume, checked_or_params).first
:id => find_record_with_rbac(CloudVolume, checked_or_params)
elsif params[:pressed] == "cloud_volume_detach"
javascript_redirect :controller => "cloud_volume",
:action => "detach",
:id => find_records_with_rbac(CloudVolume, checked_or_params).first
:id => find_record_with_rbac(CloudVolume, checked_or_params)
elsif params[:pressed] == "cloud_volume_edit"
javascript_redirect :controller => "cloud_volume",
:action => "edit",
:id => find_records_with_rbac(CloudVolume, checked_or_params).first
:id => find_record_with_rbac(CloudVolume, checked_or_params)
elsif params[:pressed] == "cloud_volume_delete"
# Clear CloudVolumeController's lastaction, since we are calling the delete_volumes from
# an external controller. This will ensure that the final redirect is properly handled.
Expand All @@ -384,15 +384,15 @@ def button
elsif params[:pressed] == "network_router_edit"
javascript_redirect :controller => "network_router",
:action => "edit",
:id => find_records_with_rbac(NetworkRouter, checked_or_params).first
:id => find_record_with_rbac(NetworkRouter, checked_or_params)
elsif params[:pressed] == "network_router_add_interface"
javascript_redirect :controller => "network_router",
:action => "add_interface_select",
:id => find_records_with_rbac(NetworkRouter, checked_or_params).first
:id => find_record_with_rbac(NetworkRouter, checked_or_params)
elsif params[:pressed] == "network_router_remove_interface"
javascript_redirect :controller => "network_router",
:action => "remove_interface_select",
:id => find_records_with_rbac(NetworkRouter, checked_or_params).first
:id => find_record_with_rbac(NetworkRouter, checked_or_params)
elsif params[:pressed].ends_with?("_edit") || ["#{pfx}_miq_request_new", "#{pfx}_clone",
"#{pfx}_migrate", "#{pfx}_publish"].include?(params[:pressed])
render_or_redirect_partial(pfx)
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/mixins/actions/vm_actions/live_migrate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def live_migrate
@in_a_form = true
@live_migrate = true

@live_migrate_items = find_records_with_rbac(VmOrTemplate, session[:live_migrate_items]).sort_by(&:name)
@live_migrate_items = find_records_with_rbac(VmOrTemplate.order(:name), session[:live_migrate_items])
build_targets_hash(@live_migrate_items)
@view = get_db_view(VmOrTemplate)
@view.table = MiqFilter.records2table(@live_migrate_items, @view.cols + ['id'])
Expand All @@ -32,7 +32,7 @@ def live_migrate

def live_migrate_form_fields
assert_privileges("instance_live_migrate")
@record = find_records_with_rbac(VmOrTemplate, [params[:id]]).first
@record = find_record_with_rbac(VmOrTemplate, params[:id])
hosts = []
unless @record.ext_management_system.nil?
# wrap in a rescue block in the event the connection to the provider fails
Expand Down Expand Up @@ -61,7 +61,7 @@ def live_migrate_vm
when "cancel"
add_flash(_("Live migration of Instances was cancelled by the user"))
when "submit"
@live_migrate_items = find_records_with_rbac(VmOrTemplate, session[:live_migrate_items]).sort_by(&:name)
@live_migrate_items = find_records_with_rbac(VmOrTemplate.order(:name), session[:live_migrate_items])
@live_migrate_items.each do |vm|
if vm.supports_live_migrate?
options = {
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/mixins/actions/vm_actions/retire.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def retire
Vm
end
# Check RBAC for all items in session[:retire_items]
@retireitems = find_records_with_rbac(kls, session[:retire_items]).sort_by(&:name)
@retireitems = find_records_with_rbac(kls.order(:name), session[:retire_items])
if params[:button]
flash = retire_handle_form_buttons(kls)
add_flash(flash)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/mixins/actions/vm_actions/right_size.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def vm_right_size
assert_privileges(params[:pressed])
# check to see if coming from show_list or drilled into vms from another CI
rec_cls = "vm"
record = find_records_with_rbac(VmOrTemplate, checked_or_params).first
record = find_record_with_rbac(VmOrTemplate, checked_or_params)
if record.nil?
add_flash(_("One %{model} must be selected to Right-Size Recommendations") %
{:model => ui_lookup(:table => request.parameters[:controller])}, :error)
Expand Down
44 changes: 19 additions & 25 deletions app/controllers/mixins/checked_id_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,6 @@ def find_checked_records_with_rbac(klass, ids = nil)
filtered
end

# !============================================!
# PLEASE PREFER find_records_with_rbac OVER THIS
# !============================================!
#
# Test RBAC in case there is only one record
# Params:
# klass - class of accessed object
# id - accessed object id
# Returns:
# database record of checked item. If user does not have rights for it,
# raises an exception
def find_record_with_rbac(klass, id, options = {})
raise _("Invalid input") unless is_integer?(id)
tested_object = klass.find_by(:id => id)
if tested_object.nil?
raise(_("Can't access selected records"))
end
Rbac.filtered_object(tested_object, :user => current_user, :named_scope => options[:named_scope]) ||
raise(_("Can't access selected records"))
end

# !============================================!
# PLEASE PREFER find_records_with_rbac OVER THIS
# !============================================!
Expand Down Expand Up @@ -117,8 +96,25 @@ def checked_or_params_id
end

# Find a record by model and id and test it with RBAC
#
# Wrapper for find_records_with_rbac method for case when only a single
# record is required
#
# Params:
# klass - class of accessed object
# id - accessed object id
# options - additional options
#
# Returns:
# Instance of selected item
#
def find_record_with_rbac(klass, id, options = {})
find_records_with_rbac(klass, Array.wrap(id), options).first
end

# Find records by model and id and test it with RBAC
# Params:
# klass - class of accessed objects
# klass - class or scope of accessed objects
# ids - accessed object ids
# options - :named_scope :
#
Expand All @@ -127,9 +123,7 @@ def checked_or_params_id
# either sets flash or raises exception
#
def find_records_with_rbac(klass, ids, options = {})
filtered = Rbac.filtered(klass.where(:id => ids),
:user => current_user,
Copy link
Member Author

@romanblanco romanblanco Jun 30, 2017

Choose a reason for hiding this comment

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

current_user (nor User.current_user) is not necessary, as it loaded in https://github.com/ManageIQ/manageiq/blob/master/lib/rbac/filterer.rb#L496 by default

thanks @lpichler

:named_scope => options[:named_scope])
filtered = Rbac.filtered(klass.where(:id => ids), :named_scope => options[:named_scope])
raise(_("Can't access selected records")) unless ids.length == filtered.length
filtered
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/vm_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ def policy_options

# Set right_size selected db records
def right_size(record = nil)
@record ||= record ? record : find_records_with_rbac(params[:id]).first
@record ||= record ? record : find_record_with_rbac(Vm, params[:id])
@lastaction = "right_size"
@rightsize = true
@in_a_form = true
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
end

it "Verify Invalid input flash error message when invalid id is passed in" do
expect { controller.send(:find_record_with_rbac, ExtManagementSystem, "invalid") }.to raise_error(RuntimeError, "Invalid input")
expect { controller.send(:find_record_with_rbac, ExtManagementSystem, "invalid") }.to raise_error(RuntimeError, "Can't access selected records")
end

it "Verify flash error message when passed in id no longer exists in database" do
Expand Down
1 change: 1 addition & 0 deletions spec/controllers/container_image_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
ApplicationController.handle_exceptions = true

expect(controller).to receive(:check_compliance).and_call_original
expect(controller).to receive(:add_flash).with("Error: Record no longer exists in the database", :error, true)
expect(controller).to receive(:add_flash).with("Container Image no longer exists", :error)
expect(controller).to receive(:add_flash).with("No Container Images were selected for Compliance Check", :error)
post :button, :params => { :pressed => 'container_image_check_compliance', :format => :js }
Expand Down