[add] initial commit

This commit is contained in:
2021-02-24 03:22:52 -05:00
commit c8c91b21d9
57 changed files with 2707 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
class CreateClients < ActiveRecord::Migration[6.1]
def change
create_table :clients do |t|
# TODO: check performance of indexing name/name_id
# prefix: char(8) + name_id sha1(name + SecureRandom.base64(8))[0, 8] + api_key: SecureRandom.hex(32)
# -> 8 + 8 + 64 = client_api_key(80+2) (delimited with '.'):
# <prefix>.<name_id>.<api_key>
t.string :name, limit: 20, null: false, unique: true
t.string :name_id, limit: 8, null: false, unique: true
t.string :api_key_hash, null: false, limit: 60
t.string :description, limit: 200, default: ''
t.boolean :perm_r, default: false
t.boolean :perm_w, default: false
t.boolean :perm_x, default: false
t.boolean :public_ip, default: false
t.string :ipv4, limit: 15, default: ''
t.timestamps
end
end
end
# reset with all migrations with
# rake db:drop db:create db:migrate

29
db/schema.rb generated Normal file
View File

@@ -0,0 +1,29 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_02_18_044438) do
create_table "clients", force: :cascade do |t|
t.string "name", limit: 20, null: false
t.string "name_id", limit: 8, null: false
t.string "api_key_hash", limit: 60, null: false
t.string "description", limit: 200, default: ""
t.boolean "perm_r", default: false
t.boolean "perm_w", default: false
t.boolean "perm_x", default: false
t.boolean "public_ip", default: false
t.string "ipv4", limit: 15, default: ""
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
end

42
db/seeds.rb Normal file
View File

@@ -0,0 +1,42 @@
# 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