module NexClient::Commands::Users

Constants

USERS_HEADERS
USERS_TITLE

Public Class Methods

create_api_user(args,opts) click to toggle source
# File lib/nex_client/commands/users.rb, line 30
def self.create_api_user(args,opts)
  o = NexClient::Organization.find(handle: args.first).first
  unless o
    error("Error! Could not find organization: #{args.first}")
    return false
  end

  # Create user
  u = o.create_api_user
  self.display_users(u)
end
destroy(args,opts) click to toggle source
# File lib/nex_client/commands/users.rb, line 42
def self.destroy(args,opts)
  handle = args.first
  e = NexClient::User.find(handle: handle).first

  # Display error
  unless e
    error("Error! Could not find user: #{name}")
    return false
  end

  # Display error
  unless e.api_only
    error("Error! Only api users can be destroyed")
    return false
  end

  # Ask confirmation
  answer = ask("Enter the handle of this user to confirm: ")
  unless answer == e.handle
    error("Aborting deletion...")
    return false
  end

  e.destroy
  success("Successfully destroyed api user: #{handle}")
end
display_users(list) click to toggle source
# File lib/nex_client/commands/users.rb, line 69
def self.display_users(list)
  table = Terminal::Table.new title: USERS_TITLE, headings: USERS_HEADERS do |t|
    [list].flatten.compact.each do |e|
      t.add_row(self.format_record(e))
    end
  end
  puts table
  puts "\n"
end
format_orgs(record) click to toggle source
# File lib/nex_client/commands/users.rb, line 91
def self.format_orgs(record)
  return "-" unless record.organizations.present?
  record.organizations.map(&:handle).uniq.join(',')
end
format_record(record) click to toggle source
# File lib/nex_client/commands/users.rb, line 79
def self.format_record(record)
  orgs = self.format_orgs(record)
  [
    record.handle,
    record.first_name || '-',
    record.email || '-',
    record.api_token || '*****',
    record.api_only,
    orgs
  ]
end
list(args,opts) click to toggle source
# File lib/nex_client/commands/users.rb, line 10
def self.list(args,opts)
  filters = {}
  filters[:api_only] = opts.api if opts.api

  # Org filter
  org = opts.organization || opts.org
  filters[:'organization.handle'] = org if org.present?

  # Create table
  list = NexClient::User.includes(:organizations).where(filters).order('handle')
  self.display_users(list)

  # Loop through results
  while (list.pages.links||{})['next']
    return true if ask("Press enter for next page ('q' to quit)") =~ /q/
    list = list.pages.next
    self.display_users(list)
  end
end