Class: WindowsUser

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

Overview

For now, we stick with WMI Win32_UserAccount using Get-AdUser would be the best command for domain machines, but it will not be installed on client machines by default Just for reference, we could also use ADSI (Active Directory Service Interfaces)

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



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/resources/user.rb', line 317

def identity(username)
  # extract domain/user information
  , domain = (username)

  # TODO: escape content
  if !domain.nil?
    filter = "Name = '#{account}' and Domain = '#{domain}'"
  else
    filter = "Name = '#{account}' and LocalAccount = true"
  end

  script = "    # find user\n    $user = Get-WmiObject Win32_UserAccount -filter \"\#{filter}\"\n    # get related groups\n    $groups = $user.GetRelated('Win32_Group') | Select-Object -Property Caption, Domain, Name, LocalAccount, SID, SIDType, Status\n    # filter user information\n    $user = $user | Select-Object -Property Caption, Description, Domain, Name, LocalAccount, Lockout, PasswordChangeable, PasswordExpires, PasswordRequired, SID, SIDType, Status\n    # build response object\n    New-Object -Type PSObject | `\n    Add-Member -MemberType NoteProperty -Name User -Value ($user) -PassThru | `\n    Add-Member -MemberType NoteProperty -Name Groups -Value ($groups) -PassThru | `\n    ConvertTo-Json\n  EOH\n\n  cmd = inspec.script(script)\n\n  # cannot rely on exit code for now, successful command returns exit code 1\n  # return nil if cmd.exit_status != 0, try to parse json\n  begin\n    params = JSON.parse(cmd.stdout)\n  rescue JSON::ParserError => _e\n    return nil\n  end\n\n  user = params['User']['Caption'] unless params['User'].nil?\n  groups = params['Groups']\n  # if groups is no array, generate one\n  groups = [groups] if !groups.is_a?(Array)\n  groups = groups.map { |grp| grp['Caption'] } unless params['Groups'].nil?\n\n  {\n    uid: nil,\n    user: user,\n    gid: nil,\n    group: nil,\n    groups: groups,\n  }\nend\n"

#meta_info(_username) ⇒ Object

not implemented yet



368
369
370
371
372
373
# File 'lib/resources/user.rb', line 368

def meta_info(_username)
  {
    home: nil,
    shell: nil,
  }
end

#parse_windows_account(username) ⇒ Object

parse windows account name



310
311
312
313
314
315
# File 'lib/resources/user.rb', line 310

def (username)
   = username.split('\\')
  name = .pop
  domain = .pop if .size > 0
  [name, domain]
end