masquerade 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Manifest ADDED
@@ -0,0 +1,4 @@
1
+ Manifest
2
+ README.rdoc
3
+ Rakefile
4
+ lib/masquerade.rb
data/README.rdoc ADDED
@@ -0,0 +1,29 @@
1
+ = Masquerade
2
+
3
+ Run a block of code as another user/group.
4
+
5
+ == Installation
6
+
7
+ # gem install masquerade
8
+
9
+ == Usage
10
+
11
+ To run a block of code as user "roger" and group "developer":
12
+ Masquerade.as :user => "roger", :group => "developer" do
13
+ puts "Hello world"
14
+ end
15
+
16
+ The block of code accepts the user hash as a paramter. So you can do things like:
17
+ Masquerade.as :user => "roger", :group => "developer" do |user_info|
18
+ puts "Hello world #user_info[:user]"
19
+ end
20
+
21
+ Both :user and :group params are optional and you can choose to give one or the two or both.
22
+
23
+ === Exceptions thrown
24
+ ==== Masquerade::PermissionsError
25
+
26
+ If the user running the script does not have the privileges to masquerade as the given user.
27
+
28
+ ==== Masquerade::BadUserOrGroupError
29
+ If the given user or group does not exist
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "rubygems"
2
+ require "rake"
3
+ require "echoe"
4
+
5
+ Echoe.new("masquerade", "0.1.0") do |p|
6
+ p.description = "A user/group impersonator"
7
+ p.summary = "Run a block of code as another user/group."
8
+ p.url = "http://rubygems.org/gems/masquerade"
9
+ p.author = "Nitesh Goel"
10
+ p.email = "nitesh@wikinvest.com"
11
+ p.ignore_pattern = ["tmp/*", "script/*"]
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
data/lib/masquerade.rb ADDED
@@ -0,0 +1,65 @@
1
+ require "etc"
2
+ =begin rdoc
3
+ = Masquerade
4
+
5
+ Run a block of code as another user/group.
6
+
7
+ == Installation
8
+
9
+ # gem install masquerade
10
+
11
+ == Usage
12
+
13
+ To run a block of code as user "roger" and group "developer":
14
+ Masquerade.as :user => "roger", :group => "developer" do
15
+ puts "Hello world"
16
+ end
17
+
18
+ The block of code accepts the user hash as a paramter. So you can do things like:
19
+ Masquerade.as :user => "roger", :group => "developer" do |user_info|
20
+ puts "Hello world #user_info[:user]"
21
+ end
22
+
23
+ Both :user and :group params are optional and you can choose to give one or the two or both.
24
+
25
+ === Exceptions thrown
26
+ ==== Masquerade::PermissionsError
27
+
28
+ If the user running the script does not have the privileges to masquerade as the given user.
29
+
30
+ ==== Masquerade::BadUserOrGroupError
31
+ If the given user or group does not exist
32
+ =end
33
+ module Masquerade
34
+ def self.as(who, &block)
35
+ begin
36
+ current_uid = Process.uid
37
+ current_gid = Process.gid
38
+ # get user info by name
39
+ if who.include?(:user)
40
+ user_struct = Etc.getpwnam(who[:user])
41
+ # set the uid of the current process to that of the chosen user
42
+ Process.uid = user_struct.uid
43
+ end
44
+ if who.include?(:group)
45
+ group_struct = Etc.getgrnam(who[:group])
46
+ Process.gid = group_struct.gid
47
+ end
48
+ # run the block
49
+ block.call(who)
50
+ # restore process uid and gid
51
+ Process.uid = current_uid
52
+ Process.gid = current_gid
53
+ rescue Errno::EPERM
54
+ raise PermissionsError, "You do not have permissions to impersonate this user or group"
55
+ rescue ArgumentError
56
+ raise BadUserOrGroupError, "The user or group name does not exist"
57
+ end
58
+ end
59
+
60
+ class PermissionsError < StandardError
61
+ end
62
+
63
+ class BadUserOrGroupError < StandardError
64
+ end
65
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{masquerade}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Nitesh Goel"]
9
+ s.date = %q{2011-04-14}
10
+ s.description = %q{A user/group impersonator}
11
+ s.email = %q{nitesh@wikinvest.com}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/masquerade.rb"]
13
+ s.files = ["Manifest", "README.rdoc", "Rakefile", "lib/masquerade.rb", "masquerade.gemspec"]
14
+ s.homepage = %q{http://rubygems.org/gems/masquerade}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Masquerade", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{masquerade}
18
+ s.rubygems_version = %q{1.7.2}
19
+ s.summary = %q{Run a block of code as another user/group.}
20
+
21
+ if s.respond_to? :specification_version then
22
+ s.specification_version = 3
23
+
24
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
+ else
26
+ end
27
+ else
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: masquerade
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Nitesh Goel
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-14 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: A user/group impersonator
22
+ email: nitesh@wikinvest.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.rdoc
29
+ - lib/masquerade.rb
30
+ files:
31
+ - Manifest
32
+ - README.rdoc
33
+ - Rakefile
34
+ - lib/masquerade.rb
35
+ - masquerade.gemspec
36
+ homepage: http://rubygems.org/gems/masquerade
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --line-numbers
42
+ - --inline-source
43
+ - --title
44
+ - Masquerade
45
+ - --main
46
+ - README.rdoc
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 11
64
+ segments:
65
+ - 1
66
+ - 2
67
+ version: "1.2"
68
+ requirements: []
69
+
70
+ rubyforge_project: masquerade
71
+ rubygems_version: 1.7.2
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Run a block of code as another user/group.
75
+ test_files: []
76
+