Updates
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
# Handle Datasquirel MariaDB Users and Grants
|
||||
|
||||
## Files
|
||||
|
||||
### refreshUsersAndGrants.js
|
||||
|
||||
This script checks MariaDB users and updates their privileges using the `mariadb_users` table in `datasquirel` database.
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
// @ts-check
|
||||
|
||||
const noDatabaseDbHandler = require("../utils/noDatabaseDbHandler");
|
||||
|
||||
/**
|
||||
* @typedef {object} GrantType
|
||||
* @property {string} database - Database Name
|
||||
* @property {string} table - Table Name
|
||||
* @property {string[]} privileges - Privileges
|
||||
*/
|
||||
|
||||
/**
|
||||
* Handle Grants for Users
|
||||
* ================================================
|
||||
* @param {object} params - Single object params
|
||||
* @param {string} params.username - Username
|
||||
* @param {string} params.host - Host
|
||||
* @param {GrantType[]} params.grants - Grants
|
||||
* @param {string} params.userId
|
||||
*
|
||||
* @returns {Promise<boolean>} success
|
||||
*/
|
||||
async function handleGrants({ username, host, grants, userId }) {
|
||||
let success = false;
|
||||
|
||||
console.log(`Handling Grants for User =>`, username, host);
|
||||
|
||||
if (!username) {
|
||||
console.log(`No username provided.`);
|
||||
return success;
|
||||
}
|
||||
|
||||
if (!host) {
|
||||
console.log(
|
||||
`No Host provided. \x1b[35m\`--host\`\x1b[0m flag is required`
|
||||
);
|
||||
return success;
|
||||
}
|
||||
|
||||
if (!grants) {
|
||||
console.log(`No grants Array provided.`);
|
||||
return success;
|
||||
}
|
||||
|
||||
try {
|
||||
const existingUser = await noDatabaseDbHandler(
|
||||
`SELECT * FROM mysql.user WHERE User = '${username}' AND Host = '${host}'`
|
||||
);
|
||||
|
||||
const isUserExisting = Boolean(existingUser?.[0]?.User);
|
||||
|
||||
if (isUserExisting) {
|
||||
const userGrants = await noDatabaseDbHandler(
|
||||
`SHOW GRANTS FOR '${username}'@'${host}'`
|
||||
);
|
||||
|
||||
for (let i = 0; i < userGrants.length; i++) {
|
||||
const grantObject = userGrants[i];
|
||||
const grant = grantObject?.[Object.keys(grantObject)[0]];
|
||||
|
||||
if (grant?.match(/GRANT .* PRIVILEGES ON .* TO/)) {
|
||||
const revokeGrantText = grant
|
||||
.replace(/GRANT/, "REVOKE")
|
||||
.replace(/ TO /, " FROM ");
|
||||
|
||||
const revokePrivilege = await noDatabaseDbHandler(
|
||||
revokeGrantText
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {GrantType[]}
|
||||
*/
|
||||
const grantsArray = grants;
|
||||
|
||||
for (let i = 0; i < grantsArray.length; i++) {
|
||||
const grantObject = grantsArray[i];
|
||||
const { database, table, privileges } = grantObject;
|
||||
|
||||
const tableText = table == "*" ? "*" : `\`${table}\``;
|
||||
const databaseText =
|
||||
database == "*"
|
||||
? `\`${process.env.DSQL_USER_DB_PREFIX}${userId}_%\``
|
||||
: `\`${database}\``;
|
||||
|
||||
const privilegesText = privileges.includes("ALL")
|
||||
? "ALL PRIVILEGES"
|
||||
: privileges.join(", ");
|
||||
|
||||
const grantText = `GRANT ${privilegesText} ON ${databaseText}.${tableText} TO '${username}'@'${host}'`;
|
||||
|
||||
const grantPriviledge = await noDatabaseDbHandler(grantText);
|
||||
}
|
||||
}
|
||||
|
||||
success = true;
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(`Error in adding SQL user =>`, error.message);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
module.exports = handleGrants;
|
||||
+269
@@ -0,0 +1,269 @@
|
||||
// @ts-check
|
||||
|
||||
const path = require("path");
|
||||
require("dotenv").config({ path: path.resolve(__dirname, "../../../.env") });
|
||||
|
||||
const generator = require("generate-password");
|
||||
const noDatabaseDbHandler = require("../utils/noDatabaseDbHandler");
|
||||
const dbHandler = require("../utils/dbHandler");
|
||||
const handleGrants = require("./handleGrants");
|
||||
const encrypt = require("../../functions/dsql/encrypt");
|
||||
const decrypt = require("../../functions/dsql/decrypt");
|
||||
|
||||
const defaultMariadbUserHost = process.env.DSQL_DB_HOST || "127.0.0.1";
|
||||
|
||||
/**
|
||||
* Refresh Mariadb User Grants
|
||||
* ===================================================
|
||||
* @param {object} params
|
||||
* @param {number | string} [params.userId]
|
||||
* @param {string} [params.mariadbUserHost]
|
||||
* @param {string} [params.mariadbUser]
|
||||
* @param {string | number} [params.sqlUserID]
|
||||
*/
|
||||
async function refreshUsersAndGrants({
|
||||
userId,
|
||||
mariadbUserHost,
|
||||
mariadbUser,
|
||||
sqlUserID,
|
||||
}) {
|
||||
/**
|
||||
* @description Users
|
||||
* @type {*[] | null}
|
||||
*/ // @ts-ignore
|
||||
const users = await dbHandler({
|
||||
query: `SELECT * FROM users`,
|
||||
});
|
||||
|
||||
if (!users?.[0]) {
|
||||
process.exit();
|
||||
}
|
||||
|
||||
for (let i = 0; i < users.length; i++) {
|
||||
const user = users[i];
|
||||
|
||||
if (!user) continue;
|
||||
if (userId && user.id != userId) continue;
|
||||
|
||||
try {
|
||||
const { mariadb_user, mariadb_host, mariadb_pass, id } = user;
|
||||
const existingUser = await noDatabaseDbHandler(
|
||||
`SELECT * FROM mysql.user WHERE User = '${mariadb_user}' AND Host = '${mariadb_host}'`
|
||||
);
|
||||
|
||||
const existingMariaDBUserArray =
|
||||
userId && sqlUserID
|
||||
? await dbHandler({
|
||||
query: `SELECT * FROM mariadb_users WHERE id = ? AND user_id = ?`,
|
||||
values: [sqlUserID, userId],
|
||||
})
|
||||
: null;
|
||||
|
||||
/**
|
||||
* @type {import("../../types").MYSQL_mariadb_users_table_def | undefined}
|
||||
*/
|
||||
const activeMariadbUserObject = Array.isArray(
|
||||
existingMariaDBUserArray
|
||||
)
|
||||
? existingMariaDBUserArray?.[0]
|
||||
: undefined;
|
||||
|
||||
const isPrimary = activeMariadbUserObject
|
||||
? String(activeMariadbUserObject.primary)?.match(/1/)
|
||||
? true
|
||||
: false
|
||||
: false;
|
||||
|
||||
const isUserExisting = Boolean(existingUser?.[0]?.User);
|
||||
|
||||
const isThisPrimaryHost = Boolean(
|
||||
mariadbUserHost == defaultMariadbUserHost
|
||||
);
|
||||
|
||||
const dslUsername = `dsql_user_${id}`;
|
||||
const dsqlPassword = activeMariadbUserObject?.password
|
||||
? activeMariadbUserObject.password
|
||||
: isUserExisting
|
||||
? mariadb_pass
|
||||
: generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
symbols: true,
|
||||
uppercase: true,
|
||||
exclude: "*#.'`\"",
|
||||
});
|
||||
|
||||
const encryptedPassword = activeMariadbUserObject?.password
|
||||
? activeMariadbUserObject.password
|
||||
: isUserExisting
|
||||
? mariadb_pass
|
||||
: encrypt({
|
||||
data: dsqlPassword,
|
||||
encryptionKey: process.env.DSQL_ENCRYPTION_PASSWORD,
|
||||
encryptionSalt: process.env.DSQL_ENCRYPTION_SALT,
|
||||
});
|
||||
if (
|
||||
!isUserExisting &&
|
||||
!sqlUserID &&
|
||||
!isPrimary &&
|
||||
!mariadbUserHost &&
|
||||
!mariadbUser
|
||||
) {
|
||||
const createNewUser = await noDatabaseDbHandler(
|
||||
`CREATE USER IF NOT EXISTS '${dslUsername}'@'${defaultMariadbUserHost}' IDENTIFIED BY '${dsqlPassword}' REQUIRE SSL`
|
||||
);
|
||||
|
||||
console.log("createNewUser", createNewUser);
|
||||
|
||||
console.log(
|
||||
`User ${user.id}: ${user.first_name} ${user.last_name} SQL credentials successfully updated.`
|
||||
);
|
||||
|
||||
const updateUser = await dbHandler({
|
||||
query: `UPDATE users SET mariadb_user = ?, mariadb_host = ?, mariadb_pass = ? WHERE id = ?`,
|
||||
values: [
|
||||
dslUsername,
|
||||
defaultMariadbUserHost,
|
||||
encryptedPassword,
|
||||
user.id,
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
if (isPrimary) {
|
||||
const finalHost = mariadbUserHost
|
||||
? mariadbUserHost
|
||||
: mariadb_host;
|
||||
|
||||
const updateUser = await dbHandler({
|
||||
query: `UPDATE users SET mariadb_user = ?, mariadb_host = ?, mariadb_pass = ? WHERE id = ?`,
|
||||
values: [
|
||||
dslUsername,
|
||||
finalHost,
|
||||
encryptedPassword,
|
||||
user.id,
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////
|
||||
//////////////////////////////////////////////
|
||||
//////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* @description Handle mariadb_users table
|
||||
*/
|
||||
const existingMariadbPrimaryUser = await dbHandler({
|
||||
query: `SELECT * FROM mariadb_users WHERE user_id = ? AND \`primary\` = 1`,
|
||||
values: [id],
|
||||
});
|
||||
|
||||
const isPrimaryUserExisting = Boolean(
|
||||
Array.isArray(existingMariadbPrimaryUser) &&
|
||||
existingMariadbPrimaryUser?.[0]?.user_id
|
||||
);
|
||||
|
||||
/** @type {import("./handleGrants").GrantType[]} */
|
||||
const primaryUserGrants = [
|
||||
{
|
||||
database: "*",
|
||||
table: "*",
|
||||
privileges: ["ALL"],
|
||||
},
|
||||
];
|
||||
|
||||
if (!isPrimaryUserExisting) {
|
||||
const insertPrimaryMariadbUser = await dbHandler({
|
||||
query: `INSERT INTO mariadb_users (user_id, username, password, \`primary\`, grants) VALUES (?, ?, ?, ?, ?)`,
|
||||
values: [
|
||||
id,
|
||||
dslUsername,
|
||||
encryptedPassword,
|
||||
"1",
|
||||
JSON.stringify(primaryUserGrants),
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////
|
||||
|
||||
const existingExtraMariadbUsers = await dbHandler({
|
||||
query: `SELECT * FROM mariadb_users WHERE user_id = ? AND \`primary\` != '1'`,
|
||||
values: [id],
|
||||
});
|
||||
|
||||
if (Array.isArray(existingExtraMariadbUsers)) {
|
||||
for (let i = 0; i < existingExtraMariadbUsers.length; i++) {
|
||||
const mariadbUser = existingExtraMariadbUsers[i];
|
||||
const {
|
||||
user_id,
|
||||
username,
|
||||
host,
|
||||
password,
|
||||
primary,
|
||||
grants,
|
||||
} = mariadbUser;
|
||||
|
||||
if (mariadbUser && username != mariadbUser) continue;
|
||||
if (mariadbUserHost && host != mariadbUserHost) continue;
|
||||
|
||||
const decrptedPassword = decrypt({
|
||||
encryptedString: password,
|
||||
encryptionKey: process.env.DSQL_ENCRYPTION_PASSWORD,
|
||||
encryptionSalt: process.env.DSQL_ENCRYPTION_SALT,
|
||||
});
|
||||
|
||||
const existingExtraMariadbUser = await noDatabaseDbHandler(
|
||||
`SELECT * FROM mysql.user WHERE User = '${username}' AND Host = '${host}'`
|
||||
);
|
||||
|
||||
const isExtraMariadbUserExisting = Boolean(
|
||||
existingExtraMariadbUser?.[0]?.User
|
||||
);
|
||||
|
||||
if (!isExtraMariadbUserExisting) {
|
||||
await noDatabaseDbHandler(
|
||||
`CREATE USER IF NOT EXISTS '${username}'@'${host}' IDENTIFIED BY '${decrptedPassword}' REQUIRE SSL`
|
||||
);
|
||||
}
|
||||
|
||||
const isGrantHandled = await handleGrants({
|
||||
username,
|
||||
host,
|
||||
grants:
|
||||
grants && typeof grants == "string"
|
||||
? JSON.parse(grants)
|
||||
: [],
|
||||
userId: String(userId),
|
||||
});
|
||||
|
||||
if (!isGrantHandled) {
|
||||
console.log(
|
||||
`Error in handling grants for user ${username}@${host}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////
|
||||
//////////////////////////////////////////////
|
||||
//////////////////////////////////////////////
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(`Error in adding SQL user =>`, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
process.exit();
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
}
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
module.exports = refreshUsersAndGrants;
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
// @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();
|
||||
+185
@@ -0,0 +1,185 @@
|
||||
// @ts-check
|
||||
|
||||
const path = require("path");
|
||||
require("dotenv").config({ path: "../../../.env" });
|
||||
const fs = require("fs");
|
||||
const { execSync } = require("child_process");
|
||||
const EJSON = require("../../../utils/ejson");
|
||||
const DB_HANDLER = require("../../../utils/backend/global-db/DB_HANDLER");
|
||||
const addDbEntry = require("../../../functions/backend/db/addDbEntry");
|
||||
const addMariadbUser = require("../../../functions/backend/addMariadbUser");
|
||||
const updateDbEntry = require("../../../functions/backend/db/updateDbEntry");
|
||||
const hashPassword = require("../../../functions/dsql/hashPassword");
|
||||
|
||||
const tmpDir = process.argv[process.argv.length - 1];
|
||||
|
||||
/**
|
||||
* # Create New User
|
||||
*/
|
||||
async function createUser() {
|
||||
/**
|
||||
* Validate Form
|
||||
*
|
||||
* @description Check if request body is valid
|
||||
*/
|
||||
try {
|
||||
const isTmpDir = Boolean(tmpDir?.match(/\.json$/));
|
||||
const targetPath = isTmpDir
|
||||
? path.resolve(process.cwd(), tmpDir)
|
||||
: path.resolve(__dirname, "./new-user.json");
|
||||
|
||||
const userObj = EJSON.parse(fs.readFileSync(targetPath, "utf-8"));
|
||||
|
||||
if (typeof userObj !== "object" || Array.isArray(userObj))
|
||||
throw new Error("User Object Invalid!");
|
||||
|
||||
const ROOT_DIR = path.resolve(__dirname, "../../../");
|
||||
|
||||
/**
|
||||
* Validate Form
|
||||
*
|
||||
* @description Check if request body is valid
|
||||
*/
|
||||
const first_name = userObj.first_name;
|
||||
const last_name = userObj.last_name;
|
||||
const email = userObj.email;
|
||||
const password = userObj.password;
|
||||
const username = userObj.username;
|
||||
|
||||
if (!email?.match(/.*@.*\..*/)) return false;
|
||||
|
||||
if (
|
||||
!first_name?.match(/^[a-zA-Z]+$/) ||
|
||||
!last_name?.match(/^[a-zA-Z]+$/)
|
||||
)
|
||||
return false;
|
||||
|
||||
if (password?.match(/ /)) return false;
|
||||
|
||||
if (username?.match(/ /)) return false;
|
||||
|
||||
let hashedPassword = hashPassword({
|
||||
encryptionKey: process.env.DSQL_ENCRYPTION_PASSWORD || "",
|
||||
password: password,
|
||||
});
|
||||
|
||||
let existingUser = await DB_HANDLER(
|
||||
`SELECT * FROM users WHERE email='${email}'`
|
||||
);
|
||||
|
||||
if (existingUser?.[0]) {
|
||||
console.log("User Exists");
|
||||
return false;
|
||||
}
|
||||
|
||||
const newUser = await addDbEntry({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "users",
|
||||
data: { ...userObj, password: hashedPassword },
|
||||
});
|
||||
|
||||
if (!newUser?.insertId) return false;
|
||||
|
||||
/**
|
||||
* Add a Mariadb User for this User
|
||||
*/
|
||||
await addMariadbUser({ userId: newUser.insertId });
|
||||
|
||||
const STATIC_ROOT = process.env.DSQL_STATIC_SERVER_DIR;
|
||||
|
||||
if (!STATIC_ROOT) {
|
||||
console.log("Static File ENV not Found!");
|
||||
throw new Error("No Static Path");
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new user folder and file
|
||||
*
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
let newUserSchemaFolderPath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${newUser.insertId}`;
|
||||
let newUserMediaFolderPath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}`
|
||||
);
|
||||
|
||||
fs.mkdirSync(newUserSchemaFolderPath, { recursive: true });
|
||||
fs.mkdirSync(newUserMediaFolderPath, { recursive: true });
|
||||
|
||||
fs.writeFileSync(
|
||||
`${newUserSchemaFolderPath}/main.json`,
|
||||
JSON.stringify([]),
|
||||
"utf8"
|
||||
);
|
||||
|
||||
const imageBasePath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}`
|
||||
);
|
||||
|
||||
if (!fs.existsSync(imageBasePath)) {
|
||||
fs.mkdirSync(imageBasePath, { recursive: true });
|
||||
}
|
||||
|
||||
let imagePath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}/user-${newUser.insertId}-profile.jpg`
|
||||
);
|
||||
|
||||
let imageThumbnailPath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}/user-${newUser.insertId}-profile-thumbnail.jpg`
|
||||
);
|
||||
|
||||
let prodImageUrl = imagePath.replace(
|
||||
STATIC_ROOT,
|
||||
process.env.DSQL_STATIC_HOST || ""
|
||||
);
|
||||
let prodImageThumbnailUrl = imageThumbnailPath.replace(
|
||||
STATIC_ROOT,
|
||||
process.env.DSQL_STATIC_HOST || ""
|
||||
);
|
||||
|
||||
fs.copyFileSync(
|
||||
path.join(ROOT_DIR, "/public/images/user-preset.png"),
|
||||
imagePath
|
||||
);
|
||||
fs.copyFileSync(
|
||||
path.join(ROOT_DIR, "/public/images/user-preset-thumbnail.png"),
|
||||
imageThumbnailPath
|
||||
);
|
||||
|
||||
execSync(`chmod 644 ${imagePath} ${imageThumbnailPath}`);
|
||||
|
||||
const updateImages = await updateDbEntry({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "users",
|
||||
identifierColumnName: "id",
|
||||
identifierValue: newUser.insertId,
|
||||
data: {
|
||||
image: prodImageUrl,
|
||||
image_thumbnail: prodImageThumbnailUrl,
|
||||
},
|
||||
});
|
||||
|
||||
if (isTmpDir) {
|
||||
try {
|
||||
fs.unlinkSync(path.resolve(process.cwd(), tmpDir));
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(`Error in creating user => ${error.message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
createUser().then((res) => {
|
||||
if (res) {
|
||||
console.log("User Creation Success!!!");
|
||||
} else {
|
||||
console.log("User Creation Failed!");
|
||||
}
|
||||
process.exit();
|
||||
});
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
// @ts-check
|
||||
|
||||
const path = require("path");
|
||||
require("dotenv").config({ path: "../../../.env" });
|
||||
const fs = require("fs");
|
||||
const EJSON = require("../../../utils/ejson");
|
||||
const hashPassword = require("../../../functions/dsql/hashPassword");
|
||||
const updateDbEntry = require("../../../functions/backend/db/updateDbEntry");
|
||||
|
||||
const tmpDir = process.argv[process.argv.length - 1];
|
||||
|
||||
/**
|
||||
* # Create New User
|
||||
*/
|
||||
async function createUser() {
|
||||
/**
|
||||
* Validate Form
|
||||
*
|
||||
* @description Check if request body is valid
|
||||
*/
|
||||
try {
|
||||
const isTmpDir = Boolean(tmpDir?.match(/\.json$/));
|
||||
const targetPath = isTmpDir
|
||||
? path.resolve(process.cwd(), tmpDir)
|
||||
: path.resolve(__dirname, "./update-user.json");
|
||||
const updateUserObj = EJSON.parse(fs.readFileSync(targetPath, "utf-8"));
|
||||
|
||||
if (typeof updateUserObj !== "object" || Array.isArray(updateUserObj))
|
||||
throw new Error("Update User Object Invalid!");
|
||||
|
||||
let hashedPassword = updateUserObj.password
|
||||
? hashPassword({
|
||||
encryptionKey: process.env.DSQL_ENCRYPTION_PASSWORD || "",
|
||||
password: updateUserObj.password,
|
||||
})
|
||||
: undefined;
|
||||
|
||||
let updatePayload = { ...updateUserObj };
|
||||
if (hashedPassword) {
|
||||
updatePayload["password"] = hashedPassword;
|
||||
}
|
||||
|
||||
/** @type {any} */
|
||||
const newUser = await updateDbEntry({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "users",
|
||||
data: { ...updatePayload, id: undefined },
|
||||
identifierColumnName: "id",
|
||||
identifierValue: updatePayload.id,
|
||||
});
|
||||
|
||||
if (!newUser?.affectedRows) return false;
|
||||
|
||||
if (isTmpDir) {
|
||||
try {
|
||||
fs.unlinkSync(path.resolve(process.cwd(), tmpDir));
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(`Error in creating user => ${error.message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
createUser().then((res) => {
|
||||
if (res) {
|
||||
console.log("User Update Success!!!");
|
||||
} else {
|
||||
console.log("User Update Failed!");
|
||||
}
|
||||
process.exit();
|
||||
});
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"id": "1",
|
||||
"verification_status": "1"
|
||||
}
|
||||
Reference in New Issue
Block a user