mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-02 10:55:30 -04:00
Always create initial user if the DB is empty when started
This commit is contained in:
parent
fe799f30c8
commit
82bcd03f15
@ -47,23 +47,30 @@ class Storage
|
||||
Logger.fatal "Error when checking tables in DB: #{e}"
|
||||
raise e
|
||||
end
|
||||
|
||||
# If the DB is initialized through CLI but no user is added, we need
|
||||
# to create the admin user when first starting the app
|
||||
user_count = db.query_one "select count(*) from users", as: Int32
|
||||
init_admin if init_user && user_count == 0
|
||||
else
|
||||
Logger.debug "Creating DB file at #{@path}"
|
||||
db.exec "create unique index username_idx on users (username)"
|
||||
db.exec "create unique index token_idx on users (token)"
|
||||
|
||||
if init_user
|
||||
random_pw = random_str
|
||||
hash = hash_password random_pw
|
||||
db.exec "insert into users values (?, ?, ?, ?)",
|
||||
"admin", hash, nil, 1
|
||||
Logger.log "Initial user created. You can log in with " \
|
||||
"#{{"username" => "admin", "password" => random_pw}}"
|
||||
end
|
||||
init_admin if init_user
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
macro init_admin
|
||||
random_pw = random_str
|
||||
hash = hash_password random_pw
|
||||
db.exec "insert into users values (?, ?, ?, ?)",
|
||||
"admin", hash, nil, 1
|
||||
Logger.log "Initial user created. You can log in with " \
|
||||
"#{{"username" => "admin", "password" => random_pw}}"
|
||||
end
|
||||
|
||||
def verify_user(username, password)
|
||||
DB.open "sqlite3://#{@path}" do |db|
|
||||
begin
|
||||
|
Loading…
x
Reference in New Issue
Block a user