Browse Source

refactor: include missing audit log data

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5239/head
Pranav C 2 years ago
parent
commit
2dec831b7e
  1. 6
      packages/nocodb/src/lib/controllers/columnController.ts
  2. 1
      packages/nocodb/src/lib/controllers/tableController.ts
  3. 16
      packages/nocodb/src/lib/services/columnService.ts
  4. 65
      packages/nocodb/src/lib/services/tableService.ts

6
packages/nocodb/src/lib/controllers/columnController.ts

@ -16,6 +16,7 @@ export async function columnAdd(
await columnService.columnAdd({
tableId: req.params.tableId,
column: req.body,
req,
})
);
}
@ -31,12 +32,15 @@ export async function columnUpdate(req: Request, res: Response<TableType>) {
await columnService.columnUpdate({
columnId: req.params.columnId,
column: req.body,
req,
})
);
}
export async function columnDelete(req: Request, res: Response<TableType>) {
res.json(await columnService.columnDelete({ columnId: req.params.columnId }));
res.json(
await columnService.columnDelete({ columnId: req.params.columnId, req })
);
}
const router = Router({ mergeParams: true });

1
packages/nocodb/src/lib/controllers/tableController.ts

@ -42,6 +42,7 @@ export async function tableDelete(req: Request, res: Response) {
const result = await tableService.tableDelete({
tableId: req.params.tableId,
user: (req as any).session?.passport?.user,
req,
});
res.json(result);

16
packages/nocodb/src/lib/services/columnService.ts

@ -52,6 +52,7 @@ export enum Altered {
}
export async function columnUpdate(param: {
req?: any;
columnId: string;
column: ColumnReqType & { colOptions?: any };
cookie?: any;
@ -812,9 +813,9 @@ export async function columnUpdate(param: {
project_id: base.project_id,
op_type: AuditOperationTypes.TABLE_COLUMN,
op_sub_type: AuditOperationSubTypes.UPDATED,
// user: (req as any)?.user?.email,
user: param.req?.user?.email,
description: `updated column ${column.column_name} with alias ${column.title} from table ${table.table_name}`,
// ip: (req as any).clientIp,
ip: param.req?.clientIp,
}).then(() => {});
await table.getColumns();
@ -833,6 +834,7 @@ export async function columnSetAsPrimary(param: { columnId: string }) {
}
export async function columnAdd(param: {
req?: any;
tableId: string;
column: ColumnReqType;
}) {
@ -1107,9 +1109,9 @@ export async function columnAdd(param: {
project_id: base.project_id,
op_type: AuditOperationTypes.TABLE_COLUMN,
op_sub_type: AuditOperationSubTypes.CREATED,
// user: (req as any)?.user?.email,
user: param?.req?.user?.email,
description: `created column ${colBody.column_name} with alias ${colBody.title} from table ${table.table_name}`,
// ip: (req as any).clientIp,
ip: param?.req.clientIp,
}).then(() => {});
T.emit('evt', { evt_type: 'column:created' });
@ -1117,7 +1119,7 @@ export async function columnAdd(param: {
return table;
}
export async function columnDelete(param: { columnId: string }) {
export async function columnDelete(param: { req?: any; columnId: string }) {
const column = await Column.get({ colId: param.columnId });
const table = await Model.getWithInfo({
id: column.fk_model_id,
@ -1319,9 +1321,9 @@ export async function columnDelete(param: { columnId: string }) {
project_id: base.project_id,
op_type: AuditOperationTypes.TABLE_COLUMN,
op_sub_type: AuditOperationSubTypes.DELETED,
// user: (req as any)?.user?.email,
user: param?.req?.user?.email,
description: `deleted column ${column.column_name} with alias ${column.title} from table ${table.table_name}`,
// ip: (req as any).clientIp,
ip: param?.req.clientIp,
}).then(() => {});
await table.getColumns();

65
packages/nocodb/src/lib/services/tableService.ts

@ -140,7 +140,11 @@ export function reorderTable(param: { tableId: string; order: any }) {
return Model.updateOrder(param.tableId, param.order);
}
export async function tableDelete(param: { tableId: string; user: User }) {
export async function tableDelete(param: {
tableId: string;
user: User;
req?: any;
}) {
const table = await Model.getByIdOrName({ id: param.tableId });
await table.getColumns();
@ -189,7 +193,7 @@ export async function tableDelete(param: { tableId: string; user: User }) {
op_sub_type: AuditOperationSubTypes.DELETED,
user: param.user?.email,
description: `Deleted ${table.type} ${table.table_name} with alias ${table.title} `,
// ip: (req as any).clientIp,
ip: param.req?.clientIp,
}).then(() => {});
T.emit('evt', { evt_type: 'table:deleted' });
@ -408,24 +412,25 @@ export async function getAccessibleTables(param: {
: (tableList.filter((t) => !t.mm) as Model[]);
}
export async function tableCreate(args: {
export async function tableCreate(param: {
projectId: string;
baseId?: string;
table: TableReqType;
user: User;
req?: any;
}) {
validatePayload('swagger.json#/components/schemas/TableReq', args.table);
validatePayload('swagger.json#/components/schemas/TableReq', param.table);
const project = await Project.getWithInfo(args.projectId);
const project = await Project.getWithInfo(param.projectId);
let base = project.bases[0];
if (args.baseId) {
base = project.bases.find((b) => b.id === args.baseId);
if (param.baseId) {
base = project.bases.find((b) => b.id === param.baseId);
}
if (
!args.table.table_name ||
(project.prefix && project.prefix === args.table.table_name)
!param.table.table_name ||
(project.prefix && project.prefix === param.table.table_name)
) {
NcError.badRequest(
'Missing table name `table_name` property in request body'
@ -433,15 +438,15 @@ export async function tableCreate(args: {
}
if (base.is_meta && project.prefix) {
if (!args.table.table_name.startsWith(project.prefix)) {
args.table.table_name = `${project.prefix}_${args.table.table_name}`;
if (!param.table.table_name.startsWith(project.prefix)) {
param.table.table_name = `${project.prefix}_${param.table.table_name}`;
}
}
args.table.table_name = DOMPurify.sanitize(args.table.table_name);
param.table.table_name = DOMPurify.sanitize(param.table.table_name);
// validate table name
if (/^\s+|\s+$/.test(args.table.table_name)) {
if (/^\s+|\s+$/.test(param.table.table_name)) {
NcError.badRequest(
'Leading or trailing whitespace not allowed in table names'
);
@ -449,7 +454,7 @@ export async function tableCreate(args: {
if (
!(await Model.checkTitleAvailable({
table_name: args.table.table_name,
table_name: param.table.table_name,
project_id: project.id,
base_id: base.id,
}))
@ -457,9 +462,9 @@ export async function tableCreate(args: {
NcError.badRequest('Duplicate table name');
}
if (!args.table.title) {
args.table.title = getTableNameAlias(
args.table.table_name,
if (!param.table.title) {
param.table.title = getTableNameAlias(
param.table.table_name,
project.prefix,
base
);
@ -467,7 +472,7 @@ export async function tableCreate(args: {
if (
!(await Model.checkAliasAvailable({
title: args.table.title,
title: param.table.title,
project_id: project.id,
base_id: base.id,
}))
@ -489,13 +494,13 @@ export async function tableCreate(args: {
tableNameLengthLimit = 128;
}
if (args.table.table_name.length > tableNameLengthLimit) {
if (param.table.table_name.length > tableNameLengthLimit) {
NcError.badRequest(`Table name exceeds ${tableNameLengthLimit} characters`);
}
const mxColumnLength = Column.getMaxColumnNameLength(sqlClientType);
for (const column of args.table.columns) {
for (const column of param.table.columns) {
if (column.column_name.length > mxColumnLength) {
NcError.badRequest(
`Column name ${column.column_name} exceeds ${mxColumnLength} characters`
@ -503,13 +508,13 @@ export async function tableCreate(args: {
}
}
args.table.columns = args.table.columns?.map((c) => ({
param.table.columns = param.table.columns?.map((c) => ({
...getColumnPropsFromUIDT(c as any, base),
cn: c.column_name,
}));
await sqlMgr.sqlOpPlus(base, 'tableCreate', {
...args.table,
tn: args.table.table_name,
...param.table,
tn: param.table.table_name,
});
const columns: Array<
@ -517,7 +522,7 @@ export async function tableCreate(args: {
cn: string;
system?: boolean;
}
> = (await sqlClient.columnList({ tn: args.table.table_name }))?.data?.list;
> = (await sqlClient.columnList({ tn: param.table.table_name }))?.data?.list;
const tables = await Model.list({
project_id: project.id,
@ -529,20 +534,20 @@ export async function tableCreate(args: {
base_id: base.id,
op_type: AuditOperationTypes.TABLE,
op_sub_type: AuditOperationSubTypes.CREATED,
user: args.user?.email,
description: `created table ${args.table.table_name} with alias ${args.table.title} `,
// ip: (req as any).clientIp,
user: param.user?.email,
description: `created table ${param.table.table_name} with alias ${param.table.title} `,
ip: param.req?.clientIp,
}).then(() => {});
mapDefaultDisplayValue(args.table.columns);
mapDefaultDisplayValue(param.table.columns);
T.emit('evt', { evt_type: 'table:created' });
// todo: type correction
const result = await Model.insert(project.id, base.id, {
...args.table,
...param.table,
columns: columns.map((c, i) => {
const colMetaFromReq = args.table?.columns?.find(
const colMetaFromReq = param.table?.columns?.find(
(c1) => c.cn === c1.column_name
);
return {

Loading…
Cancel
Save