Skip to content

Commit db98805

Browse files
committed
feat: enhance audit logging with additional user data
- Added 'username' and 'reason' fields to the audit schema for improved tracking of authentication attempts. - Updated the AuditsService to handle unknown agent IDs more gracefully, defaulting to 'N/A' when necessary. - Enhanced the Vue component to display user-related information and improve document handling logic in the audit table.
1 parent d050bb4 commit db98805

3 files changed

Lines changed: 46 additions & 13 deletions

File tree

apps/api/src/core/audits/audits.controller.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export class AuditsController extends AbstractController {
4040
ip: 1,
4141
agent: 1,
4242
'data.result': 1,
43+
'data.username': 1,
44+
'data.reason': 1,
4345
'changes.path': 1,
4446
'changes.type': 1,
4547
metadata: 1,

apps/api/src/core/audits/audits.service.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ export class AuditsService extends AbstractServiceSchema<Audits> {
4949
reason: string
5050
agentId?: Types.ObjectId | string
5151
}): Promise<Audits> {
52-
const agentObjectId =
53-
params.agentId instanceof Types.ObjectId
52+
const hasKnownAgentId = params.agentId instanceof Types.ObjectId || (typeof params.agentId === 'string' && Types.ObjectId.isValid(params.agentId))
53+
const agentObjectId = hasKnownAgentId
54+
? params.agentId instanceof Types.ObjectId
5455
? params.agentId
55-
: typeof params.agentId === 'string' && Types.ObjectId.isValid(params.agentId)
56-
? new Types.ObjectId(params.agentId)
57-
: new Types.ObjectId()
56+
: new Types.ObjectId(params.agentId)
57+
: new Types.ObjectId('000000000000000000000000')
5858

5959
const createdAt = new Date()
6060

@@ -66,7 +66,7 @@ export class AuditsService extends AbstractServiceSchema<Audits> {
6666
agent: {
6767
$ref: 'agents',
6868
id: agentObjectId,
69-
name: params.username,
69+
name: hasKnownAgentId ? params.username : 'N/A',
7070
},
7171
data: {
7272
event: 'authentication_attempt',

apps/web/src/pages/audits/table.vue

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ q-page.container.q-pa-sm
7070
template(#body-cell-document='props')
7171
q-td(:props='props')
7272
q-chip.bg-positive.text-white.q-pa-sm(
73-
v-if='props.row?.coll === "Identities" && props.row?.documentId'
73+
v-if='props.row?.coll === "Identities" && canOpenDocument(props.row)'
7474
icon='mdi-account'
7575
clickable
7676
dense
@@ -80,13 +80,13 @@ q-page.container.q-pa-sm
8080
q-tooltip.text-body2(anchor='top middle' self='bottom middle')
8181
span Voir la fiche identité
8282
q-chip.bg-positive.text-white.q-pa-sm(
83-
v-else-if='["Agents", "auth"].includes(props.row?.coll) && props.row?.documentId'
83+
v-else-if='["Agents", "auth"].includes(props.row?.coll) && canOpenDocument(props.row)'
8484
icon='mdi-account'
8585
clickable
8686
dense
8787
@click='openDocument(props.row)'
8888
)
89-
span(v-text='props.row?.documentId')
89+
span(v-text='getDocumentLabel(props.row)')
9090
q-tooltip.text-body2(anchor='top middle' self='bottom middle')
9191
span Voir la fiche agent
9292
q-chip(
@@ -95,19 +95,35 @@ q-page.container.q-pa-sm
9595
dense
9696
color='grey-4'
9797
text-color='dark'
98-
:label='props.row?.documentId || "N/A"'
98+
:label='getDocumentLabel(props.row)'
9999
)
100100
template(#body-cell-author='props')
101101
q-td(:props='props')
102-
span(v-text='props.row?.agent?.name || "system"')
102+
span(v-text='getAuthorLabel(props.row)')
103103
template(#body-cell-ip='props')
104104
q-td(:props='props')
105105
q-chip(size='sm' dense color='blue-1' text-color='dark' :label='props.row?.ip || "N/A"')
106106
template(#body-cell-changes='props')
107107
q-td(:props='props')
108108
.row.items-center.q-gutter-xs
109109
q-chip(
110-
v-if='!props.row?.changes?.length'
110+
v-if='props.row?.op === "authentication"'
111+
size='sm'
112+
dense
113+
color='blue-grey-2'
114+
text-color='dark'
115+
:label='`IDENTIFIANT: ${props.row?.data?.username || "N/A"}`'
116+
)
117+
q-chip(
118+
v-if='props.row?.op === "authentication"'
119+
size='sm'
120+
dense
121+
color='blue-grey-2'
122+
text-color='dark'
123+
:label='`RAISON: ${props.row?.data?.reason || "N/A"}`'
124+
)
125+
q-chip(
126+
v-else-if='!props.row?.changes?.length'
111127
size='sm'
112128
dense
113129
color='grey-4'
@@ -289,7 +305,7 @@ export default defineNuxtComponent({
289305
},
290306
{
291307
name: 'changes',
292-
label: 'Changements',
308+
label: 'Informations',
293309
align: 'left',
294310
field: (row) => row?.changes || [],
295311
sortable: false,
@@ -380,6 +396,21 @@ export default defineNuxtComponent({
380396
break
381397
}
382398
},
399+
isUnknownAuthenticationAgent(row: any): boolean {
400+
return row?.coll === 'auth' && `${row?.documentId || ''}` === '000000000000000000000000'
401+
},
402+
canOpenDocument(row: any): boolean {
403+
return !!row?.documentId && !this.isUnknownAuthenticationAgent(row)
404+
},
405+
getDocumentLabel(row: any): string {
406+
if (this.isUnknownAuthenticationAgent(row)) return 'N/A'
407+
return row?.documentId || 'N/A'
408+
},
409+
getAuthorLabel(row: any): string {
410+
if (this.isUnknownAuthenticationAgent(row)) return 'N/A'
411+
if (row?.coll === 'auth' && row?.agent?.name === 'N/A') return 'N/A'
412+
return row?.agent?.name || 'system'
413+
},
383414
getOperationLabel(row: any): string {
384415
return getAuditOperationLabel(row?.op, row?.data?.result)
385416
},

0 commit comments

Comments
 (0)