2024-11-05 11:12:42 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const generator = require("generate-password");
|
|
|
|
const DB_HANDLER = require("../../utils/backend/global-db/DB_HANDLER");
|
|
|
|
const NO_DB_HANDLER = require("../../utils/backend/global-db/NO_DB_HANDLER");
|
|
|
|
const addDbEntry = require("./db/addDbEntry");
|
2024-12-06 13:24:26 +00:00
|
|
|
const encrypt = require("../dsql/encrypt");
|
|
|
|
const LOCAL_DB_HANDLER = require("../../utils/backend/global-db/LOCAL_DB_HANDLER");
|
2024-11-05 11:12:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* # Add Mariadb User
|
|
|
|
*
|
|
|
|
* @description this function adds a Mariadb user to the database server
|
|
|
|
*
|
|
|
|
* @param {object} params - parameters object *
|
|
|
|
* @param {number | string} params.userId - invited user object
|
2024-12-06 13:24:26 +00:00
|
|
|
* @param {boolean} [params.useLocal]
|
2024-11-05 11:12:42 +00:00
|
|
|
*
|
|
|
|
* @returns {Promise<any>} new user auth object payload
|
|
|
|
*/
|
2024-12-06 13:24:26 +00:00
|
|
|
module.exports = async function addMariadbUser({ userId, useLocal }) {
|
2024-11-05 11:12:42 +00:00
|
|
|
try {
|
|
|
|
const defaultMariadbUserHost = process.env.DSQL_DB_HOST || "127.0.0.1";
|
|
|
|
|
|
|
|
const username = `dsql_user_${userId}`;
|
|
|
|
const password = generator.generate({
|
|
|
|
length: 16,
|
|
|
|
numbers: true,
|
|
|
|
symbols: true,
|
|
|
|
uppercase: true,
|
|
|
|
exclude: "*#.'`\"",
|
|
|
|
});
|
2024-12-06 13:24:26 +00:00
|
|
|
const encryptedPassword = encrypt({ data: password });
|
2024-11-05 11:12:42 +00:00
|
|
|
|
2024-12-08 19:39:44 +00:00
|
|
|
const createMariadbUsersQuery = `CREATE USER IF NOT EXISTS '${username}'@'127.0.0.1' IDENTIFIED BY '${password}'`;
|
2024-11-05 11:12:42 +00:00
|
|
|
|
2024-12-06 13:24:26 +00:00
|
|
|
if (useLocal) {
|
|
|
|
await LOCAL_DB_HANDLER(createMariadbUsersQuery);
|
|
|
|
} else {
|
|
|
|
await NO_DB_HANDLER(createMariadbUsersQuery);
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateUserQuery = `UPDATE users SET mariadb_user = ?, mariadb_host = '127.0.0.1', mariadb_pass = ? WHERE id = ?`;
|
|
|
|
const updateUserValues = [username, encryptedPassword, userId];
|
|
|
|
|
|
|
|
const updateUser = useLocal
|
|
|
|
? await LOCAL_DB_HANDLER(updateUserQuery, updateUserValues)
|
|
|
|
: await DB_HANDLER(updateUserQuery, updateUserValues);
|
2024-11-05 11:12:42 +00:00
|
|
|
|
|
|
|
const addMariadbUser = await addDbEntry({
|
|
|
|
tableName: "mariadb_users",
|
|
|
|
data: {
|
|
|
|
user_id: userId,
|
|
|
|
username,
|
|
|
|
host: defaultMariadbUserHost,
|
|
|
|
password: encryptedPassword,
|
|
|
|
primary: "1",
|
|
|
|
grants: '[{"database":"*","table":"*","privileges":["ALL"]}]',
|
|
|
|
},
|
|
|
|
dbContext: "Master",
|
2024-12-06 13:24:26 +00:00
|
|
|
useLocal,
|
2024-11-05 11:12:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
console.log(`User ${userId} SQL credentials successfully added.`);
|
|
|
|
} catch (/** @type {any} */ error) {
|
|
|
|
console.log(
|
|
|
|
`Error in adding SQL user in 'addMariadbUser' function =>`,
|
|
|
|
error.message
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////
|