Masquerade

Run a block of code as another user/group.

Installation

# gem install masquerade

Usage

To run a block of code as user “roger” and group “developer”:

Masquerade.as :user => "roger", :group => "developer" do
  puts "Hello world"
end

Alternatively, you can use the uid and gid directly:

Masquerade.as :user => 509, :group => 1009 do
  puts "Hello world"
end

The block of code accepts the user hash as a paramter. So you can do things like:

Masquerade.as :user => "roger", :group => "developer" do ||
  puts "Hello #{[:user]}"
end

Both :user and :group params are optional and you can choose to give one of the two or both:

Masquerade.as :user => "roger" do ||
  puts "Hello #{[:user]}"
end

Exceptions thrown

Masquerade::PermissionsError

If the user running the script does not have the privileges to masquerade as the given user.

Masquerade::BadUserOrGroupError

If the given user or group does not exist

Return value

Returns the result of the block call. So you can do:

response = Masquerade.as :user => "roger", :group => "developer" do ||
  "Hello world #{[:user]}"
end