Class: Fusuma::Plugin::Appmatcher::UserSwitcher
- Inherits:
-
Object
- Object
- Fusuma::Plugin::Appmatcher::UserSwitcher
- Includes:
- CustomProcess
- Defined in:
- lib/fusuma/plugin/appmatcher/user_switcher.rb
Defined Under Namespace
Classes: User
Instance Method Summary collapse
-
#as_user(user = @login_user) ⇒ Object
Execute the provided block in a child process as the specified user The parent blocks until the child finishes.
-
#drop_priv(user) ⇒ Object
Drops privileges to that of the specified user.
-
#initialize ⇒ UserSwitcher
constructor
A new instance of UserSwitcher.
Constructor Details
#initialize ⇒ UserSwitcher
Returns a new instance of UserSwitcher.
12 13 14 15 16 17 |
# File 'lib/fusuma/plugin/appmatcher/user_switcher.rb', line 12 def initialize username = ENV['SUDO_USER'] || Etc.getlogin uid = `id -u #{username}`.chomp.to_i gid = `id -g #{username}`.chomp.to_i @login_user = User.new(username, uid, gid) end |
Instance Method Details
#as_user(user = @login_user) ⇒ Object
Execute the provided block in a child process as the specified user The parent blocks until the child finishes.
29 30 31 32 33 34 |
# File 'lib/fusuma/plugin/appmatcher/user_switcher.rb', line 29 def as_user(user = @login_user) fork do drop_priv(user) yield(user) if block_given? end end |
#drop_priv(user) ⇒ Object
Drops privileges to that of the specified user
20 21 22 23 24 25 |
# File 'lib/fusuma/plugin/appmatcher/user_switcher.rb', line 20 def drop_priv(user) # Process.initgroups(user.username, user.gid) Process::Sys.setegid(user.gid) Process::Sys.setgid(user.gid) Process::Sys.setuid(user.uid) end |