58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
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 });
|
|
|
|
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,
|
|
};
|
|
}
|