72 lines
3.2 KiB
JavaScript
72 lines
3.2 KiB
JavaScript
const { hashPassword } = require('../auth');
|
|
const { PERMISSIONS, DEFAULT_ROLE } = require('../rbac');
|
|
|
|
async function bootstrapDatabase(pool) {
|
|
for (const permission of PERMISSIONS) {
|
|
await pool.query(
|
|
`INSERT INTO a_permissions (permission_key, name, section_name, description, created_by, modified_by)
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
|
ON DUPLICATE KEY UPDATE name = VALUES(name), section_name = VALUES(section_name), description = VALUES(description), modified_by = VALUES(modified_by)` ,
|
|
[permission.key, permission.name, permission.sectionName, permission.description || null, null, null]
|
|
);
|
|
}
|
|
|
|
const [userCountRows] = await pool.query('SELECT COUNT(*) AS user_count FROM a_users');
|
|
const username = String(process.env.DEFAULT_ADMIN_USERNAME || 'admin').trim() || 'admin';
|
|
if (!userCountRows.length || Number(userCountRows[0].user_count) === 0) {
|
|
const name = String(process.env.DEFAULT_ADMIN_NAME || 'Admin').trim() || 'Admin';
|
|
const password = String(process.env.DEFAULT_ADMIN_PASSWORD || 'admin').trim() || 'admin';
|
|
const passwordRecord = hashPassword(password);
|
|
await pool.query(
|
|
'INSERT IGNORE INTO a_users (name, username, password_hash, password_salt, password_iterations, created_by, modified_by) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
|
[name, username, passwordRecord.hash, passwordRecord.salt, passwordRecord.iterations, null, null]
|
|
);
|
|
}
|
|
|
|
await pool.query('UPDATE a_users SET name = username WHERE name IS NULL OR name = ""');
|
|
|
|
await pool.query(
|
|
`INSERT INTO a_roles (role_key, name, description, created_by, modified_by)
|
|
VALUES (?, ?, ?, ?, ?)
|
|
ON DUPLICATE KEY UPDATE name = VALUES(name), description = VALUES(description), modified_by = VALUES(modified_by)`,
|
|
[DEFAULT_ROLE.key, DEFAULT_ROLE.name, DEFAULT_ROLE.description || null, null, null]
|
|
);
|
|
|
|
const [defaultRoleRows] = await pool.query('SELECT id FROM a_roles WHERE role_key = ? LIMIT 1', [DEFAULT_ROLE.key]);
|
|
const defaultRoleId = defaultRoleRows.length ? Number(defaultRoleRows[0].id) : null;
|
|
if (defaultRoleId) {
|
|
await pool.query(
|
|
`INSERT IGNORE INTO a_role_permissions (role_id, permission_id, created_by, modified_by)
|
|
SELECT ?, id, NULL, NULL FROM a_permissions`,
|
|
[defaultRoleId]
|
|
);
|
|
}
|
|
|
|
if (!userCountRows.length || Number(userCountRows[0].user_count) === 0) {
|
|
if (defaultRoleId) {
|
|
await pool.query(
|
|
`INSERT IGNORE INTO a_user_roles (user_id, role_id, created_by, modified_by)
|
|
SELECT id, ?, NULL, NULL FROM a_users`,
|
|
[defaultRoleId]
|
|
);
|
|
}
|
|
}
|
|
|
|
const [defaultAdminRows] = await pool.query('SELECT id FROM a_users WHERE username = ? LIMIT 1', [username]);
|
|
if (defaultAdminRows.length) {
|
|
const defaultAdminId = Number(defaultAdminRows[0].id);
|
|
const [defaultAdminRoleRows] = await pool.query('SELECT COUNT(*) AS role_count FROM a_user_roles WHERE user_id = ?', [defaultAdminId]);
|
|
if (!defaultAdminRoleRows.length || Number(defaultAdminRoleRows[0].role_count) === 0) {
|
|
if (defaultRoleId) {
|
|
await pool.query(
|
|
'INSERT IGNORE INTO a_user_roles (user_id, role_id, created_by, modified_by) VALUES (?, ?, ?, ?)',
|
|
[defaultAdminId, defaultRoleId, null, null]
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
bootstrapDatabase
|
|
}; |