Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions app/Audit/AuditContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php namespace App\Audit;
/**
* Copyright 2025 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
class AuditContext
{
public function __construct(
public ?int $userId = null,
public ?string $userEmail = null,
public ?string $userFirstName = null,
public ?string $userLastName = null,
public ?string $uiApp = null,
public ?string $uiFlow = null,
public ?string $route = null,
public ?string $httpMethod = null,
public ?string $clientIp = null,
public ?string $userAgent = null,
) {}
}
52 changes: 40 additions & 12 deletions app/Audit/AuditEventListener.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?php

namespace App\Audit;

<?php namespace App\Audit;
/**
* Copyright 2022 OpenStack Foundation
* Copyright 2025 OpenStack Foundation
Copy link

Copilot AI Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The copyright year was updated from 2022 to 2025, but the original 2022 copyright should be preserved. Copyright notices typically use a range format like "Copyright 2022-2025" to reflect the original creation date and subsequent modifications.

Suggested change
* Copyright 2025 OpenStack Foundation
* Copyright 2022-2025 OpenStack Foundation

Copilot uses AI. Check for mistakes.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand All @@ -15,7 +12,7 @@
* limitations under the License.
**/

use App\Audit\AuditLogOtlpStrategy;
use App\Audit\Interfaces\IAuditStrategy;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;
Expand All @@ -29,30 +26,34 @@ class AuditEventListener

public function onFlush(OnFlushEventArgs $eventArgs): void
{
if (app()->environment('testing')){
return;
}
$em = $eventArgs->getObjectManager();
$uow = $em->getUnitOfWork();
// Strategy selection based on environment configuration
$strategy = $this->getAuditStrategy($em);

if (!$strategy) {
return; // No audit strategy enabled
}

$ctx = $this->buildAuditContext();

try {
foreach ($uow->getScheduledEntityInsertions() as $entity) {
$strategy->audit($entity, [], AuditLogOtlpStrategy::EVENT_ENTITY_CREATION);
$strategy->audit($entity, [], IAuditStrategy::EVENT_ENTITY_CREATION, $ctx);
}

foreach ($uow->getScheduledEntityUpdates() as $entity) {
$strategy->audit($entity, $uow->getEntityChangeSet($entity), AuditLogOtlpStrategy::EVENT_ENTITY_UPDATE);
$strategy->audit($entity, $uow->getEntityChangeSet($entity), IAuditStrategy::EVENT_ENTITY_UPDATE, $ctx);
}

foreach ($uow->getScheduledEntityDeletions() as $entity) {
$strategy->audit($entity, [], AuditLogOtlpStrategy::EVENT_ENTITY_DELETION);
$strategy->audit($entity, [], IAuditStrategy::EVENT_ENTITY_DELETION, $ctx);
}

foreach ($uow->getScheduledCollectionUpdates() as $col) {
$strategy->audit($col, [], AuditLogOtlpStrategy::EVENT_COLLECTION_UPDATE);
$strategy->audit($col, [], IAuditStrategy::EVENT_COLLECTION_UPDATE, $ctx);
}
} catch (\Exception $e) {
Log::error('Audit event listener failed', [
Expand All @@ -78,9 +79,36 @@ private function getAuditStrategy($em)
]);
}
}

// Use database strategy (either as default or fallback)
return new AuditLogStrategy($em);
}

private function buildAuditContext(): AuditContext
{
$resourceCtx = app(\models\oauth2\IResourceServerContext::class);
$userExternalId = $resourceCtx->getCurrentUserId();
$member = null;
if ($userExternalId) {
$memberRepo = app(\models\main\IMemberRepository::class);
$member = $memberRepo->findOneBy(["user_external_id" => $userExternalId]);
}

//$ui = app()->bound('ui.context') ? app('ui.context') : [];

$req = request();

return new AuditContext(
userId: $member?->getId(),
userEmail: $member?->getEmail(),
userFirstName: $member?->getFirstName(),
userLastName: $member?->getLastName(),
uiApp: $ui['app'] ?? null,
uiFlow: $ui['flow'] ?? null,
Comment on lines +106 to +107
Copy link

Copilot AI Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable $ui is used on lines 102-103 but is never defined. The commented-out line 93 suggests this was meant to retrieve UI context, but it's commented out, leaving $ui undefined. This will cause a PHP error when these values are accessed.

Either uncomment and fix line 93 to properly populate $ui, or remove the references to $ui['app'] and $ui['flow'] on lines 102-103.

Copilot uses AI. Check for mistakes.
route: $req?->path(),
httpMethod: $req?->method(),
clientIp: $req?->ip(),
userAgent: $req?->userAgent(),
);
}
}
77 changes: 77 additions & 0 deletions app/Audit/AuditLogFormatterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php namespace App\Audit;
/**
* Copyright 2025 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use App\Audit\ConcreteFormatters\ChildEntityFormatters\ChildEntityFormatterFactory;
use App\Audit\ConcreteFormatters\EntityCollectionUpdateAuditLogFormatter;
use App\Audit\ConcreteFormatters\EntityCreationAuditLogFormatter;
use App\Audit\ConcreteFormatters\EntityDeletionAuditLogFormatter;
use App\Audit\ConcreteFormatters\EntityUpdateAuditLogFormatter;
use App\Audit\Interfaces\IAuditStrategy;

class AuditLogFormatterFactory implements IAuditLogFormatterFactory
{

private array $config;

public function __construct()
{
// cache the config so we don't hit config() repeatedly
$this->config = config('audit_log', []);
}

public function getStrategyClass(object $subject, string $event_type): ?IAuditLogFormatter
{
$class = get_class($subject);
$cls = $this->config['entities'][$class]['strategy'] ?? null;
return !is_null($cls) ? new $cls($event_type):null;
}

public function make($subject, $eventType): ?IAuditLogFormatter
{
$formatter = null;
switch ($eventType) {
case IAuditStrategy::EVENT_COLLECTION_UPDATE:
$child_entity = null;
if (count($subject) > 0) {
$child_entity = $subject[0];
}
if (is_null($child_entity) && count($subject->getSnapshot()) > 0) {
Comment on lines +45 to +48
Copy link

Copilot AI Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential issue with count() on line 30: if $subject is not countable (e.g., it's null or some other non-collection type), this will trigger a PHP warning. Consider adding a check like is_countable($subject) && count($subject) > 0 or verifying that $subject is a valid collection before attempting to count it.

Suggested change
if (count($subject) > 0) {
$child_entity = $subject[0];
}
if (is_null($child_entity) && count($subject->getSnapshot()) > 0) {
if (is_countable($subject) && count($subject) > 0) {
$child_entity = $subject[0];
}
if (is_null($child_entity) && is_countable($subject->getSnapshot()) && count($subject->getSnapshot()) > 0) {

Copilot uses AI. Check for mistakes.
$child_entity = $subject->getSnapshot()[0];
}
$child_entity_formatter = $child_entity != null ? ChildEntityFormatterFactory::build($child_entity) : null;
$formatter = new EntityCollectionUpdateAuditLogFormatter($child_entity_formatter);
break;
case IAuditStrategy::EVENT_ENTITY_CREATION:
$formatter = $this->getStrategyClass($subject, $eventType);
if(is_null($formatter)) {
$formatter = new EntityCreationAuditLogFormatter();
}
break;
case IAuditStrategy::EVENT_ENTITY_DELETION:
$formatter = $this->getStrategyClass($subject, $eventType);
if(is_null($formatter)) {
$child_entity_formatter = ChildEntityFormatterFactory::build($subject);
$formatter = new EntityDeletionAuditLogFormatter($child_entity_formatter);
}
break;
case IAuditStrategy::EVENT_ENTITY_UPDATE:
$formatter = $this->getStrategyClass($subject, $eventType);
if(is_null($formatter)) {
$child_entity_formatter = ChildEntityFormatterFactory::build($subject);
$formatter = new EntityUpdateAuditLogFormatter($child_entity_formatter);
}
break;
}
return $formatter;
}
}
Loading