Class: OsCtld::SystemUsers
- Inherits:
-
Object
- Object
- OsCtld::SystemUsers
- Includes:
- OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System, Lockable, Singleton
- Defined in:
- lib/osctld/system_users.rb
Overview
Cache for existing system users
Constant Summary collapse
- COMMENT =
'osctl'.freeze
Instance Attribute Summary collapse
-
#users ⇒ Object
readonly
protected
Returns the value of attribute users.
Instance Method Summary collapse
-
#add(name, ugid, homedir) ⇒ Object
Add new user to the system.
-
#include?(name) ⇒ Boolean
Check if a system user exists.
-
#initialize ⇒ SystemUsers
constructor
A new instance of SystemUsers.
- #load_users ⇒ Object protected
- #log_type ⇒ Object
-
#remove(name) ⇒ Object
Remove user from the system.
- #uid_of(name) ⇒ Integer
Methods included from Lockable
#exclusively, included, #inclusively, #init_lock, #lock, #unlock
Constructor Details
#initialize ⇒ SystemUsers
Returns a new instance of SystemUsers.
23 24 25 26 27 |
# File 'lib/osctld/system_users.rb', line 23 def initialize init_lock @users = {} load_users end |
Instance Attribute Details
#users ⇒ Object (readonly, protected)
Returns the value of attribute users.
67 68 69 |
# File 'lib/osctld/system_users.rb', line 67 def users @users end |
Instance Method Details
#add(name, ugid, homedir) ⇒ Object
Add new user to the system
33 34 35 36 37 38 39 |
# File 'lib/osctld/system_users.rb', line 33 def add(name, ugid, homedir) raise 'user already exists' if include?(name) syscmd("groupadd -g #{ugid} #{name}") syscmd("useradd -u #{ugid} -g #{ugid} -d #{homedir} -c #{COMMENT} #{name}") exclusively { users[name] = ugid } end |
#include?(name) ⇒ Boolean
Check if a system user exists
51 52 53 |
# File 'lib/osctld/system_users.rb', line 51 def include?(name) inclusively { users.has_key?(name) } end |
#load_users ⇒ Object (protected)
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/osctld/system_users.rb', line 69 def load_users exclusively do users.clear syscmd('getent passwd').output.split("\n").each do |line| fields = line.split(':') name = fields.first uid = fields[2].to_i comment = fields[4] UGidRegistry << uid users[name] = uid if comment == COMMENT end end end |
#log_type ⇒ Object
61 62 63 |
# File 'lib/osctld/system_users.rb', line 61 def log_type 'system-users' end |
#remove(name) ⇒ Object
Remove user from the system
43 44 45 46 47 |
# File 'lib/osctld/system_users.rb', line 43 def remove(name) syscmd("userdel -f #{name}") syscmd("groupdel #{name}", valid_rcs: [6]) exclusively { users.delete(name) } end |
#uid_of(name) ⇒ Integer
57 58 59 |
# File 'lib/osctld/system_users.rb', line 57 def uid_of(name) inclusively { users[name] } end |