106 lines
3.9 KiB
JavaScript
106 lines
3.9 KiB
JavaScript
|
// @ts-check
|
||
|
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
|
||
|
require("dotenv").config({ path: "../../.env" });
|
||
|
const generator = require("generate-password");
|
||
|
const noDatabaseDbHandler = require("../utils/noDatabaseDbHandler");
|
||
|
const dbHandler = require("../utils/dbHandler");
|
||
|
const encrypt = require("../../functions/dsql/encrypt");
|
||
|
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
|
||
|
/**
|
||
|
* Create database from Schema Function
|
||
|
* ==============================================================================
|
||
|
* @param {object} params - Single object params
|
||
|
* @param {number|string|null} params.userId - User ID or null
|
||
|
*/
|
||
|
async function resetSQLCredentialsPasswords() {
|
||
|
/**
|
||
|
* @description Users
|
||
|
* @type {*[] | null}
|
||
|
*/ // @ts-ignore
|
||
|
const users = await dbHandler({
|
||
|
query: `SELECT * FROM users`,
|
||
|
});
|
||
|
|
||
|
if (!users) {
|
||
|
process.exit();
|
||
|
}
|
||
|
|
||
|
for (let i = 0; i < users.length; i++) {
|
||
|
const user = users[i];
|
||
|
|
||
|
if (!user) continue;
|
||
|
|
||
|
try {
|
||
|
/**
|
||
|
* @type {any[]}
|
||
|
*/ // @ts-ignore
|
||
|
const maridbUsers = await dbHandler({
|
||
|
query: `SELECT * FROM mysql.user WHERE User = 'dsql_user_${user.id}'`,
|
||
|
});
|
||
|
|
||
|
for (let j = 0; j < maridbUsers.length; j++) {
|
||
|
const { User, Host } = maridbUsers[j];
|
||
|
|
||
|
const password = generator.generate({
|
||
|
length: 16,
|
||
|
numbers: true,
|
||
|
symbols: true,
|
||
|
uppercase: true,
|
||
|
exclude: "*#.'`\"",
|
||
|
});
|
||
|
|
||
|
const encryptedPassword = encrypt({
|
||
|
data: password,
|
||
|
encryptionKey: process.env.DSQL_ENCRYPTION_PASSWORD,
|
||
|
encryptionSalt: process.env.DSQL_ENCRYPTION_SALT,
|
||
|
});
|
||
|
|
||
|
await noDatabaseDbHandler(
|
||
|
`SET PASSWORD FOR '${User}'@'${Host}' = PASSWORD('${password}')`
|
||
|
);
|
||
|
|
||
|
if (user.mariadb_user == User && user.mariadb_host == Host) {
|
||
|
const updateUser = await dbHandler({
|
||
|
query: `UPDATE users SET mariadb_pass = ? WHERE id = ?`,
|
||
|
values: [encryptedPassword, user.id],
|
||
|
});
|
||
|
}
|
||
|
|
||
|
console.log(
|
||
|
`User ${user.id}: ${user.first_name} ${user.last_name} Password Updated successfully added.`
|
||
|
);
|
||
|
}
|
||
|
} catch (/** @type {any} */ error) {
|
||
|
console.log(
|
||
|
`Error Updating User ${user.id} Password =>`,
|
||
|
error.message
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
process.exit();
|
||
|
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
}
|
||
|
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
|
||
|
resetSQLCredentialsPasswords();
|