53 lines
2.3 KiB
JavaScript
53 lines
2.3 KiB
JavaScript
import dbHandler from "../../backend/dbHandler";
|
|
import normalizeText from "../../../utils/normalize-text";
|
|
import decrypt from "../../dsql/decrypt";
|
|
export default async function handleMariadbUserCreation({ user, existingRecord, updatedRecord, }) {
|
|
const parsedPassword = decrypt({
|
|
encryptedString: (updatedRecord === null || updatedRecord === void 0 ? void 0 : updatedRecord.password) || "",
|
|
});
|
|
if ((existingRecord === null || existingRecord === void 0 ? void 0 : existingRecord.id) && (updatedRecord === null || updatedRecord === void 0 ? void 0 : updatedRecord.id)) {
|
|
if (existingRecord.username !== updatedRecord.username ||
|
|
existingRecord.host !== updatedRecord.host) {
|
|
const renameSQLUser = await dbHandler({
|
|
query: normalizeText(`
|
|
RENAME USER '${existingRecord.username}'@'${existingRecord.host}' \
|
|
TO '${updatedRecord.username}'@'${updatedRecord.host}'
|
|
`),
|
|
});
|
|
if (!renameSQLUser) {
|
|
await createNewSQLUser({
|
|
host: updatedRecord.host,
|
|
password: parsedPassword,
|
|
username: updatedRecord.username,
|
|
});
|
|
}
|
|
}
|
|
const updateSQLUser = await dbHandler({
|
|
query: normalizeText(`
|
|
ALTER USER '${updatedRecord.username}'@'${updatedRecord.host}' \
|
|
IDENTIFIED BY '${parsedPassword}'
|
|
`),
|
|
});
|
|
if (!updateSQLUser) {
|
|
await createNewSQLUser({
|
|
host: updatedRecord.host,
|
|
password: parsedPassword,
|
|
username: updatedRecord.username,
|
|
});
|
|
}
|
|
}
|
|
else if (!(existingRecord === null || existingRecord === void 0 ? void 0 : existingRecord.id) && (updatedRecord === null || updatedRecord === void 0 ? void 0 : updatedRecord.id)) {
|
|
await createNewSQLUser({
|
|
host: updatedRecord.host,
|
|
password: parsedPassword,
|
|
username: updatedRecord.username,
|
|
});
|
|
}
|
|
return { success: true };
|
|
}
|
|
export async function createNewSQLUser({ host, password, username, }) {
|
|
return await dbHandler({
|
|
query: `CREATE USER IF NOT EXISTS '${username}'@'${host}' IDENTIFIED BY '${password}'`,
|
|
});
|
|
}
|