1- <?php
2-
3- namespace App \Audit ;
4-
1+ <?php namespace App \Audit ;
52/**
6- * Copyright 2022 OpenStack Foundation
3+ * Copyright 2025 OpenStack Foundation
74 * Licensed under the Apache License, Version 2.0 (the "License");
85 * you may not use this file except in compliance with the License.
96 * You may obtain a copy of the License at
1512 * limitations under the License.
1613 **/
1714
18- use App \Audit \AuditLogOtlpStrategy ;
15+ use App \Audit \Interfaces \ IAuditStrategy ;
1916use Doctrine \ORM \Event \OnFlushEventArgs ;
2017use Illuminate \Support \Facades \App ;
2118use Illuminate \Support \Facades \Log ;
@@ -33,26 +30,26 @@ public function onFlush(OnFlushEventArgs $eventArgs): void
3330 $ uow = $ em ->getUnitOfWork ();
3431 // Strategy selection based on environment configuration
3532 $ strategy = $ this ->getAuditStrategy ($ em );
36-
33+ $ ctx = $ this -> buildAuditContext ();
3734 if (!$ strategy ) {
3835 return ; // No audit strategy enabled
3936 }
4037
4138 try {
4239 foreach ($ uow ->getScheduledEntityInsertions () as $ entity ) {
43- $ strategy ->audit ($ entity , [], AuditLogOtlpStrategy ::EVENT_ENTITY_CREATION );
40+ $ strategy ->audit ($ entity , [], IAuditStrategy ::EVENT_ENTITY_CREATION , $ ctx );
4441 }
4542
4643 foreach ($ uow ->getScheduledEntityUpdates () as $ entity ) {
47- $ strategy ->audit ($ entity , $ uow ->getEntityChangeSet ($ entity ), AuditLogOtlpStrategy ::EVENT_ENTITY_UPDATE );
44+ $ strategy ->audit ($ entity , $ uow ->getEntityChangeSet ($ entity ), IAuditStrategy ::EVENT_ENTITY_UPDATE , $ ctx );
4845 }
4946
5047 foreach ($ uow ->getScheduledEntityDeletions () as $ entity ) {
51- $ strategy ->audit ($ entity , [], AuditLogOtlpStrategy ::EVENT_ENTITY_DELETION );
48+ $ strategy ->audit ($ entity , [], IAuditStrategy ::EVENT_ENTITY_DELETION , $ ctx );
5249 }
5350
5451 foreach ($ uow ->getScheduledCollectionUpdates () as $ col ) {
55- $ strategy ->audit ($ col , [], AuditLogOtlpStrategy ::EVENT_COLLECTION_UPDATE );
52+ $ strategy ->audit ($ col , [], IAuditStrategy ::EVENT_COLLECTION_UPDATE , $ ctx );
5653 }
5754 } catch (\Exception $ e ) {
5855 Log::error ('Audit event listener failed ' , [
@@ -78,9 +75,36 @@ private function getAuditStrategy($em)
7875 ]);
7976 }
8077 }
81-
78+
8279 // Use database strategy (either as default or fallback)
8380 return new AuditLogStrategy ($ em );
81+ }
82+
83+ private function buildAuditContext (): AuditContext
84+ {
85+ $ resourceCtx = app (\models \oauth2 \IResourceServerContext::class);
86+ $ userExternalId = $ resourceCtx ->getCurrentUserId ();
87+ $ member = null ;
88+ if ($ userExternalId ) {
89+ $ memberRepo = app (\models \main \IMemberRepository::class);
90+ $ member = $ memberRepo ->findOneBy (["user_external_id " => $ userExternalId ]);
91+ }
92+
93+ //$ui = app()->bound('ui.context') ? app('ui.context') : [];
94+
95+ $ req = request ();
8496
97+ return new AuditContext (
98+ userId: $ member ?->getId(),
99+ userEmail: $ member ?->getEmail(),
100+ userFirstName: $ member ?->getFirstName(),
101+ userLastName: $ member ?->getLastName(),
102+ uiApp: $ ui ['app ' ] ?? null ,
103+ uiFlow: $ ui ['flow ' ] ?? null ,
104+ route: $ req ?->path(),
105+ httpMethod: $ req ?->method(),
106+ clientIp: $ req ?->ip(),
107+ userAgent: $ req ?->userAgent(),
108+ );
85109 }
86110}
0 commit comments