Class: UnixUser

Inherits:
UserInfo show all
Defined in:
lib/resources/user.rb

Overview

implements generic unix id handling

Direct Known Subclasses

AixUser, DarwinUser, FreeBSDUser, LinuxUser

Instance Attribute Summary

Attributes inherited from UserInfo

#inspec

Instance Method Summary collapse

Methods inherited from UserInfo

#credentials, #initialize

Methods included from Converter

#convert_to_i

Constructor Details

This class inherits a constructor from UserInfo

Instance Method Details

#identity(username) ⇒ Object

extracts the identity



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/resources/user.rb', line 201

def identity(username)
  cmd = inspec.command("id #{username}")
  return nil if cmd.exit_status != 0

  # parse words
  params = SimpleConfig.new(
    parse_id_entries(cmd.stdout.chomp),
    assignment_re: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/,
    group_re: nil,
    multiple_values: false,
  ).params

  {
    uid: convert_to_i(parse_value(params['uid']).keys[0]),
    user: parse_value(params['uid']).values[0],
    gid: convert_to_i(parse_value(params['gid']).keys[0]),
    group: parse_value(params['gid']).values[0],
    groups: parse_value(params['groups']).values,
  }
end

#parse_id_entries(raw) ⇒ Object

splits the results of id into seperate lines



223
224
225
226
227
228
229
230
231
# File 'lib/resources/user.rb', line 223

def parse_id_entries(raw)
  data = []
  until (index = raw.index(/\)\s{1}/)).nil?
    data.push(raw[0, index+1]) # inclue closing )
    raw = raw[index+2, raw.length-index-2]
  end
  data.push(raw) if !raw.nil?
  data.join("\n")
end

#parse_value(line) ⇒ Object

parse one id entry like ‘0(wheel)”



190
191
192
193
194
195
196
197
198
# File 'lib/resources/user.rb', line 190

def parse_value(line)
  SimpleConfig.new(
    line,
    line_separator: ',',
    assignment_re: /^\s*([^\(]*?)\s*\(\s*(.*?)\)*$/,
    group_re: nil,
    multiple_values: false,
  ).params
end