Skip to content

Commit

Permalink
Scope User and MiqGroup searches within the current region
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunne committed Jan 5, 2018
1 parent a0fb84a commit faaf0f7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/models/mixins/miq_request_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def get_option_last(key)
end

def get_user
@user ||= User.find_by(:userid => userid).tap do |u|
@user ||= User.in_my_region.find_by(:userid => userid).tap do |u|
u.current_group_by_description = options[:requester_group] if options[:requester_group]
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def validate
def current_group_by_description=(group_description)
if group_description
desired_group = miq_groups.detect { |g| g.description == group_description }
desired_group ||= MiqGroup.find_by(:description => group_description) if super_admin_user?
desired_group ||= MiqGroup.in_region(region_id).find_by(:description => group_description) if super_admin_user?
self.current_group = desired_group if desired_group
end
end
Expand Down
22 changes: 22 additions & 0 deletions spec/models/mixins/miq_request_mixin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
describe MiqRequestMixin do
let(:test_class) do
Class.new do
attr_accessor :options, :userid
include MiqRequestMixin

def initialize
@options = {}
end
end
end

let(:test_instance) { test_class.new }

it "#get_user only searches users in the current region" do
user = FactoryGirl.create(:user, :userid => "TestUser")
FactoryGirl.create(:user, :userid => "TestUser", :id => ApplicationRecord.id_in_region(1, ApplicationRecord.my_region_number + 1))

test_instance.userid = "TestUser"
expect(test_instance.get_user).to eq(user)
end
end
9 changes: 9 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,15 @@
subject.current_group_by_description = g2.description
expect(subject.current_group).to eq(g2)
end

it "ignores groups from other regions" do
expect(subject).to be_super_admin_user

group = FactoryGirl.create(:miq_group, :id => ApplicationRecord.id_in_region(1, ApplicationRecord.my_region_number + 1))

subject.current_group_by_description = group.description
expect(subject.current_group.description).not_to eq(group.description)
end
end
end

Expand Down

0 comments on commit faaf0f7

Please sign in to comment.