"use strict"; (() => { var exports = {}; exports.id = 3363; exports.ids = [3363]; exports.modules = { /***/ 3785: /***/ ((module) => { module.exports = require("generate-password"); /***/ }), /***/ 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"); /***/ }), /***/ 8210: /***/ ((__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 path__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(1017); /* harmony import */ var path__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(path__WEBPACK_IMPORTED_MODULE_3__); /* harmony import */ var child_process__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(2081); /* harmony import */ var child_process__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(child_process__WEBPACK_IMPORTED_MODULE_4__); /* harmony import */ var _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(2224); /* harmony import */ var _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_5___default = /*#__PURE__*/__webpack_require__.n(_package_shared_utils_backend_global_db_DB_HANDLER__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); const generator = __webpack_require__(3785); //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** * 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 , grants } = req.body; /** * Validate Form * * @description Check if request body is valid */ try { const defaultMariadbUserHost = process.env.DSQL_DB_HOST || "127.0.0.1"; //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// const username = `dsql_user_${user.id}`; const { host , password } = data; if (!host.match(/./)) { throw new Error("Invalid host"); } 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, ""); const finPassword = password?.match(/./) ? password : generator.generate({ length: 16, numbers: true, symbols: true, uppercase: true, exclude: "*#.'`\"" }); //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// const existinSQLUser = await _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_5___default()(`SELECT * FROM mysql.user WHERE User = ? AND (Host = ? OR Host = ?)`, [ username, sanitizedNewHost, defaultMariadbUserHost ]); const doesSQLUserExist = Boolean(existinSQLUser?.[0]?.User); if (doesSQLUserExist) { throw new Error("SQL User already exists. Remember if you have the '%' host on your primary user, it catches all other hosts."); } const absoluteHost = Boolean(sanitizedNewHost.match(/^%$/)); if (absoluteHost) { throw new Error("Can't create a catch-all host for this username. Catch-all host is only allowed on the primary user."); } //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// const existingUsersCount = await _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_5___default()(`SELECT COUNT(*) FROM mariadb_users WHERE user_id = ?`, [ user.id ]); if (existingUsersCount?.[0]?.["COUNT(*)"] >= 10) { throw new Error("User limit reached"); } const encryptedPassword = _package_shared_functions_backend_encrypt__WEBPACK_IMPORTED_MODULE_6___default()(finPassword); const newMariaDBUser = await _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_5___default()(`INSERT INTO mariadb_users (user_id, username, host, password, grants) VALUES (?, ?, ?, ?, ?)`, [ user.id, username, sanitizedNewHost, encryptedPassword, JSON.stringify(grants), ]); if (!newMariaDBUser?.insertId) { throw new Error("Error in adding SQL user"); } const execPath = path__WEBPACK_IMPORTED_MODULE_3___default().resolve(__dirname, "../../../shell/mariadb-users"); const execSQLUserScript = (0,child_process__WEBPACK_IMPORTED_MODULE_4__.execSync)(`node refreshUsersAndGrants.js --userId ${user.id} --username ${username} --host ${sanitizedNewHost}`, { 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)) var __webpack_exports__ = __webpack_require__.X(0, [5425,2224,2163,6825,3017,7547], () => (__webpack_exec__(8210))); module.exports = __webpack_exports__; })();