dsql-admin/dsql-app/docker-root-user-init.js
Benjamin Toby 748ff55092 Bug Fixes
2024-11-05 15:18:40 +01:00

61 lines
1.6 KiB
JavaScript
Executable File

// @ts-check
const path = require("path");
require("dotenv").config({ path: path.resolve(__dirname, ".env") });
const {
hashPassword,
} = require("./package-shared/functions/backend/passwordHash");
const fs = require("fs");
const { execSync } = require("child_process");
/**
* # Create Extra users
* @description Create `READ_ONLY` and `FULL_ACCESS` users
*/
async function initRootUser() {
/**
* Grab Schema
*
* @description Grab Schema
*/
const first_name = process.env.DSQL_LOCAL_SU_FIRST_NAME;
const last_name = process.env.DSQL_LOCAL_SU_LAST_NAME;
const email = process.env.DSQL_LOCAL_SU_EMAIL;
const username = process.env.DSQL_LOCAL_SU_USERNAME;
const password = process.env.DSQL_LOCAL_SU_PASSWORD;
if (!password) {
throw new Error("Please enter a root admin user password");
}
/** @type {any} */
const userObject = {
id: 1,
first_name,
last_name,
username,
email,
password,
verification_status: 1,
user_priviledge: 7,
};
try {
const tmpDir = "/app/.tmp";
if (!tmpDir) {
fs.mkdirSync(tmpDir, { recursive: true });
}
const tmpFilePath = `${tmpDir}/new-user.json`;
fs.writeFileSync(tmpFilePath, JSON.stringify(userObject));
const execStr = `node /app/shell/mariadb-users/users/create-user.js ${tmpFilePath}`;
execSync(execStr, { stdio: "inherit" });
} catch (error) {
console.log(`docker-root-user-init.js ERROR: ${error.message}`);
}
process.exit();
}
initRootUser();