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 ;
@@ -29,30 +26,34 @@ class AuditEventListener
2926
3027 public function onFlush (OnFlushEventArgs $ eventArgs ): void
3128 {
29+ if (app ()->environment ('testing ' )){
30+ return ;
31+ }
3232 $ em = $ eventArgs ->getObjectManager ();
3333 $ uow = $ em ->getUnitOfWork ();
3434 // Strategy selection based on environment configuration
3535 $ strategy = $ this ->getAuditStrategy ($ em );
36-
3736 if (!$ strategy ) {
3837 return ; // No audit strategy enabled
3938 }
4039
40+ $ ctx = $ this ->buildAuditContext ();
41+
4142 try {
4243 foreach ($ uow ->getScheduledEntityInsertions () as $ entity ) {
43- $ strategy ->audit ($ entity , [], AuditLogOtlpStrategy ::EVENT_ENTITY_CREATION );
44+ $ strategy ->audit ($ entity , [], IAuditStrategy ::EVENT_ENTITY_CREATION , $ ctx );
4445 }
4546
4647 foreach ($ uow ->getScheduledEntityUpdates () as $ entity ) {
47- $ strategy ->audit ($ entity , $ uow ->getEntityChangeSet ($ entity ), AuditLogOtlpStrategy ::EVENT_ENTITY_UPDATE );
48+ $ strategy ->audit ($ entity , $ uow ->getEntityChangeSet ($ entity ), IAuditStrategy ::EVENT_ENTITY_UPDATE , $ ctx );
4849 }
4950
5051 foreach ($ uow ->getScheduledEntityDeletions () as $ entity ) {
51- $ strategy ->audit ($ entity , [], AuditLogOtlpStrategy ::EVENT_ENTITY_DELETION );
52+ $ strategy ->audit ($ entity , [], IAuditStrategy ::EVENT_ENTITY_DELETION , $ ctx );
5253 }
5354
5455 foreach ($ uow ->getScheduledCollectionUpdates () as $ col ) {
55- $ strategy ->audit ($ col , [], AuditLogOtlpStrategy ::EVENT_COLLECTION_UPDATE );
56+ $ strategy ->audit ($ col , [], IAuditStrategy ::EVENT_COLLECTION_UPDATE , $ ctx );
5657 }
5758 } catch (\Exception $ e ) {
5859 Log::error ('Audit event listener failed ' , [
@@ -78,9 +79,36 @@ private function getAuditStrategy($em)
7879 ]);
7980 }
8081 }
81-
82+
8283 // Use database strategy (either as default or fallback)
8384 return new AuditLogStrategy ($ em );
85+ }
86+
87+ private function buildAuditContext (): AuditContext
88+ {
89+ $ resourceCtx = app (\models \oauth2 \IResourceServerContext::class);
90+ $ userExternalId = $ resourceCtx ->getCurrentUserId ();
91+ $ member = null ;
92+ if ($ userExternalId ) {
93+ $ memberRepo = app (\models \main \IMemberRepository::class);
94+ $ member = $ memberRepo ->findOneBy (["user_external_id " => $ userExternalId ]);
95+ }
96+
97+ //$ui = app()->bound('ui.context') ? app('ui.context') : [];
98+
99+ $ req = request ();
84100
101+ return new AuditContext (
102+ userId: $ member ?->getId(),
103+ userEmail: $ member ?->getEmail(),
104+ userFirstName: $ member ?->getFirstName(),
105+ userLastName: $ member ?->getLastName(),
106+ uiApp: $ ui ['app ' ] ?? null ,
107+ uiFlow: $ ui ['flow ' ] ?? null ,
108+ route: $ req ?->path(),
109+ httpMethod: $ req ?->method(),
110+ clientIp: $ req ?->ip(),
111+ userAgent: $ req ?->userAgent(),
112+ );
85113 }
86114}
0 commit comments