|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 |
| -# see last line where we create an admin if there is none, asking for email and password |
4 |
| -def prompt_for_admin_password |
5 |
| - if ENV['ADMIN_PASSWORD'] |
6 |
| - password = ENV['ADMIN_PASSWORD'].dup |
7 |
| - puts "Admin Password #{password}" |
8 |
| - else |
9 |
| - print "Password [test123]: " |
10 |
| - password = STDIN.gets.strip |
11 |
| - password = 'test123' if password.blank? |
12 |
| - end |
| 3 | +admin_role = Spree::Role.find_or_create_by(name: 'admin') |
13 | 4 |
|
14 |
| - password |
| 5 | +if Spree::User.admin.any? |
| 6 | + puts 'No admin user created.' |
| 7 | + return |
15 | 8 | end
|
16 | 9 |
|
17 |
| -def prompt_for_admin_email |
18 |
| - if ENV['ADMIN_EMAIL'] |
19 |
| - email = ENV['ADMIN_EMAIL'].dup |
20 |
| - puts "Admin User #{email}" |
21 |
| - else |
22 |
| - print "Email [[email protected]]: " |
23 |
| - email = STDIN.gets.strip |
24 |
| - email = '[email protected]' if email.blank? |
25 |
| - end |
| 10 | +email = ENV['ADMIN_EMAIL'] || '[email protected]' |
| 11 | +password = ENV['ADMIN_PASSWORD'] || 'test123' |
26 | 12 |
|
27 |
| - email |
28 |
| -end |
29 |
| - |
30 |
| -def create_admin_user |
31 |
| - if ENV['AUTO_ACCEPT'] |
32 |
| - password = 'test123' |
33 |
| - |
34 |
| - else |
35 |
| - puts 'Create the admin user (press enter for defaults).' |
36 |
| - # name = prompt_for_admin_name unless name |
37 |
| - email = prompt_for_admin_email |
38 |
| - password = prompt_for_admin_password |
39 |
| - end |
40 |
| - attributes = { |
41 |
| - password: password, |
42 |
| - password_confirmation: password, |
43 |
| - email: email, |
44 |
| - login: email |
45 |
| - } |
46 |
| - |
47 |
| - load 'spree/user.rb' |
| 13 | +puts "Creating admin user with:" |
| 14 | +puts " - email: #{email}" |
| 15 | +puts " - password: #{password}" |
| 16 | +puts "(please use the ADMIN_EMAIL and ADMIN_PASSWORD environment variables to control how the default admin user is created)" |
48 | 17 |
|
49 |
| - if Spree::User.find_by(email: email) |
50 |
| - puts "\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" |
51 |
| - else |
52 |
| - admin = Spree::User.new(attributes) |
53 |
| - if admin.save |
54 |
| - role = Spree::Role.find_or_create_by(name: 'admin') |
55 |
| - admin.spree_roles << role |
56 |
| - admin.save |
57 |
| - admin.generate_spree_api_key! |
58 |
| - puts "Done!" |
59 |
| - else |
60 |
| - puts "There were some problems with persisting a new admin user:" |
61 |
| - admin.errors.full_messages.each do |error| |
62 |
| - puts error |
63 |
| - end |
64 |
| - end |
65 |
| - end |
| 18 | +if Spree::User.find_by(email: email) |
| 19 | + warn "WARNING: There is already a user with the email: #{email}, so no account changes were made." |
| 20 | + return |
66 | 21 | end
|
67 | 22 |
|
68 |
| -if Spree::User.admin.empty? |
69 |
| - create_admin_user |
| 23 | +admin = Spree::User.new( |
| 24 | + password: password, |
| 25 | + password_confirmation: password, |
| 26 | + email: email, |
| 27 | + login: email, |
| 28 | +) |
| 29 | + |
| 30 | +if admin.save |
| 31 | + admin.spree_roles << admin_role |
| 32 | + admin.save |
| 33 | + admin.generate_spree_api_key! |
70 | 34 | else
|
71 |
| - puts 'Admin user has already been created.' |
72 |
| - puts 'Would you like to create a new admin user? (yes/no)' |
73 |
| - if ["yes", "y"].include? STDIN.gets.strip.downcase |
74 |
| - create_admin_user |
75 |
| - else |
76 |
| - puts 'No admin user created.' |
77 |
| - end |
| 35 | + warn "There were some problems while creating the admin user:" |
| 36 | + warn(admin.errors.full_messages.map { |m| "- #{m}" }) |
| 37 | + warn "(attributes: #{admin.attributes.inspect})" |
78 | 38 | end
|
0 commit comments