301 lines
12 KiB
JavaScript
301 lines
12 KiB
JavaScript
"use strict";
|
|
(() => {
|
|
var exports = {};
|
|
exports.id = 2316;
|
|
exports.ids = [2316];
|
|
exports.modules = {
|
|
|
|
/***/ 6517:
|
|
/***/ ((module) => {
|
|
|
|
module.exports = require("lodash");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 6109:
|
|
/***/ ((module) => {
|
|
|
|
module.exports = require("sanitize-html");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 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");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 4577:
|
|
/***/ ((__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 _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2224);
|
|
/* harmony import */ var _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_package_shared_utils_backend_global_db_DB_HANDLER__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 _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(6825);
|
|
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_2__);
|
|
/* harmony import */ var _package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(5338);
|
|
/* harmony import */ var _package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(_package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_3__);
|
|
/* harmony import */ var _package_shared_functions_backend_noDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(9132);
|
|
/* harmony import */ var _package_shared_functions_backend_noDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(_package_shared_functions_backend_noDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_4__);
|
|
// @ts-check
|
|
/**
|
|
* ==============================================================================
|
|
* Imports
|
|
* ==============================================================================
|
|
*/ const fs = __webpack_require__(7147);
|
|
const path = __webpack_require__(1017);
|
|
const { execSync } = __webpack_require__(2081);
|
|
|
|
|
|
|
|
|
|
|
|
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
|
|
* 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({
|
|
msg: "Failed!"
|
|
});
|
|
/**
|
|
* User auth
|
|
*
|
|
* @description Authenticate user
|
|
*/ const user = await _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_2___default()(req, res, true);
|
|
if (!user) {
|
|
return res.json({
|
|
success: false,
|
|
msg: "Unauthorized"
|
|
});
|
|
}
|
|
const fullUserArray = await _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_0___default()(`SELECT * FROM users WHERE id = ?`, [
|
|
user.id,
|
|
]);
|
|
const fullUser = fullUserArray?.[0];
|
|
/**
|
|
* User auth
|
|
*
|
|
* @description Authenticate user
|
|
*/ const sanitizedReqBody = req.body;
|
|
const { db_name , db_slug , db_description , db_image , duplicate , keepUpdated , keepDataUpdated , } = sanitizedReqBody;
|
|
if (db_slug.match(/[^a-zA-Z0-9-_]/)) {
|
|
return res.json({
|
|
success: false,
|
|
msg: "Input Error"
|
|
});
|
|
}
|
|
const db_full_name = `datasquirel_user_${user.id}_${db_slug}`;
|
|
const createDatabaseForUser = await _package_shared_functions_backend_noDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_4___default()(`CREATE DATABASE IF NOT EXISTS \`${db_full_name}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`);
|
|
/**
|
|
* User auth
|
|
*
|
|
* @description Authenticate user
|
|
*/ if (!createDatabaseForUser?.affectedRows) {
|
|
return res.json({
|
|
success: false,
|
|
msg: "Database Could Not be created!"
|
|
});
|
|
}
|
|
/** @type {any} */ let newDbData = {
|
|
user_id: user.id,
|
|
db_name,
|
|
db_slug,
|
|
db_full_name,
|
|
db_description,
|
|
db_image
|
|
};
|
|
if (duplicate && keepUpdated) {
|
|
newDbData["active_clone"] = "1";
|
|
newDbData["active_clone_parent_db"] = duplicate;
|
|
}
|
|
if (duplicate && keepDataUpdated) {
|
|
newDbData["active_data"] = "1";
|
|
}
|
|
const newDatabaseEntry = await _package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_3___default()({
|
|
dbFullName: "datasquirel",
|
|
tableName: "user_databases",
|
|
data: newDbData
|
|
});
|
|
/**
|
|
* Send Response
|
|
*
|
|
* @description Send a boolean response
|
|
*/ if (newDatabaseEntry.insertId) {
|
|
try {
|
|
/**
|
|
* Create new user folder and file
|
|
*
|
|
* @description Create new user folder and file
|
|
*/ const userSchemaMainFilePath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${user.id}/main.json`;
|
|
/** @type {import("@/package-shared/types").DSQL_DatabaseSchemaType[]} */ let userSchemaData = JSON.parse(fs.readFileSync(userSchemaMainFilePath, "utf8"));
|
|
/** @type {any} */ let newDbSchemaObject = {
|
|
dbName: db_name,
|
|
dbSlug: db_slug,
|
|
dbFullName: db_full_name,
|
|
dbDescription: db_description,
|
|
dbImage: db_image,
|
|
tables: []
|
|
};
|
|
if (duplicate) {
|
|
try {
|
|
const parentDatabase = userSchemaData.filter((db)=>db.dbFullName === duplicate)[0];
|
|
newDbSchemaObject.tables = parentDatabase.tables;
|
|
if (keepUpdated) {
|
|
newDbSchemaObject["childDatabase"] = true;
|
|
newDbSchemaObject["childDatabaseDbFullName"] = duplicate;
|
|
if (keepDataUpdated) newDbSchemaObject["updateData"] = true;
|
|
if (parentDatabase.childrenDatabases) {
|
|
parentDatabase.childrenDatabases.push({
|
|
dbFullName: db_full_name
|
|
});
|
|
} else {
|
|
parentDatabase.childrenDatabases = [
|
|
{
|
|
dbFullName: db_full_name
|
|
},
|
|
];
|
|
}
|
|
}
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
for(let i = 0; i < newDbSchemaObject.tables.length; i++){
|
|
const newDbTable = newDbSchemaObject.tables[i];
|
|
const newDbTableRecord = await _package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_3___default()({
|
|
dbFullName: "datasquirel",
|
|
tableName: "user_database_tables",
|
|
data: {
|
|
user_id: user.id,
|
|
db_id: newDatabaseEntry.insertId,
|
|
db_slug: db_slug,
|
|
table_name: newDbTable.tableFullName,
|
|
table_slug: newDbTable.tableName
|
|
}
|
|
});
|
|
}
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
} catch (/** @type {any} */ error) {
|
|
_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1___default()({
|
|
component: "/api/createUserDatabase/lines-117-159",
|
|
message: error.message,
|
|
user: user
|
|
});
|
|
}
|
|
}
|
|
userSchemaData.push(newDbSchemaObject);
|
|
fs.writeFileSync(`${userSchemaMainFilePath}`, JSON.stringify(userSchemaData), "utf8");
|
|
////////////////////////////////////////
|
|
if (duplicate) {
|
|
const targetPath = path.join(process.cwd(), "/shell");
|
|
const dbShellUpdate = execSync(`node createDbFromSchema.js --user ${user.id} --database ${db_full_name}`, {
|
|
cwd: targetPath
|
|
});
|
|
}
|
|
////////////////////////////////////////
|
|
const grantDbPriviledges = await _package_shared_functions_backend_noDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_4___default()(`GRANT ALL PRIVILEGES ON ${db_full_name}.* TO '${process.env.DSQL_DB_FULL_ACCESS_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS || "%"}' WITH GRANT OPTION`);
|
|
if (grantDbPriviledges.error) {
|
|
throw new Error(grantDbPriviledges.error);
|
|
}
|
|
const grantRead = await _package_shared_functions_backend_noDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_4___default()(`GRANT SELECT ON ${db_full_name}.* TO '${process.env.DSQL_DB_READ_ONLY_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS || "%"}'`);
|
|
if (grantRead.error) {
|
|
throw new Error(grantRead.error);
|
|
}
|
|
if (fullUser?.mariadb_user && fullUser?.mariadb_pass) {
|
|
const grantDbPriviledgesToSqlUser = await _package_shared_functions_backend_noDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_4___default()(`GRANT ALL PRIVILEGES ON ${db_full_name}.* TO '${fullUser.mariadb_user}'@'${fullUser.mariadb_host}'`);
|
|
}
|
|
await _package_shared_functions_backend_noDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_4___default()(`FLUSH PRIVILEGES`);
|
|
////////////////////////////////////////
|
|
res.json({
|
|
success: true
|
|
});
|
|
////////////////////////////////////////
|
|
} catch (/** @type {any} */ error1) {
|
|
////////////////////////////////////////
|
|
_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1___default()({
|
|
component: "/api/createUserDatabase/main-catch-error",
|
|
message: error1.message,
|
|
user: user
|
|
});
|
|
res.json({
|
|
success: false,
|
|
msg: "Database Files could not be written!"
|
|
});
|
|
////////////////////////////////////////
|
|
}
|
|
} else {
|
|
res.json({
|
|
success: false,
|
|
msg: "Database Could Not Be Recorded!"
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
/***/ })
|
|
|
|
};
|
|
;
|
|
|
|
// 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, [2224,2163,6825,3017,3403,7547,5886,5338,7487,9132], () => (__webpack_exec__(4577)));
|
|
module.exports = __webpack_exports__;
|
|
|
|
})(); |