mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-01 21:47:16 +00:00
We only create a user when seeding the database and we check that there's no user already. We don't have a use case for adding an admin user to a database with user data. It also referred to a spree task that doesn't exist in our code base.
41 lines
918 B
Ruby
41 lines
918 B
Ruby
# frozen_string_literal: true
|
|
|
|
def create_admin_user
|
|
attributes = read_user_attributes
|
|
|
|
admin = Spree::User.new(attributes)
|
|
admin.skip_confirmation!
|
|
admin.skip_confirmation_notification!
|
|
|
|
# The default domain example.com is not resolved by all nameservers.
|
|
ValidEmail2::Address.define_method(:valid_mx?) { true }
|
|
|
|
if admin.save
|
|
printf <<~TEXT
|
|
New admin user persisted!
|
|
Username: #{admin.email}
|
|
Password: #{admin.password}
|
|
TEXT
|
|
else
|
|
printf "There was some problems with persisting new admin user:\n"
|
|
admin.errors.full_messages.each do |error|
|
|
printf "#{error}\n"
|
|
end
|
|
end
|
|
end
|
|
|
|
def read_user_attributes
|
|
password = ENV.fetch("ADMIN_PASSWORD", "ofn123")
|
|
email = ENV.fetch("ADMIN_EMAIL", "ofn@example.com")
|
|
|
|
{
|
|
admin: true,
|
|
password:,
|
|
password_confirmation: password,
|
|
email:,
|
|
login: email
|
|
}
|
|
end
|
|
|
|
create_admin_user if Spree::User.none?
|