dsql-admin/dsql-app/.local_dist/server/pages/api/sql-users/update.js

219 lines
9.0 KiB
JavaScript
Raw Normal View History

2024-11-05 11:12:42 +00:00
"use strict";
(() => {
var exports = {};
exports.id = 9685;
exports.ids = [9685];
exports.modules = {
/***/ 6517:
/***/ ((module) => {
module.exports = require("lodash");
/***/ }),
/***/ 2261:
/***/ ((module) => {
module.exports = require("serverless-mysql");
/***/ }),
/***/ 4300:
/***/ ((module) => {
module.exports = require("buffer");
/***/ }),
/***/ 2081:
/***/ ((module) => {
module.exports = require("child_process");
/***/ }),
/***/ 6113:
/***/ ((module) => {
module.exports = require("crypto");
/***/ }),
/***/ 7147:
/***/ ((module) => {
module.exports = require("fs");
/***/ }),
/***/ 3685:
/***/ ((module) => {
module.exports = require("http");
/***/ }),
/***/ 1017:
/***/ ((module) => {
module.exports = require("path");
/***/ }),
/***/ 2287:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "default": () => (/* binding */ handler)
/* harmony export */ });
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(6825);
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(2163);
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(6517);
/* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(lodash__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(2224);
/* harmony import */ var _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(_package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_3__);
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(1017);
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(path__WEBPACK_IMPORTED_MODULE_4__);
/* harmony import */ var child_process__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(2081);
/* harmony import */ var child_process__WEBPACK_IMPORTED_MODULE_5___default = /*#__PURE__*/__webpack_require__.n(child_process__WEBPACK_IMPORTED_MODULE_5__);
/* harmony import */ var _package_shared_functions_backend_encrypt__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(7547);
/* harmony import */ var _package_shared_functions_backend_encrypt__WEBPACK_IMPORTED_MODULE_6___default = /*#__PURE__*/__webpack_require__.n(_package_shared_functions_backend_encrypt__WEBPACK_IMPORTED_MODULE_6__);
// @ts-check
const fs = __webpack_require__(7147);
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
* API handler
* ==============================================================================
* @type {import("next").NextApiHandler}
*/ async function handler(req, res) {
/**
* Check method
*
* @description Check request method and return if invalid
*/ if (req.method !== "POST") return res.json({
user: null,
msg: "Registration Failed!"
});
/**
* User Auth
*
* @description User Auth
*/ const user = await _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_0___default()(req, res, true);
if (!user) {
return res.json({
success: false,
msg: "Unauthorized"
});
}
/**
* Validate Form
*
* @description Check if request body is valid
*/ const { data , edit , prev , grants } = req.body;
/**
* @type {import("@/package-shared/types").MYSQL_mariadb_users_table_def | undefined}
*/ const existingMariaDBUser = prev;
/**
* Validate Form
*
* @description Check if request body is valid
*/ try {
if (!existingMariaDBUser?.id) {
throw new Error("Invalid existing DB entry");
}
const existingMariaDBUserArray = await _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_3___default()(`SELECT * FROM mariadb_users WHERE id = ? AND user_id = ?`, [
existingMariaDBUser.id,
user.id
]);
/**
* @type {import("@/package-shared/types").MYSQL_mariadb_users_table_def | undefined}
*/ const activeMariadbUserObject = existingMariaDBUserArray?.[0];
if (!activeMariadbUserObject?.id) {
throw new Error("Active User couldn't be verified");
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
const username = `dsql_user_${user.id}`;
const { host , password } = data;
if (!host) {
throw new Error("No host provided");
}
if (host.match(/^%$/)) {
throw new Error("Cannot set a catch-all host for this user.");
}
const sanitizedNewHost = String(host).replace(/[^a-zA-Z0-9-\.\:\/\%]/g, "");
if (activeMariadbUserObject.host != data.host) {
const existinUser = await _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_3___default()(`SELECT * FROM mariadb_users WHERE user_id = ? AND host = ?`, [
user.id,
sanitizedNewHost
]);
if (!existinUser?.[0]?.id) {
const renameUser = await _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_3___default()(`RENAME USER '${username}'@'${activeMariadbUserObject.host}' TO '${username}'@'${sanitizedNewHost}'`);
} else {
throw new Error("User already exists with this host. Please choose another host.");
}
}
const encryptedPassword = _package_shared_functions_backend_encrypt__WEBPACK_IMPORTED_MODULE_6___default()(password);
const finalPassword = encryptedPassword == activeMariadbUserObject.password ? activeMariadbUserObject.password : encryptedPassword;
const updateUserEntry = await _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_3___default()(`UPDATE mariadb_users SET host = ?, password = ?, grants = ? WHERE id = ?`, [
sanitizedNewHost,
finalPassword,
JSON.stringify(grants),
activeMariadbUserObject.id,
]);
const updateSQLUserPassword = await _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_3___default()(`SET PASSWORD FOR '${username}'@'${sanitizedNewHost}' = PASSWORD('${password}');`);
2024-11-06 09:37:01 +00:00
const execPath = path__WEBPACK_IMPORTED_MODULE_4___default().resolve(process.cwd(), "./shell/mariadb-users");
2024-11-05 11:12:42 +00:00
const execSQLUserScript = (0,child_process__WEBPACK_IMPORTED_MODULE_5__.execSync)(`node refreshUsersAndGrants.js --userId ${user.id} --username ${username} --host ${sanitizedNewHost} --sql-user-id ${activeMariadbUserObject.id}}`, {
cwd: execPath
});
res.json({
success: true
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} catch (/** @type {any} */ error) {
_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1___default()({
component: "/api/acceptUserInvitation/catch-error",
message: error.message,
user: user
});
res.json({
success: false,
msg: "Error in adding SQL user =>" + error.message,
err: error.message
});
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
}
/***/ })
};
;
// load runtime
var __webpack_require__ = require("../../../webpack-api-runtime.js");
__webpack_require__.C(exports);
var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
2024-11-05 14:18:40 +00:00
var __webpack_exports__ = __webpack_require__.X(0, [2224,2163,6825,3017,7547], () => (__webpack_exec__(2287)));
2024-11-05 11:12:42 +00:00
module.exports = __webpack_exports__;
})();