Class: PostDB::CLI::Users

Inherits:
Thor
  • Object
show all
Defined in:
lib/postdb/cli/users.rb

Instance Method Summary collapse

Instance Method Details

#add(email = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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

#list(domain = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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