import { generate } from "generate-password"; import dbHandler from "../functions/backend/dbHandler"; import dsqlCrud from "./data-fetching/crud"; import { DSQL_DATASQUIREL_USERS, DsqlTables } from "../types/dsql"; import encrypt from "../functions/dsql/encrypt"; import { UserType } from "../types"; import grabUserMainSqlUserName from "./grab-user-main-sql-user-name"; import grabDbNames from "./grab-db-names"; import { createNewSQLUser } from "../functions/web-app/mariadb-user/handle-mariadb-user-creation"; type Params = { user: UserType; }; type Return = { fullName?: string; host?: string; username?: string; password?: string; }; export default async function createUserSQLUser(user: UserType) { const { fullName, host, username: mariaDBUsername, webHost, } = grabUserMainSqlUserName({ user }); const { userDbPrefix } = grabDbNames({ user }); await dbHandler({ query: `DROP USER IF EXISTS '${mariaDBUsername}'@'${webHost}'`, noErrorLogs: true, }); const newPassword = generate({ length: 32 }); const updateUser = await dsqlCrud({ action: "update", table: "users", data: { mariadb_host: webHost, mariadb_user: mariaDBUsername, mariadb_pass: newPassword, }, targetId: user.id, }); if (!updateUser.success) { console.log("updateUser", updateUser); throw new Error(`Couldn't Update Users Table!`); } await createNewSQLUser({ host: webHost, password: newPassword, username: mariaDBUsername, }); const updateWebHostGrants = (await dbHandler({ query: `GRANT ALL PRIVILEGES ON \`${userDbPrefix.replace( /\_/g, "\\_" )}%\`.* TO '${mariaDBUsername}'@'${webHost}'`, })) as any[]; return { fullName, host, username: mariaDBUsername, password: newPassword, }; }