class PostDB::CLI::Users

Public Instance Methods

add(email = nil) click to toggle source
# File lib/postdb/cli/users.rb, line 54
def add(email = nil)
  unless email
    email = prompt.ask("Email:") do |q|
      q.required(true)
      q.validate(:email)
    end
  end

  email = email.downcase

  if PostDB::User.where(email: email).count > 0
    exit_with_warning("The user '#{email}' has already been added.")
  end

  domain_name = email.split('@')[1]

  unless domain = PostDB::Domain.where(name: domain_name).first
    exit_with_error("The domain '#{domain_name}' is not available.")
  end

  password = get_password_or_ask

  PostDB::User.create(domain: domain, email: email, password: password)

  prompt.ok("The user '#{email}' has been added.")
end
change_password(email = nil) click to toggle source
# File lib/postdb/cli/users.rb, line 113
def change_password(email = nil)
  unless email
    users = PostDB::User.all

    if users.empty?
      exit_with_warning("There don't appear to be any users on this system.")
    end

    email = prompt.select("User:", users.map(&:email))
  end

  email = email.downcase

  users = PostDB::User.where(email: email)

  if users.empty?
    exit_with_warning("The user '#{email}' could not be found.")
  end

  password = get_password_or_ask

  users.each do |user|
    user.update(password: password)
  end

  prompt.ok("The password for '#{email}' has been changed.")
end
get_password_or_ask() click to toggle source
# File lib/postdb/cli/users.rb, line 7
def get_password_or_ask
  password = options[:password]

  unless password
    password = prompt.mask("Password:") do |q|
      q.required(true)
    end

    password_confirmation = prompt.mask("Password Confirmation:") do |q|
      q.required(true)
    end

    unless password == password_confirmation
      exit_with_error("Those passwords don't match.")
    end
  end

  password
end
list(domain = nil) click to toggle source
# File lib/postdb/cli/users.rb, line 29
def list(domain = nil)
  domains = PostDB::Domain.where(**(domain ? { name: domain } : {}))

  if domains.empty?
    if domain
      exit_with_warning("The domain '#{domain}' could not be found.")
    else
      exit_with_warning("There don't appear to be any domains on this system.")
    end
  end

  domains.each_with_index do |domain, index|
    users = domain.users.sort { |a, b| a.email <=> b.email }

    puts TTY::Table.new(
      header: [domain.name].pad(' '),
      rows: users.empty? ? [[' No Users ']] : users.map { |u| [u.email].pad(' ') }
    ).render(:ascii)

    new_line unless (index + 1) == domains.count
  end
end
remove(email = nil) click to toggle source
# File lib/postdb/cli/users.rb, line 83
def remove(email = nil)
  unless email
    users = PostDB::User.all

    if users.empty?
      exit_with_warning("There don't appear to be any users on this system.")
    end

    email = prompt.select("User:", users.map(&:email))
  end

  email = email.downcase

  users = PostDB::User.where(email: email)

  if users.empty?
    exit_with_warning("The user '#{email}' could not be found.")
  end

  unless options[:force]
    confirm_action!("Remove the user '#{email}'?", "'#{email}' left untouched.")
  end

  users.each(&:destroy)

  prompt.ok("The user '#{email}' has been removed.")
end