Skip to content

Commit

Permalink
Merge pull request #16089 from mkanoor/run_with_user
Browse files Browse the repository at this point in the history
Added user_id, group_id, tenant_id
  • Loading branch information
Fryguy authored Oct 3, 2017
2 parents c187deb + 21404dc commit 874cad6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
10 changes: 7 additions & 3 deletions app/models/miq_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,7 @@ def deliver(requester = nil)
begin
status = STATUS_OK
message = "Message delivered successfully"
Timeout.timeout(msg_timeout) do
result = obj.send(method_name, *args)
end
result = User.with_user_group(user_id, group_id) { dispatch_method(obj, args) }
rescue MiqException::MiqQueueRetryLater => err
unget(err.options)
message = "Message not processed. Retrying #{err.options[:deliver_on] ? "at #{err.options[:deliver_on]}" : 'immediately'}"
Expand All @@ -428,6 +426,12 @@ def deliver(requester = nil)
return status, message, result
end

def dispatch_method(obj, args)
Timeout.timeout(msg_timeout) do
obj.send(method_name, *args)
end
end

DELIVER_IN_ERROR_MSG = 'Deliver in error'.freeze
def delivered_in_error(msg = nil)
delivered('error', msg || DELIVER_IN_ERROR_MSG, nil)
Expand Down
12 changes: 12 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,18 @@ def self.with_user(user, userid = nil)
Thread.current[:userid] = saved_userid
end

def self.with_user_group(user, group, &block)
return yield if user.nil?
user = User.find(user) unless user.kind_of?(User)
if group && group.kind_of?(MiqGroup)
user.current_group = group
elsif group != user.current_group_id
group = MiqGroup.find_by(:id => group)
user.current_group = group if group
end
User.with_user(user, &block)
end

def self.current_user=(user)
Thread.current[:userid] = user.try(:userid)
Thread.current[:user] = user
Expand Down
22 changes: 22 additions & 0 deletions spec/models/miq_queue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@
end
end

describe "user_context" do
it "sets the User.current_user" do
user = FactoryGirl.create(:user_with_group, :name => 'Freddy Kreuger')
msg = FactoryGirl.create(:miq_queue, :state => MiqQueue::STATE_DEQUEUE,
:handler => @miq_server,
:class_name => 'Storage',
:method_name => 'foobar',
:user_id => user.id,
:args => [1, 2, 3],
:group_id => user.current_group.id,
:tenant_id => user.current_tenant.id)
expect(Storage).to receive(:foobar) do
expect(User.current_user.name).to eq(user.name)
expect(User.current_user.current_group.id).to eq(user.current_user.current_group.id)
expect(User.current_user.current_tenant.id).to eq(user.current_user.current_tenant.id)
expect(args).to eq([1, 2, 3])
end

msg.deliver
end
end

describe "#check_for_timeout" do
it "will destroy a dequeued message when it times out" do
handler = FactoryGirl.create(:miq_ems_refresh_worker)
Expand Down
19 changes: 19 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,25 @@
end
end

describe "#with_user_group" do
let(:user1) { FactoryGirl.create(:user_with_group) }
let(:user2) { FactoryGirl.create(:user_with_group) }

it "sets the user and group" do
User.with_user_group(user1, user2.current_group_id) do
expect(User.current_userid).to eq(user1.userid)
expect(User.current_user).to eq(user1)
expect(User.current_user.current_group).to eq(user2.current_group)
User.with_user_group(user2, user1.current_group) do
expect(User.current_userid).to eq(user2.userid)
expect(User.current_user).to eq(user2)
end
expect(User.current_userid).to eq(user1.userid)
expect(User.current_user).to eq(user1)
end
end
end

context ".super_admin" do
it "has super_admin" do
FactoryGirl.create(:miq_group, :role => "super_administrator")
Expand Down

0 comments on commit 874cad6

Please sign in to comment.