mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2025-02-23 13:49:03 +00:00
fix: re-create "root" user if users table is empty
previously it'd always re-create it if "root" user itself is missing from users table this facilitates not having "root" user altogether
This commit is contained in:
parent
6ff735badb
commit
0a62002a6e
@ -1,3 +1,10 @@
|
|||||||
|
const logger = require('./../../logger')
|
||||||
|
|
||||||
|
// Default root user's credentials
|
||||||
|
// Root user will only be created if "users" table is empty
|
||||||
|
const DEFAULT_ROOT_USERNAME = 'root'
|
||||||
|
const DEFAULT_ROOT_PASSWORD = 'changeme'
|
||||||
|
|
||||||
const initDatabase = async db => {
|
const initDatabase = async db => {
|
||||||
// Create the tables we need to store galleries and files
|
// Create the tables we need to store galleries and files
|
||||||
await db.schema.hasTable('albums').then(exists => {
|
await db.schema.hasTable('albums').then(exists => {
|
||||||
@ -51,21 +58,23 @@ const initDatabase = async db => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const root = await db.table('users')
|
const usersCount = await db.table('users')
|
||||||
.where('username', 'root')
|
.count('id as count')
|
||||||
.first()
|
.then(rows => rows[0].count)
|
||||||
|
|
||||||
if (!root) {
|
if (usersCount === 0) {
|
||||||
const hash = await require('bcrypt').hash('changeme', 10)
|
const hash = await require('bcrypt').hash(DEFAULT_ROOT_PASSWORD, 10)
|
||||||
const timestamp = Math.floor(Date.now() / 1000)
|
const timestamp = Math.floor(Date.now() / 1000)
|
||||||
await db.table('users').insert({
|
await db.table('users')
|
||||||
username: 'root',
|
.insert({
|
||||||
password: hash,
|
username: DEFAULT_ROOT_USERNAME,
|
||||||
token: require('randomstring').generate(64),
|
password: hash,
|
||||||
timestamp,
|
token: require('randomstring').generate(64),
|
||||||
permission: require('./../permissionController').permissions.superadmin,
|
timestamp,
|
||||||
registration: timestamp
|
permission: require('./../permissionController').permissions.superadmin,
|
||||||
})
|
registration: timestamp
|
||||||
|
})
|
||||||
|
logger.log(`Created user "${DEFAULT_ROOT_USERNAME}" with password "${DEFAULT_ROOT_PASSWORD}".`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user