diff --git a/app/models/mixins/miq_request_mixin.rb b/app/models/mixins/miq_request_mixin.rb index 4e11c3c3f3a..4d9ec8c9ab0 100644 --- a/app/models/mixins/miq_request_mixin.rb +++ b/app/models/mixins/miq_request_mixin.rb @@ -31,7 +31,9 @@ def get_option_last(key) end def get_user - @user ||= User.find_by_userid(userid) + @user ||= User.find_by(:userid => userid).tap do |u| + u.current_group_by_description = options[:requester_group] if options[:requester_group] + end end alias_method :tenant_identity, :get_user diff --git a/spec/models/miq_request_spec.rb b/spec/models/miq_request_spec.rb index 6f74e8b98b5..514f16037ee 100644 --- a/spec/models/miq_request_spec.rb +++ b/spec/models/miq_request_spec.rb @@ -412,4 +412,24 @@ expect { described_class.class_from_request_data({}) }.to raise_error("Invalid request_type") end end + + context "#get_user" do + let(:root_tenant) { Tenant.seed } + let(:tenant1) { FactoryGirl.create(:tenant, :parent => root_tenant) } + let(:group1) { FactoryGirl.create(:miq_group, :description => 'Group 1', :tenant => root_tenant) } + let(:group2) { FactoryGirl.create(:miq_group, :description => 'Group 2', :tenant => tenant1) } + let(:user) { FactoryGirl.create(:user, :miq_groups => [group1, group2], :current_group => group1) } + + it "takes the requester group" do + request = FactoryGirl.create(:miq_provision_request, :requester => user, :options => {:requester_group => group2.description}) + expect(user.current_group).to eq(group1) + expect(request.get_user.current_group).to eq(group2) + end + + it "stays with user's current group" do + request = FactoryGirl.create(:miq_provision_request, :requester => user) + expect(user.current_group).to eq(group1) + expect(request.get_user.current_group).to eq(group1) + end + end end