73 lines
1.9 KiB
TypeScript
73 lines
1.9 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[];
|
|
|
|
const updateUser = await dsqlCrud<
|
|
DSQL_DATASQUIREL_USERS,
|
|
(typeof DsqlTables)[number]
|
|
>({
|
|
action: "update",
|
|
table: "users",
|
|
targetField: "id",
|
|
targetValue: user.id,
|
|
data: {
|
|
mariadb_host: webHost,
|
|
mariadb_pass: encrypt({ data: newPassword }) || undefined,
|
|
mariadb_user: mariaDBUsername,
|
|
},
|
|
});
|
|
|
|
return {
|
|
fullName,
|
|
host,
|
|
username: mariaDBUsername,
|
|
password: newPassword,
|
|
};
|
|
}
|