Skip to content

Commit

Permalink
add script to create an initial admin user
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcooke committed Apr 28, 2017
1 parent cc04a57 commit a1aa173
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ gem 'moonrope', :git => 'https://github.com/adamcooke/moonrope', :branch => 'mas
gem 'jwt'
gem 'createsend', '~> 4.0'
gem 'acme-client', :git => 'https://github.com/unixcharles/acme-client'
gem 'highline', :require => true

group :development, :test do
gem 'byebug'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ GEM
haml (4.0.7)
tilt
hashie (3.4.6)
highline (1.7.8)
httparty (0.14.0)
multi_xml (>= 0.5.2)
i18n (0.8.1)
Expand Down Expand Up @@ -230,6 +231,7 @@ DEPENDENCIES
gelf
haml
hashie
highline
jquery-rails
jwt
kaminari
Expand Down
5 changes: 5 additions & 0 deletions bin/postal
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ case "$1" in
run "ruby script/auto_upgrade.rb"
;;

make-user)
run "bundle exec ruby script/make_user.rb"
;;

bundle)
if [ -n "$2" ]; then
run "bundle install --path=$2 --without=development --jobs=4 --clean"
Expand Down Expand Up @@ -111,5 +115,6 @@ case "$1" in
echo -e " * \e[35mregister-lets-encrypt\e[0m - register the generated Let's Encrypt key"
echo -e " * \e[35mupgrade\e[0m - upgrade the Postal installation"
echo -e " * \e[35mbundle\e[0m - download & install all required Ruby dependencies"
echo -e " * \e[35mmake-user\e[0m - create a new admin user"
echo
esac
34 changes: 34 additions & 0 deletions lib/postal/user_creator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'highline'

module Postal
module UserCreator

def self.start(&block)
cli = HighLine.new
puts "\e[32mPostal User Creator\e[0m"
puts "Enter the information required to create a new Postal user."
puts "This tool is usually only used to create your initial admin user."
puts
user = User.new
user.email_address = cli.ask("E-Mail Address".ljust(20, ' ') + ": ")
user.first_name = cli.ask("First Name".ljust(20, ' ') + ": ")
user.last_name = cli.ask("Last Name".ljust(20, ' ') + ": ")
user.password = cli.ask("Initial Password:".ljust(20, ' ') + ": ") { |value| value.echo = '*' }

block.call(user) if block_given?
if user.save
puts
puts "User has been created with e-mail address \e[32m#{user.email_address}\e[0m"
puts
else
puts
puts "\e[31mFailed to create user\e[0m"
for error in user.errors.full_messages
puts " * #{error}"
end
puts
end

end
end
end
9 changes: 9 additions & 0 deletions script/make_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env ruby
trap("INT") { puts ; exit }

require_relative '../config/environment'
require 'postal/user_creator'

Postal::UserCreator.start do |u|
u.admin = true
end

0 comments on commit a1aa173

Please sign in to comment.