Updates
This commit is contained in:
@@ -1,137 +0,0 @@
|
||||
import generator from "generate-password";
|
||||
import noDatabaseDbHandler from "../utils/noDatabaseDbHandler";
|
||||
import dbHandler from "../utils/dbHandler";
|
||||
import handleGrants from "./handleGrants";
|
||||
import encrypt from "../../functions/dsql/encrypt";
|
||||
import decrypt from "../../functions/dsql/decrypt";
|
||||
import { DSQL_DATASQUIREL_MARIADB_USERS } from "../../types/dsql";
|
||||
import { MariaDBUser } from "../../types";
|
||||
|
||||
type Param = {
|
||||
userId?: number | string;
|
||||
mariadbUserHost?: string;
|
||||
mariadbUsername?: string;
|
||||
sqlUserID?: string | number;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Refresh Mariadb User Grants
|
||||
*/
|
||||
export default async function refreshUsersAndGrants({
|
||||
userId,
|
||||
mariadbUserHost,
|
||||
mariadbUsername,
|
||||
sqlUserID,
|
||||
}: Param) {
|
||||
const mariadbUsers = (await dbHandler({
|
||||
query: `SELECT * FROM mariadb_users`,
|
||||
})) as any[] | null;
|
||||
|
||||
if (!mariadbUsers?.[0]) {
|
||||
return;
|
||||
}
|
||||
|
||||
const isRootUser = userId
|
||||
? userId == Number(process.env.DSQL_SU_USER_ID)
|
||||
: false;
|
||||
|
||||
const isWildcardHost = mariadbUserHost == "%";
|
||||
|
||||
if (isWildcardHost && !isRootUser) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (let i = 0; i < mariadbUsers.length; i++) {
|
||||
const mariadbUser = mariadbUsers[i] as
|
||||
| DSQL_DATASQUIREL_MARIADB_USERS
|
||||
| undefined;
|
||||
|
||||
if (!mariadbUser) continue;
|
||||
if (userId && mariadbUser.user_id != userId) continue;
|
||||
if (sqlUserID && mariadbUser.id != sqlUserID) continue;
|
||||
|
||||
try {
|
||||
const { username, password, host, user_id } = mariadbUser;
|
||||
|
||||
const existingUser = await noDatabaseDbHandler(
|
||||
`SELECT * FROM mysql.user WHERE User = '${username}' AND Host = '${host}'`
|
||||
);
|
||||
|
||||
const isUserExisting = Boolean(existingUser?.[0]?.User);
|
||||
|
||||
const isPrimary = String(mariadbUser.primary)?.match(/1/)
|
||||
? true
|
||||
: false;
|
||||
|
||||
const dsqlPassword = mariadbUser?.password
|
||||
? decrypt({ encryptedString: mariadbUser.password })
|
||||
: isUserExisting && password
|
||||
? decrypt({ encryptedString: password })
|
||||
: generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
symbols: true,
|
||||
uppercase: true,
|
||||
exclude: "*#.'`\"",
|
||||
});
|
||||
|
||||
const encryptedPassword = mariadbUser?.password
|
||||
? mariadbUser.password
|
||||
: isUserExisting
|
||||
? password
|
||||
: encrypt({ data: dsqlPassword });
|
||||
|
||||
if (!isUserExisting) {
|
||||
if (isWildcardHost) {
|
||||
const _existingUsers = (await noDatabaseDbHandler(
|
||||
`SELECT * FROM mysql.user WHERE user='${mariadbUsername}'`
|
||||
)) as MariaDBUser[];
|
||||
|
||||
for (let i = 0; i < _existingUsers.length; i++) {
|
||||
const exUsr = _existingUsers[i];
|
||||
await noDatabaseDbHandler(
|
||||
`DROP USER '${exUsr.User}'@'${exUsr.Host}'`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const createNewUser = await noDatabaseDbHandler(
|
||||
`CREATE USER IF NOT EXISTS '${mariadbUsername}'@'${mariadbUserHost}' IDENTIFIED BY '${dsqlPassword}'`
|
||||
);
|
||||
}
|
||||
|
||||
if (isPrimary) {
|
||||
const updateUser = await dbHandler({
|
||||
query: `UPDATE users SET mariadb_user = ?, mariadb_host = ?, mariadb_pass = ? WHERE id = ?`,
|
||||
values: [
|
||||
mariadbUsername,
|
||||
mariadbUserHost,
|
||||
encryptedPassword,
|
||||
user_id,
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
const isGrantHandled = await handleGrants({
|
||||
username: mariadbUser.username,
|
||||
host: mariadbUser.host,
|
||||
grants:
|
||||
mariadbUser.grants && typeof mariadbUser.grants == "string"
|
||||
? JSON.parse(mariadbUser.grants)
|
||||
: [],
|
||||
userId: String(user_id),
|
||||
});
|
||||
|
||||
if (!isGrantHandled) {
|
||||
console.log(
|
||||
`Error in handling grants for user ${mariadbUser.username}@${mariadbUser.host}`
|
||||
);
|
||||
}
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(
|
||||
`Error Refreshing MariaDB Users and Grants`,
|
||||
error as Error
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
require("dotenv").config({ path: "../../.env" });
|
||||
import generator from "generate-password";
|
||||
import noDatabaseDbHandler from "../utils/noDatabaseDbHandler";
|
||||
import dbHandler from "../utils/dbHandler";
|
||||
import dbHandler from "../../functions/backend/dbHandler";
|
||||
import encrypt from "../../functions/dsql/encrypt";
|
||||
import grabSQLKeyName from "../../utils/grab-sql-key-name";
|
||||
|
||||
/**
|
||||
* # Reset SQL Passwords
|
||||
@@ -23,7 +24,9 @@ async function resetSQLCredentialsPasswords() {
|
||||
|
||||
try {
|
||||
const maridbUsers = (await dbHandler({
|
||||
query: `SELECT * FROM mysql.user WHERE User = 'dsql_user_${user.id}'`,
|
||||
query: `SELECT * FROM mysql.user WHERE User = '${grabSQLKeyName(
|
||||
{ type: "user", userId: user.id }
|
||||
)}'`,
|
||||
})) as any[];
|
||||
|
||||
for (let j = 0; j < maridbUsers.length; j++) {
|
||||
|
||||
@@ -8,6 +8,8 @@ import addDbEntry from "../../../functions/backend/db/addDbEntry";
|
||||
import addMariadbUser from "../../../functions/backend/addMariadbUser";
|
||||
import updateDbEntry from "../../../functions/backend/db/updateDbEntry";
|
||||
import hashPassword from "../../../functions/dsql/hashPassword";
|
||||
import { DSQL_DATASQUIREL_USERS } from "../../../types/dsql";
|
||||
import grabDirNames from "../../../utils/backend/names/grab-dir-names";
|
||||
|
||||
const tmpDir = process.argv[process.argv.length - 1];
|
||||
|
||||
@@ -76,14 +78,14 @@ async function createUser() {
|
||||
data: { ...userObj, password: hashedPassword },
|
||||
});
|
||||
|
||||
if (!newUser?.insertId) return false;
|
||||
if (!newUser?.payload?.insertId) return false;
|
||||
|
||||
/**
|
||||
* Add a Mariadb User for this User
|
||||
*/
|
||||
await addMariadbUser({ userId: newUser.insertId });
|
||||
await addMariadbUser({ userId: newUser.payload.insertId });
|
||||
|
||||
const STATIC_ROOT = process.env.DSQL_STATIC_SERVER_DIR;
|
||||
const { STATIC_ROOT } = grabDirNames();
|
||||
|
||||
if (!STATIC_ROOT) {
|
||||
console.log("Static File ENV not Found!");
|
||||
@@ -95,10 +97,10 @@ async function createUser() {
|
||||
*
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
let newUserSchemaFolderPath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${newUser.insertId}`;
|
||||
let newUserSchemaFolderPath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${newUser.payload.insertId}`;
|
||||
let newUserMediaFolderPath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}`
|
||||
`images/user-images/user-${newUser.payload.insertId}`
|
||||
);
|
||||
|
||||
fs.mkdirSync(newUserSchemaFolderPath, { recursive: true });
|
||||
@@ -112,7 +114,7 @@ async function createUser() {
|
||||
|
||||
const imageBasePath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}`
|
||||
`images/user-images/user-${newUser.payload.insertId}`
|
||||
);
|
||||
|
||||
if (!fs.existsSync(imageBasePath)) {
|
||||
@@ -121,12 +123,12 @@ async function createUser() {
|
||||
|
||||
let imagePath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}/user-${newUser.insertId}-profile.jpg`
|
||||
`images/user-images/user-${newUser.payload.insertId}/user-${newUser.payload.insertId}-profile.jpg`
|
||||
);
|
||||
|
||||
let imageThumbnailPath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}/user-${newUser.insertId}-profile-thumbnail.jpg`
|
||||
`images/user-images/user-${newUser.payload.insertId}/user-${newUser.payload.insertId}-profile-thumbnail.jpg`
|
||||
);
|
||||
|
||||
let prodImageUrl = imagePath.replace(
|
||||
@@ -149,11 +151,11 @@ async function createUser() {
|
||||
|
||||
execSync(`chmod 644 ${imagePath} ${imageThumbnailPath}`);
|
||||
|
||||
const updateImages = await updateDbEntry({
|
||||
const updateImages = await updateDbEntry<DSQL_DATASQUIREL_USERS>({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "users",
|
||||
identifierColumnName: "id",
|
||||
identifierValue: newUser.insertId,
|
||||
identifierValue: newUser.payload.insertId,
|
||||
data: {
|
||||
image: prodImageUrl,
|
||||
image_thumbnail: prodImageThumbnailUrl,
|
||||
|
||||
Reference in New Issue
Block a user