Update auth/initialize_database.rb
This commit is contained in:
parent
f02c071911
commit
61bc30a679
|
@ -1,64 +1,66 @@
|
||||||
require_relative "cryptography"
|
require_relative "cryptography"
|
||||||
|
|
||||||
require 'sqlite3'
|
require 'sqlite3'
|
||||||
|
|
||||||
def initialize_database
|
def initialize_database
|
||||||
puts 'Checking if database needs initializing.'
|
puts 'Checking if database needs initializing.'
|
||||||
|
|
||||||
database_path = DATA_LOCATION + DATABASE_SUBPATH
|
database_path = DATA_LOCATION + DATABASE_SUBPATH
|
||||||
file_path = DATA_LOCATION + FILE_STORAGE_LOCATION
|
file_path = DATA_LOCATION + FILE_STORAGE_LOCATION
|
||||||
|
|
||||||
unless File.directory?(DATA_LOCATION)
|
unless File.directory?(DATA_LOCATION)
|
||||||
Dir.mkdir(DATA_LOCATION)
|
Dir.mkdir(DATA_LOCATION)
|
||||||
|
puts "Directory '#{DATA_LOCATION}' created successfully."
|
||||||
unless File.directory?(database_path)
|
end
|
||||||
Dir.mkdir(database_path)
|
|
||||||
puts "Directory '#{database_path}' created successfully."
|
unless File.directory?(database_path)
|
||||||
end
|
Dir.mkdir(database_path)
|
||||||
|
puts "Directory '#{database_path}' created successfully."
|
||||||
unless File.directory?(file_path)
|
end
|
||||||
Dir.mkdir(file_path)
|
|
||||||
puts "Directory '#{file_path}' created successfully."
|
unless File.directory?(file_path)
|
||||||
end
|
Dir.mkdir(file_path)
|
||||||
end
|
puts "Directory '#{file_path}' created successfully."
|
||||||
|
end
|
||||||
db = SQLite3::Database.new( DATA_LOCATION + DATABASE_SUBPATH + DATABASE_NAME )#'./database/auth.db')
|
|
||||||
|
|
||||||
puts 'Creating tables if necessary.'
|
db = SQLite3::Database.new( DATA_LOCATION + DATABASE_SUBPATH + DATABASE_NAME )#'./database/auth.db')
|
||||||
db.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username TEXT, hashed_password TEXT, is_admin INTEGER)')
|
|
||||||
db.execute('CREATE TABLE IF NOT EXISTS claims (id INTEGER PRIMARY KEY, claim TEXT)')
|
puts 'Creating tables if necessary.'
|
||||||
db.execute('CREATE TABLE IF NOT EXISTS user_claims (id INTEGER PRIMARY KEY, user_id INTEGER, claim_id INTEGER)')
|
db.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username TEXT, hashed_password TEXT, is_admin INTEGER)')
|
||||||
|
db.execute('CREATE TABLE IF NOT EXISTS claims (id INTEGER PRIMARY KEY, claim TEXT)')
|
||||||
result = db.get_first_value('SELECT COUNT(*) FROM claims')
|
db.execute('CREATE TABLE IF NOT EXISTS user_claims (id INTEGER PRIMARY KEY, user_id INTEGER, claim_id INTEGER)')
|
||||||
|
|
||||||
if result == 0
|
result = db.get_first_value('SELECT COUNT(*) FROM claims')
|
||||||
db.execute('INSERT INTO claims (claim) VALUES (?)', ['add_user'])
|
|
||||||
db.execute('INSERT INTO claims (claim) VALUES (?)', ['remove_user'])
|
if result == 0
|
||||||
db.execute('INSERT INTO claims (claim) VALUES (?)', ['is_machine'])
|
db.execute('INSERT INTO claims (claim) VALUES (?)', ['add_user'])
|
||||||
db.execute('INSERT INTO claims (claim) VALUES (?)', ['is_user'])
|
db.execute('INSERT INTO claims (claim) VALUES (?)', ['remove_user'])
|
||||||
db.execute('INSERT INTO claims (claim) VALUES (?)', ['add_claim_to_user'])
|
db.execute('INSERT INTO claims (claim) VALUES (?)', ['is_machine'])
|
||||||
db.execute('INSERT INTO claims (claim) VALUES (?)', ['remove_claim_from_user'])
|
db.execute('INSERT INTO claims (claim) VALUES (?)', ['is_user'])
|
||||||
end
|
db.execute('INSERT INTO claims (claim) VALUES (?)', ['add_claim_to_user'])
|
||||||
|
db.execute('INSERT INTO claims (claim) VALUES (?)', ['remove_claim_from_user'])
|
||||||
result = db.get_first_value('SELECT COUNT(*) FROM users')
|
end
|
||||||
|
|
||||||
if result == 0
|
result = db.get_first_value('SELECT COUNT(*) FROM users')
|
||||||
puts 'Default admin user added.'
|
|
||||||
|
if result == 0
|
||||||
create_new_user('admin', 'pass123', 1)
|
puts 'Default admin user added.'
|
||||||
|
|
||||||
admin_user_id = db.get_first_value('SELECT id FROM users WHERE username = ?', ['admin'])
|
create_new_user('admin', 'pass123', 1)
|
||||||
claim_ids = db.execute('SELECT id FROM claims')
|
|
||||||
|
admin_user_id = db.get_first_value('SELECT id FROM users WHERE username = ?', ['admin'])
|
||||||
claim_ids.each do |claim_id|
|
claim_ids = db.execute('SELECT id FROM claims')
|
||||||
db.execute('INSERT INTO user_claims (user_id, claim_id) VALUES (?, ?)', [admin_user_id, claim_id])
|
|
||||||
end
|
claim_ids.each do |claim_id|
|
||||||
else
|
db.execute('INSERT INTO user_claims (user_id, claim_id) VALUES (?, ?)', [admin_user_id, claim_id])
|
||||||
puts 'Table already contains data. Skipping default user creation.'
|
end
|
||||||
end
|
else
|
||||||
|
puts 'Table already contains data. Skipping default user creation.'
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue