Method: Inspec::Resources::WindowsUser#identity

Defined in:
lib/resources/user.rb

#identity(username) ⇒ Object



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/resources/user.rb', line 402

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

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

  script = <<-EOH
    # find user
    $user = Get-WmiObject Win32_UserAccount -filter "#{filter}"
    # get related groups
    $groups = $user.GetRelated('Win32_Group') | Select-Object -Property Caption, Domain, Name, LocalAccount, SID, SIDType, Status
    # filter user information
    $user = $user | Select-Object -Property Caption, Description, Domain, Name, LocalAccount, Lockout, PasswordChangeable, PasswordExpires, PasswordRequired, SID, SIDType, Status
    # build response object
    New-Object -Type PSObject | `
    Add-Member -MemberType NoteProperty -Name User -Value ($user) -PassThru | `
    Add-Member -MemberType NoteProperty -Name Groups -Value ($groups) -PassThru | `
    ConvertTo-Json
  EOH

  cmd = inspec.script(script)

  # cannot rely on exit code for now, successful command returns exit code 1
  # return nil if cmd.exit_status != 0, try to parse json
  begin
    params = JSON.parse(cmd.stdout)
  rescue JSON::ParserError => _e
    return nil
  end

  user = params['User']['Caption'] unless params['User'].nil?
  groups = params['Groups']
  # if groups is no array, generate one
  groups = [groups] if !groups.is_a?(Array)
  groups = groups.map { |grp| grp['Caption'] } unless params['Groups'].nil?

  {
    uid: nil,
    user: user,
    gid: nil,
    group: nil,
    groups: groups,
  }
end