Class: OsCtld::SystemUsers

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Lockable

#exclusively, included, #inclusively, #init_lock, #lock, #unlock

Constructor Details

#initializeSystemUsers

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

#usersObject (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

Parameters:

  • name (String)
  • ugid (Integer)
  • homedir (String)


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

Parameters:

  • name (String)

Returns:

  • (Boolean)


51
52
53
# File 'lib/osctld/system_users.rb', line 51

def include?(name)
  inclusively { users.has_key?(name) }
end

#load_usersObject (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_typeObject



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

Parameters:

  • name (String)


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

Parameters:

  • name (String)

Returns:

  • (Integer)


57
58
59
# File 'lib/osctld/system_users.rb', line 57

def uid_of(name)
  inclusively { users[name] }
end