107 lines
4.6 KiB
JavaScript
107 lines
4.6 KiB
JavaScript
const { hashPassword } = require('#src/auth');
|
|
const { PERMISSIONS, DEFAULT_ROLE } = require('#src/rbac');
|
|
|
|
async function bootstrapDatabase(pool) {
|
|
const [canvasSizeCountRows] = await pool.query('SELECT COUNT(*) AS canvas_size_count FROM c_canvas_sizes');
|
|
if (!canvasSizeCountRows.length || Number(canvasSizeCountRows[0].canvas_size_count) === 0) {
|
|
await pool.query(`
|
|
INSERT IGNORE INTO c_canvas_sizes (name, width, height) VALUES
|
|
('Full HD', 1920, 1080),
|
|
('HD', 1280, 720),
|
|
('4K UHD', 3840, 2160),
|
|
('Portrait Full HD', 1080, 1920),
|
|
('Portrait HD', 720, 1280)
|
|
`);
|
|
}
|
|
|
|
for (const permission of PERMISSIONS) {
|
|
await pool.query(
|
|
`UPDATE a_permissions
|
|
SET name = ?, section_name = ?, description = ?, modified_by = ?
|
|
WHERE permission_key = ?`,
|
|
[permission.name, permission.sectionName, permission.description || null, null, permission.key]
|
|
);
|
|
await pool.query(
|
|
`INSERT INTO a_permissions (permission_key, name, section_name, description, created_by, modified_by)
|
|
SELECT ?, ?, ?, ?, ?, ?
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM a_permissions WHERE permission_key = ?
|
|
)`,
|
|
[permission.key, permission.name, permission.sectionName, permission.description || null, null, null, permission.key]
|
|
);
|
|
}
|
|
|
|
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]
|
|
);
|
|
}
|
|
|
|
const [legacyRoleRows] = await pool.query('SELECT id FROM a_roles WHERE role_key = ? LIMIT 1', ['administrators']);
|
|
const [currentRoleRows] = await pool.query('SELECT id FROM a_roles WHERE role_key = ? LIMIT 1', [DEFAULT_ROLE.key]);
|
|
if (legacyRoleRows.length && !currentRoleRows.length) {
|
|
await pool.query(
|
|
`UPDATE a_roles
|
|
SET role_key = ?, name = ?, description = ?, modified_by = ?
|
|
WHERE role_key = ?`,
|
|
[DEFAULT_ROLE.key, DEFAULT_ROLE.name, DEFAULT_ROLE.description || null, null, 'administrators']
|
|
);
|
|
}
|
|
|
|
await pool.query(
|
|
`INSERT INTO a_roles (role_key, name, description, created_by, modified_by)
|
|
SELECT ?, ?, ?, ?, ?
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM a_roles WHERE role_key = ?
|
|
)`,
|
|
[DEFAULT_ROLE.key, DEFAULT_ROLE.name, DEFAULT_ROLE.description || null, null, null, DEFAULT_ROLE.key]
|
|
);
|
|
|
|
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 INTO a_role_permissions (role_id, permission_id, created_by, modified_by)
|
|
SELECT ?, permissions.id, NULL, NULL
|
|
FROM a_permissions permissions
|
|
LEFT JOIN a_role_permissions existing
|
|
ON existing.role_id = ? AND existing.permission_id = permissions.id
|
|
WHERE existing.id IS NULL`,
|
|
[defaultRoleId, 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
|
|
}; |