From 6d7c41c04b5fada6a8a31e475a10cd85fc880ead Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Sat, 20 Jul 2019 20:48:49 +0100 Subject: [PATCH] Bring db/default/users from spree_auth_devise, this is necessary to create the first user in the DB --- db/default/users.rb | 82 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 db/default/users.rb diff --git a/db/default/users.rb b/db/default/users.rb new file mode 100644 index 0000000000..bf60502187 --- /dev/null +++ b/db/default/users.rb @@ -0,0 +1,82 @@ +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 create_admin_user + if ENV['AUTO_ACCEPT'] + password = 'spree123' + email = 'spree@example.com' + else + puts 'Create the admin user (press enter for defaults).' + #name = prompt_for_admin_name unless name + email = prompt_for_admin_email + password = prompt_for_admin_password + end + attributes = { + :password => password, + :password_confirmation => password, + :email => email, + :login => email + } + + 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 + 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