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'

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



65
66
67
# File 'lib/osctld/system_users.rb', line 65

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
# File 'lib/osctld/system_users.rb', line 33

def add(name, ugid, homedir)
  fail '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)


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

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

#load_usersObject (protected)



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/osctld/system_users.rb', line 67

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



60
61
62
# File 'lib/osctld/system_users.rb', line 60

def log_type
  'system-users'
end

#remove(name) ⇒ Object

Remove user from the system

Parameters:

  • name (String)


42
43
44
45
46
# File 'lib/osctld/system_users.rb', line 42

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)


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

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