Skip to content

Instantly share code, notes, and snippets.

@BenMorganIO
Last active August 29, 2015 14:03
Show Gist options
  • Save BenMorganIO/68982e2873a0e8785011 to your computer and use it in GitHub Desktop.
Save BenMorganIO/68982e2873a0e8785011 to your computer and use it in GitHub Desktop.
Spree Admin Rake Task Override
require 'highline/import'
# see last line where we create an admin if there is none, asking for email and password
def prompt_for_admin_password
if ENV['ADMIN_PASSWORD']
password = ENV['ADMIN_PASSWORD'].dup
say "Admin Password #{password}"
else
password = ask('Password [spree123]: ') do |q|
q.echo = false
q.validate = /^(|.{5,40})$/
q.responses[:not_valid] = 'Invalid password. Must be at least 5 characters long.'
q.whitespace = :strip
end
password = 'spree123' if password.blank?
end
password
end
def prompt_for_admin_email
if ENV['ADMIN_EMAIL']
email = ENV['ADMIN_EMAIL'].dup
say "Admin User #{email}"
else
email = ask('Email [spree@example.com]: ') do |q|
q.echo = true
q.whitespace = :strip
end
email = 'spree@example.com' if email.blank?
end
email
end
def prompt_for_first_name
if ENV['ADMIN_FIRST_NAME']
first_name = ENV['ADMIN_FIRST_NAME'].dup
say "Admin User #{first_name}"
else
first_name = ask('First Name [John]: ') do |q|
q.echo = true
q.whitespace = :strip
end
first_name = 'John' if first_name.blank?
end
first_name
end
def prompt_for_last_name
if ENV['ADMIN_LAST_NAME']
last_name = ENV['ADMIN_LAST_NAME'].dup
say "Admin User #{last_name}"
else
last_name = ask('Last Name [Doe]: ') do |q|
q.echo = true
q.whitespace = :strip
end
last_name = 'Doe' if last_name.blank?
end
last_name
end
def create_admin_user
if ENV['AUTO_ACCEPT']
password = 'spree123'
email = 'spree@example.com'
first_name = 'John'
last_name = 'Doe'
else
puts 'Create the admin user (press enter for defaults).'
email = prompt_for_admin_email
password = prompt_for_admin_password
first_name = prompt_for_first_name
last_name = prompt_for_last_name
end
attributes = {
password: password,
password_confirmation: password,
email: email,
login: email,
first_name: first_name,
last_name: last_name
}
load 'spree/user.rb'
if Spree::User.find_by_email(email)
say "\nWARNING: There is already a user with the email: #{email}, " \
"so no account changes were made. If you wish to create an additional" \
"admin user, please run rake spree_auth:admin:create again with a different email.\n\n"
else
admin = Spree::User.new(attributes)
if admin.save
role = Spree::Role.find_or_create_by(name: 'admin')
admin.spree_roles << role
admin.save
admin.generate_spree_api_key!
say "Done!"
else
say "There was some problems with persisting new admin user:"
admin.errors.full_messages.each do |error|
say error
end
end
end
end
if Spree::User.admin.empty?
create_admin_user
else
puts 'Admin user has already been previously created.'
if agree('Would you like to create a new admin user? (yes/no)')
create_admin_user
else
puts 'No admin user created.'
end
end
namespace :spree_auth do
namespace :admin do
desc "Create admin username, password, first name, and last name"
task create: :environment do
require File.join(File.dirname(__FILE__), '..', '..', 'db', 'default', 'users.rb')
puts "Done!"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment