Files
rynDNS/db/seeds.rb
2021-02-25 00:04:34 -05:00

43 lines
1.9 KiB
Ruby

# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
# run `rails db:seed` or `rails db:seed RAILS_RESET_ADMIN=''`
# Rails.application.load_seed from within rails
# reseed db with default admin account
if (!Client.admins? || ENV['RAILS_RESET_ADMIN']) && !ENV['RAILS_API_DEMO']
output = ENV['RAILS_RESET_ADMIN'] ? 'Resetting admin account' : 'No admin accounts in Database, adding default: admin'
puts output
admin = Client.find_by name: 'admin'
admin ||= Client.new_client({ name: 'admin' })
api_key = admin.gen_api_key
admin.update_client(({ permission: 'rwx' }))
admin.save
puts "Created default admin with api_key: #{api_key}"
end
if ENV['RAILS_API_DEMO']
puts 'WARNING: Clearing DB and loading demo clients'
Client.delete_all
demo_clients = [
{ name: 'admin', name_id: '41a7c846', description: 'Demo Admin', permission: 'rwx',
api_key: '23b142e6451df545d5034ca8f6050b80ff3e9689f20d5e45d72d160fa7e1cf32' },
{ name: 'read_client', name_id: '28aef5cb', description: 'Demo Read Client', permission: 'r',
api_key: 'd502cb94ba0802c6363f95ecb3a70de7e2e4b889779e435b7c033f2c72ed4f20' },
{ name: 'write_client', name_id: '3d6129cc', description: 'Demo Write Client', permission: 'w', ipv4: '127.0.0.1',
api_key: '48d41d2a28b16a9a274d3f407026aee2b82cddbb3c11a5496ffe329c78e5d54b' }
]
demo_clients.each do |client_params|
client = Client.new_client(client_params)
client.update(client_params.slice(:name_id)) # to force name_id
client.save
puts "Client Name: #{client.name}"
puts "api_key: #{RynDNS::Application::API_KEY_PREFIX}.#{client.name_id}.#{client_params[:api_key]}"
end
end