Updates
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { DsqlCrudQueryObject } from "../../../../types";
|
||||
import { DsqlTables } from "../../../../types/dsql";
|
||||
import dsqlCrud from "../../../../utils/data-fetching/crud";
|
||||
import query from "./query";
|
||||
import _ from "lodash";
|
||||
import _n from "../../../../utils/numberfy";
|
||||
|
||||
export type GrabUserResourceParams<T extends { [k: string]: any } = any> = {
|
||||
query?: DsqlCrudQueryObject<T>;
|
||||
userId?: string | number;
|
||||
tableName: (typeof DsqlTables)[number];
|
||||
count?: boolean;
|
||||
countOnly?: boolean;
|
||||
noLimit?: boolean;
|
||||
isSuperUser?: boolean;
|
||||
targetID?: string | number;
|
||||
};
|
||||
|
||||
export default async function dbGrabUserResource<
|
||||
T extends { [k: string]: any } = any
|
||||
>(params: GrabUserResourceParams<T>) {
|
||||
let queryObject = query(params);
|
||||
|
||||
let result = await dsqlCrud({
|
||||
action: "get",
|
||||
table: params.tableName,
|
||||
query: queryObject,
|
||||
count: params.count,
|
||||
countOnly: params.countOnly,
|
||||
});
|
||||
|
||||
const payload = result?.payload as T[] | undefined;
|
||||
|
||||
return {
|
||||
batch: payload || null,
|
||||
single: payload?.[0] || null,
|
||||
debug: {
|
||||
queryObject: result?.queryObject,
|
||||
error: result?.error,
|
||||
msg: result?.msg,
|
||||
},
|
||||
count: _n(result?.count),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { GrabUserResourceParams } from ".";
|
||||
import _ from "lodash";
|
||||
import { DsqlCrudQueryObject } from "../../../../types";
|
||||
import ResourceLimits from "../../../../dict/resource-limits";
|
||||
import _n from "../../../../utils/numberfy";
|
||||
|
||||
export default function (params?: GrabUserResourceParams) {
|
||||
let queryObject: DsqlCrudQueryObject<{ [k: string]: any }> = {
|
||||
limit: params?.noLimit ? undefined : ResourceLimits["general"],
|
||||
order: {
|
||||
field: "id",
|
||||
strategy: "DESC",
|
||||
},
|
||||
};
|
||||
|
||||
if (params?.targetID) {
|
||||
const targetIDQuery: DsqlCrudQueryObject<{ [k: string]: any }> = {
|
||||
query: {
|
||||
id: {
|
||||
value: _n(params.targetID).toString(),
|
||||
},
|
||||
},
|
||||
};
|
||||
queryObject = _.merge(queryObject, targetIDQuery);
|
||||
}
|
||||
|
||||
let queryFixedObject: DsqlCrudQueryObject<{ [k: string]: any }> =
|
||||
params?.isSuperUser
|
||||
? {}
|
||||
: {
|
||||
query: {
|
||||
user_id: {
|
||||
value: String(params?.userId || 0),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
return _.merge(queryObject, params?.query, queryFixedObject);
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import { DSQL_DATASQUIREL_MARIADB_USERS } from "../../../types/dsql";
|
||||
import { UserType } from "../../../types";
|
||||
import dbHandler from "../../backend/dbHandler";
|
||||
import normalizeText from "../../../utils/normalize-text";
|
||||
import decrypt from "../../dsql/decrypt";
|
||||
|
||||
type Params = {
|
||||
user: UserType;
|
||||
existingRecord?: DSQL_DATASQUIREL_MARIADB_USERS | null;
|
||||
updatedRecord: DSQL_DATASQUIREL_MARIADB_USERS;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
msg?: string;
|
||||
success?: boolean;
|
||||
};
|
||||
|
||||
export default async function handleMariadbUserCreation({
|
||||
user,
|
||||
existingRecord,
|
||||
updatedRecord,
|
||||
}: Params): Promise<Return> {
|
||||
const parsedPassword = decrypt({
|
||||
encryptedString: updatedRecord?.password || "",
|
||||
});
|
||||
|
||||
if (existingRecord?.id && 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?.id && updatedRecord?.id) {
|
||||
await createNewSQLUser({
|
||||
host: updatedRecord.host,
|
||||
password: parsedPassword,
|
||||
username: updatedRecord.username,
|
||||
});
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
type CreateNewUserParams = {
|
||||
username?: string;
|
||||
host?: string;
|
||||
password?: string;
|
||||
};
|
||||
|
||||
export async function createNewSQLUser({
|
||||
host,
|
||||
password,
|
||||
username,
|
||||
}: CreateNewUserParams) {
|
||||
return await dbHandler({
|
||||
query: `CREATE USER IF NOT EXISTS '${username}'@'${host}' IDENTIFIED BY '${password}'`,
|
||||
});
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
import {
|
||||
DSQL_DATASQUIREL_MARIADB_USER_DATABASES,
|
||||
DSQL_DATASQUIREL_MARIADB_USER_PRIVILEGES,
|
||||
DSQL_DATASQUIREL_MARIADB_USER_TABLES,
|
||||
DSQL_DATASQUIREL_MARIADB_USERS,
|
||||
DsqlTables,
|
||||
} from "../../../types/dsql";
|
||||
import { UserType } from "../../../types";
|
||||
import dsqlCrud from "../../../utils/data-fetching/crud";
|
||||
import _n from "../../../utils/numberfy";
|
||||
|
||||
type Params = {
|
||||
user: UserType;
|
||||
updatedRecord: DSQL_DATASQUIREL_MARIADB_USERS;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
msg?: string;
|
||||
success?: boolean;
|
||||
};
|
||||
|
||||
export default async function handleMariadbUserGrantsForDatabasesCleanUpRecords({
|
||||
user,
|
||||
updatedRecord,
|
||||
}: Params): Promise<Return> {
|
||||
/**
|
||||
* # Clean up Records
|
||||
*/
|
||||
await dsqlCrud<
|
||||
DSQL_DATASQUIREL_MARIADB_USER_DATABASES,
|
||||
(typeof DsqlTables)[number]
|
||||
>({
|
||||
action: "delete",
|
||||
table: "mariadb_user_databases",
|
||||
deleteData: {
|
||||
user_id: user.id,
|
||||
mariadb_user_id: updatedRecord.id,
|
||||
},
|
||||
});
|
||||
|
||||
await dsqlCrud<
|
||||
DSQL_DATASQUIREL_MARIADB_USER_PRIVILEGES,
|
||||
(typeof DsqlTables)[number]
|
||||
>({
|
||||
action: "delete",
|
||||
table: "mariadb_user_privileges",
|
||||
deleteData: {
|
||||
user_id: user.id,
|
||||
mariadb_user_id: updatedRecord.id,
|
||||
},
|
||||
});
|
||||
|
||||
await dsqlCrud<
|
||||
DSQL_DATASQUIREL_MARIADB_USER_TABLES,
|
||||
(typeof DsqlTables)[number]
|
||||
>({
|
||||
action: "delete",
|
||||
table: "mariadb_user_tables",
|
||||
deleteData: {
|
||||
user_id: user.id,
|
||||
mariadb_user_id: updatedRecord.id,
|
||||
},
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
import { DSQL_DATASQUIREL_MARIADB_USERS } from "../../../types/dsql";
|
||||
import {
|
||||
DatabaseScopedAccessObject,
|
||||
UserSQLPermissions,
|
||||
UserType,
|
||||
} from "../../../types";
|
||||
import grabDbFullName from "../../../utils/grab-db-full-name";
|
||||
import dbHandler from "../../backend/dbHandler";
|
||||
import normalizeText from "../../../utils/normalize-text";
|
||||
|
||||
type Params = {
|
||||
currentAccessedDatabase: DatabaseScopedAccessObject;
|
||||
user: UserType;
|
||||
updatedRecord: DSQL_DATASQUIREL_MARIADB_USERS;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
msg?: string;
|
||||
success?: boolean;
|
||||
};
|
||||
|
||||
export default async function handleMariadbUserGrantsForDatabasesRecreateGrants({
|
||||
currentAccessedDatabase,
|
||||
user,
|
||||
updatedRecord,
|
||||
}: Params): Promise<Return> {
|
||||
const { accessedDatabase, dbSlug, allGrants, allTables, grants, tables } =
|
||||
currentAccessedDatabase;
|
||||
|
||||
const dbFullName = grabDbFullName({
|
||||
user,
|
||||
dbName: dbSlug,
|
||||
});
|
||||
|
||||
if (allGrants && allTables) {
|
||||
const grantAllPrivileges = await dbHandler({
|
||||
query: normalizeText(`
|
||||
GRANT ALL PRIVILEGES ON \`${dbFullName}\`.* TO \
|
||||
'${updatedRecord.username}'@'${updatedRecord.host}'
|
||||
`),
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
if (allGrants && tables?.[0]) {
|
||||
for (let t = 0; t < tables.length; t++) {
|
||||
const table = tables[t];
|
||||
|
||||
// queries.push(
|
||||
// normalizeText(`
|
||||
// GRANT ALL PRIVILEGES ON \`${dbFullName}\`.\`${table.tableSlug}\` \
|
||||
// TO '${updatedRecord.username}'@'${updatedRecord.host}'
|
||||
// `)
|
||||
// );
|
||||
|
||||
const grantAllPrivilegesToTables = await dbHandler({
|
||||
query: normalizeText(`
|
||||
GRANT ALL PRIVILEGES ON \`${dbFullName}\`.\`${table.tableSlug}\` \
|
||||
TO '${updatedRecord.username}'@'${updatedRecord.host}'
|
||||
`),
|
||||
});
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
if (grants?.[0]) {
|
||||
const isGrantsInalid = grants.find(
|
||||
(g) => !UserSQLPermissions.includes(g)
|
||||
);
|
||||
|
||||
if (isGrantsInalid) {
|
||||
return { msg: `grants is/are invalid!` };
|
||||
}
|
||||
|
||||
if (tables?.[0]) {
|
||||
for (let t = 0; t < tables.length; t++) {
|
||||
const table = tables[t];
|
||||
|
||||
const grantSpecificPrivilegesToTables = await dbHandler({
|
||||
query: normalizeText(`
|
||||
GRANT ${grants.join(",")} ON \
|
||||
\`${dbFullName}\`.\`${table.tableSlug}\` TO \
|
||||
'${updatedRecord.username}'@'${updatedRecord.host}'
|
||||
`),
|
||||
});
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
} else {
|
||||
const grantSecificPrivilegesToAllTables = await dbHandler({
|
||||
query: normalizeText(`
|
||||
GRANT ${grants.join(",")} ON \
|
||||
\`${dbFullName}\`.* TO \
|
||||
'${updatedRecord.username}'@'${updatedRecord.host}'
|
||||
`),
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
import {
|
||||
DSQL_DATASQUIREL_MARIADB_USER_DATABASES,
|
||||
DSQL_DATASQUIREL_MARIADB_USER_PRIVILEGES,
|
||||
DSQL_DATASQUIREL_MARIADB_USER_TABLES,
|
||||
DSQL_DATASQUIREL_MARIADB_USERS,
|
||||
DsqlTables,
|
||||
} from "../../../types/dsql";
|
||||
import {
|
||||
DatabaseScopedAccessObject,
|
||||
UserSQLPermissions,
|
||||
UserType,
|
||||
} from "../../../types";
|
||||
import dsqlCrud from "../../../utils/data-fetching/crud";
|
||||
import _n from "../../../utils/numberfy";
|
||||
|
||||
type Params = {
|
||||
currentAccessedDatabase: DatabaseScopedAccessObject;
|
||||
user: UserType;
|
||||
updatedRecord: DSQL_DATASQUIREL_MARIADB_USERS;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
msg?: string;
|
||||
success?: boolean;
|
||||
};
|
||||
|
||||
export default async function handleMariadbUserGrantsForDatabasesRecreateRecordsForDatabase({
|
||||
currentAccessedDatabase,
|
||||
user,
|
||||
updatedRecord,
|
||||
}: Params): Promise<Return> {
|
||||
const { accessedDatabase, dbSlug, allGrants, allTables, grants, tables } =
|
||||
currentAccessedDatabase;
|
||||
|
||||
const insertSQLDbRecord = await dsqlCrud<
|
||||
DSQL_DATASQUIREL_MARIADB_USER_DATABASES,
|
||||
(typeof DsqlTables)[number]
|
||||
>({
|
||||
action: "insert",
|
||||
table: "mariadb_user_databases",
|
||||
data: {
|
||||
all_privileges: allGrants ? 1 : 0,
|
||||
all_tables: allTables ? 1 : 0,
|
||||
db_id: _n(accessedDatabase.dbId),
|
||||
db_slug: accessedDatabase.dbSlug,
|
||||
db_schema_id: _n(accessedDatabase.dbSchemaId),
|
||||
user_id: user.id,
|
||||
mariadb_user_id: updatedRecord.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (tables?.[0]) {
|
||||
for (let t = 0; t < tables.length; t++) {
|
||||
const table = tables[t];
|
||||
|
||||
const insertTable = await dsqlCrud<
|
||||
DSQL_DATASQUIREL_MARIADB_USER_TABLES,
|
||||
(typeof DsqlTables)[number]
|
||||
>({
|
||||
action: "insert",
|
||||
table: "mariadb_user_tables",
|
||||
data: {
|
||||
all_privileges: allGrants ? 1 : 0,
|
||||
all_fields: 1,
|
||||
user_id: user.id,
|
||||
mariadb_user_id: updatedRecord.id,
|
||||
table_slug: table.tableSlug,
|
||||
db_id: _n(table.dbId),
|
||||
db_slug: table.dbSlug,
|
||||
db_schema_id: _n(table.dbSchemaId),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (grants?.[0]) {
|
||||
const isGrantsInalid = grants.find(
|
||||
(g) => !UserSQLPermissions.includes(g)
|
||||
);
|
||||
|
||||
if (isGrantsInalid) {
|
||||
return { msg: `grants is/are invalid!` };
|
||||
}
|
||||
|
||||
for (let t = 0; t < grants.length; t++) {
|
||||
const grant = grants[t];
|
||||
|
||||
await dsqlCrud<
|
||||
DSQL_DATASQUIREL_MARIADB_USER_PRIVILEGES,
|
||||
(typeof DsqlTables)[number]
|
||||
>({
|
||||
action: "insert",
|
||||
table: "mariadb_user_privileges",
|
||||
data: {
|
||||
user_id: user.id,
|
||||
mariadb_user_id: updatedRecord.id,
|
||||
privilege: grant,
|
||||
db_id: _n(accessedDatabase.dbId),
|
||||
db_slug: accessedDatabase.dbSlug,
|
||||
db_schema_id: _n(accessedDatabase.dbSchemaId),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
import { DSQL_DATASQUIREL_MARIADB_USERS } from "../../../types/dsql";
|
||||
import { DatabaseScopedAccessObject, UserType } from "../../../types";
|
||||
import handleMariadbUserGrantsForDatabasesRecreateRecordsForDatabase from "./handle-mariadb-user-grants-for-databases-recreate-records";
|
||||
import handleMariadbUserGrantsForDatabasesRecreateGrants from "./handle-mariadb-user-grants-for-databases-recreate-grants";
|
||||
|
||||
type Params = {
|
||||
accessedDatabases: DatabaseScopedAccessObject[];
|
||||
user: UserType;
|
||||
updatedRecord: DSQL_DATASQUIREL_MARIADB_USERS;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
msg?: string;
|
||||
success?: boolean;
|
||||
};
|
||||
|
||||
export default async function handleMariadbUserGrantsForDatabases({
|
||||
accessedDatabases,
|
||||
user,
|
||||
updatedRecord,
|
||||
}: Params): Promise<Return> {
|
||||
/**
|
||||
* # Recreate Records
|
||||
*/
|
||||
for (let i = 0; i < accessedDatabases.length; i++) {
|
||||
await handleMariadbUserGrantsForDatabasesRecreateRecordsForDatabase({
|
||||
currentAccessedDatabase: accessedDatabases[i],
|
||||
updatedRecord,
|
||||
user,
|
||||
});
|
||||
|
||||
await handleMariadbUserGrantsForDatabasesRecreateGrants({
|
||||
currentAccessedDatabase: accessedDatabases[i],
|
||||
updatedRecord,
|
||||
user,
|
||||
});
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
import {
|
||||
AddUpdateMariadbUserAPIReqBody,
|
||||
UserSQLPermissions,
|
||||
UserType,
|
||||
} from "../../../types";
|
||||
import {
|
||||
DSQL_DATASQUIREL_MARIADB_USER_PRIVILEGES,
|
||||
DSQL_DATASQUIREL_MARIADB_USERS,
|
||||
DsqlTables,
|
||||
} from "../../../types/dsql";
|
||||
import dsqlCrud from "../../../utils/data-fetching/crud";
|
||||
import grabDbNames from "../../../utils/grab-db-names";
|
||||
import normalizeText from "../../../utils/normalize-text";
|
||||
import dbHandler from "../../backend/dbHandler";
|
||||
import handleMariadbUserGrantsForDatabases from "./handle-mariadb-user-grants-for-databases";
|
||||
import revokeAllExistingGrants from "./revoke-all-existing-grants";
|
||||
|
||||
type Params = AddUpdateMariadbUserAPIReqBody & {
|
||||
user: UserType;
|
||||
updatedRecord: DSQL_DATASQUIREL_MARIADB_USERS;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
msg?: string;
|
||||
success?: boolean;
|
||||
};
|
||||
|
||||
export default async function handleMariadbUserGrants({
|
||||
accessedDatabases,
|
||||
grants,
|
||||
isAllDbsAccess,
|
||||
isAllGrants,
|
||||
user,
|
||||
updatedRecord,
|
||||
}: Params): Promise<Return> {
|
||||
const { userDbPrefix } = grabDbNames({ user });
|
||||
|
||||
/**
|
||||
* # Revoke All Existing Grants
|
||||
*/
|
||||
await revokeAllExistingGrants({ updatedRecord, user });
|
||||
|
||||
/**
|
||||
* # Recreate Grants
|
||||
*/
|
||||
if (isAllGrants && isAllDbsAccess) {
|
||||
const grantAllPrivileges = await dbHandler({
|
||||
query: normalizeText(`
|
||||
GRANT ALL PRIVILEGES ON \
|
||||
\`${userDbPrefix.replace(/\_/g, "\\_")}%\`.* TO \
|
||||
'${updatedRecord.username}'@'${updatedRecord.host}'
|
||||
`),
|
||||
});
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
if (isAllDbsAccess && grants) {
|
||||
const isGrantsInalid = grants.find(
|
||||
(g) => !UserSQLPermissions.includes(g)
|
||||
);
|
||||
|
||||
if (isGrantsInalid) {
|
||||
return { msg: `grants is/are invalid!` };
|
||||
}
|
||||
|
||||
const grantQuery = normalizeText(`
|
||||
GRANT ${grants.join(",")} ON \`${userDbPrefix}%\`.* TO \
|
||||
'${updatedRecord.username}'@'${updatedRecord.host}'
|
||||
`);
|
||||
|
||||
const grantSpecificPrivilegesToAllDbs = await dbHandler({
|
||||
query: grantQuery,
|
||||
});
|
||||
|
||||
for (let t = 0; t < grants.length; t++) {
|
||||
const grant = grants[t];
|
||||
|
||||
const addGrant = await dsqlCrud<
|
||||
DSQL_DATASQUIREL_MARIADB_USER_PRIVILEGES,
|
||||
(typeof DsqlTables)[number]
|
||||
>({
|
||||
action: "insert",
|
||||
table: "mariadb_user_privileges",
|
||||
data: {
|
||||
user_id: user.id,
|
||||
mariadb_user_id: updatedRecord.id,
|
||||
privilege: grant,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
if (accessedDatabases?.[0]) {
|
||||
const res = await handleMariadbUserGrantsForDatabases({
|
||||
accessedDatabases,
|
||||
updatedRecord,
|
||||
user,
|
||||
});
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
import { AddUpdateMariadbUserAPIReqBody, UserType } from "../../../types";
|
||||
import {
|
||||
DSQL_DATASQUIREL_MARIADB_USERS,
|
||||
DsqlTables,
|
||||
} from "../../../types/dsql";
|
||||
import grabSQLUserName from "../../../utils/grab-sql-user-name";
|
||||
import addDbEntry from "../../backend/db/addDbEntry";
|
||||
import encrypt from "../../dsql/encrypt";
|
||||
import dbGrabUserResource from "../db/grab-user-resource";
|
||||
|
||||
type Params = AddUpdateMariadbUserAPIReqBody & {
|
||||
user: UserType;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
existingRecord?: DSQL_DATASQUIREL_MARIADB_USERS | null;
|
||||
updatedRecord?: DSQL_DATASQUIREL_MARIADB_USERS | null;
|
||||
msg?: string;
|
||||
};
|
||||
|
||||
export default async function handleMariadbUserRecord({
|
||||
mariadbUser,
|
||||
accessedDatabases,
|
||||
grants,
|
||||
isAllDbsAccess,
|
||||
isAllGrants,
|
||||
user,
|
||||
}: Params): Promise<Return> {
|
||||
const { name: finalMariadbUserName } = grabSQLUserName({
|
||||
name: mariadbUser.username,
|
||||
user,
|
||||
});
|
||||
|
||||
const finalPassword = mariadbUser.password?.replace(/ /g, "");
|
||||
if (!finalPassword) return { msg: `Couldn't get password` };
|
||||
|
||||
const encryptedFinalPassword = encrypt({ data: finalPassword });
|
||||
const finalHost = mariadbUser.host?.replace(/ /g, "");
|
||||
|
||||
const newMariadbUser: DSQL_DATASQUIREL_MARIADB_USERS = {
|
||||
password: encryptedFinalPassword || undefined,
|
||||
username: finalMariadbUserName,
|
||||
all_databases: isAllDbsAccess ? 1 : 0,
|
||||
all_grants: isAllGrants ? 1 : 0,
|
||||
host: finalHost,
|
||||
user_id: user.id,
|
||||
};
|
||||
|
||||
let { single: existingRecord } =
|
||||
await dbGrabUserResource<DSQL_DATASQUIREL_MARIADB_USERS>({
|
||||
tableName: "mariadb_users",
|
||||
userId: user.id,
|
||||
query: {
|
||||
query: {
|
||||
id: {
|
||||
value: String(mariadbUser.id || 0),
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const record = await addDbEntry<
|
||||
DSQL_DATASQUIREL_MARIADB_USERS,
|
||||
(typeof DsqlTables)[number]
|
||||
>({
|
||||
tableName: "mariadb_users",
|
||||
data: newMariadbUser,
|
||||
update: true,
|
||||
duplicateColumnName: "id",
|
||||
duplicateColumnValue: (existingRecord?.id || 0).toString(),
|
||||
});
|
||||
|
||||
let { single: updatedRecord } =
|
||||
await dbGrabUserResource<DSQL_DATASQUIREL_MARIADB_USERS>({
|
||||
tableName: "mariadb_users",
|
||||
userId: user.id,
|
||||
query: {
|
||||
query: {
|
||||
id: {
|
||||
value: String(
|
||||
existingRecord?.id || record?.payload?.insertId || 0
|
||||
),
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return { existingRecord, updatedRecord };
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import { UserType } from "../../../types";
|
||||
import { DSQL_DATASQUIREL_MARIADB_USERS } from "../../../types/dsql";
|
||||
import grabDbNames from "../../../utils/grab-db-names";
|
||||
import normalizeText from "../../../utils/normalize-text";
|
||||
import dbHandler from "../../backend/dbHandler";
|
||||
import decrypt from "../../dsql/decrypt";
|
||||
import { createNewSQLUser } from "./handle-mariadb-user-creation";
|
||||
|
||||
type Params = {
|
||||
user: UserType;
|
||||
updatedRecord: DSQL_DATASQUIREL_MARIADB_USERS;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
msg?: string;
|
||||
success?: boolean;
|
||||
};
|
||||
|
||||
export default async function revokeAllExistingGrants({
|
||||
user,
|
||||
updatedRecord,
|
||||
}: Params): Promise<Return> {
|
||||
const { userDbPrefix } = grabDbNames({ user });
|
||||
const parsedPassword = decrypt({
|
||||
encryptedString: updatedRecord?.password || "",
|
||||
});
|
||||
|
||||
const revokeAllPrivileges = await dbHandler({
|
||||
query: normalizeText(`
|
||||
REVOKE ALL PRIVILEGES ON *.* FROM '${updatedRecord.username}'@'${updatedRecord.host}'
|
||||
`),
|
||||
});
|
||||
|
||||
if (!revokeAllPrivileges) {
|
||||
await createNewSQLUser({
|
||||
host: updatedRecord.host,
|
||||
password: parsedPassword,
|
||||
username: updatedRecord.username,
|
||||
});
|
||||
}
|
||||
|
||||
const revokeGrantOption = await dbHandler({
|
||||
query: normalizeText(`
|
||||
REVOKE GRANT OPTION ON *.* FROM '${updatedRecord.username}'@'${updatedRecord.host}'
|
||||
`),
|
||||
});
|
||||
|
||||
const userGrants = (await dbHandler({
|
||||
query: `SHOW GRANTS FOR '${updatedRecord.username}'@'${updatedRecord.host}'`,
|
||||
})) as any[];
|
||||
|
||||
for (let i = 0; i < userGrants.length; i++) {
|
||||
const grantObject = userGrants[i];
|
||||
const grant = grantObject?.[Object.keys(grantObject)[0]];
|
||||
|
||||
if (!grant?.match(/GRANT USAGE .* IDENTIFIED BY PASSWORD/)) {
|
||||
const revokeGrantText = grant
|
||||
.replace(/GRANT/, "REVOKE")
|
||||
.replace(/ TO /, " FROM ");
|
||||
|
||||
const revokePrivilege = await dbHandler({ query: revokeGrantText });
|
||||
}
|
||||
}
|
||||
|
||||
const flushPrivileges = await dbHandler({
|
||||
query: `FLUSH PRIVILEGES`,
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
Reference in New Issue
Block a user