Skip to content

Commit b2c6671

Browse files
Feature | Inject AuditContext into custom formatters (#465)
* feat: Inject AuditContext into custom formatters * feat: Added AbstractAuditLogFormatter class to void setContext to be rewritten everywhere * fix: Changed concrete formatters to extend abstract class AbstractAuditLogFormatter instead of implement interface IAuditLogFormatter
1 parent f74501e commit b2c6671

10 files changed

+54
-17
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Audit;
4+
5+
/**
6+
* Copyright 2025 OpenStack Foundation
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
**/
17+
18+
abstract class AbstractAuditLogFormatter implements IAuditLogFormatter
19+
{
20+
protected AuditContext $ctx;
21+
22+
final public function setContext(AuditContext $ctx): void
23+
{
24+
$this->ctx = $ctx;
25+
}
26+
27+
abstract public function format($subject, array $change_set): ?string;
28+
}

app/Audit/AuditLogFormatterFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function getStrategyClass(object $subject, string $event_type): ?IAuditLo
3636
return !is_null($cls) ? new $cls($event_type):null;
3737
}
3838

39-
public function make($subject, $eventType): ?IAuditLogFormatter
39+
public function make(AuditContext $ctx, $subject, $eventType): ?IAuditLogFormatter
4040
{
4141
$formatter = null;
4242
switch ($eventType) {
@@ -72,6 +72,7 @@ public function make($subject, $eventType): ?IAuditLogFormatter
7272
}
7373
break;
7474
}
75+
$formatter->setContext($ctx);
7576
return $formatter;
7677
}
7778
}

app/Audit/AuditLogOtlpStrategy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function audit($subject, array $change_set, string $event_type, AuditCon
5252
return;
5353
}
5454
Log::debug("AuditLogOtlpStrategy::audit current user", ["user_id" => $ctx->userId, "user_email" => $ctx->userEmail]);
55-
$formatter = $this->formatterFactory->make($subject, $event_type);
55+
$formatter = $this->formatterFactory->make($ctx, $subject, $event_type);
5656
if(is_null($formatter)) {
5757
Log::warning("AuditLogOtlpStrategy::audit formatter not found");
5858
return;

app/Audit/ConcreteFormatters/EntityCollectionUpdateAuditLogFormatter.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace App\Audit\ConcreteFormatters;
44

55
use App\Audit\ConcreteFormatters\ChildEntityFormatters\IChildEntityAuditLogFormatter;
6-
use App\Audit\IAuditLogFormatter;
6+
use App\Audit\AbstractAuditLogFormatter;
77
use Illuminate\Support\Facades\Log;
88
use ReflectionException;
99

@@ -25,7 +25,7 @@
2525
* Class EntityCollectionUpdateAuditLogFormatter
2626
* @package App\Audit\ConcreteFormatters
2727
*/
28-
class EntityCollectionUpdateAuditLogFormatter implements IAuditLogFormatter
28+
class EntityCollectionUpdateAuditLogFormatter extends AbstractAuditLogFormatter
2929
{
3030
/**
3131
* @var IChildEntityAuditLogFormatter
@@ -66,4 +66,5 @@ public function format($subject, $change_set): ?string {
6666
return null;
6767
}
6868
}
69-
}
69+
}
70+

app/Audit/ConcreteFormatters/EntityCreationAuditLogFormatter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Audit\ConcreteFormatters;
44

5-
use App\Audit\IAuditLogFormatter;
5+
use App\Audit\AbstractAuditLogFormatter;
66
use ReflectionClass;
77

88
/**
@@ -23,7 +23,7 @@
2323
* Class EntityCreationAuditLogFormatter
2424
* @package App\Audit\ConcreteFormatters
2525
*/
26-
class EntityCreationAuditLogFormatter implements IAuditLogFormatter
26+
class EntityCreationAuditLogFormatter extends AbstractAuditLogFormatter
2727
{
2828
protected function getCreationIgnoredEntities(): array {
2929
return [
@@ -41,4 +41,4 @@ public function format($subject, $change_set): ?string {
4141
if (in_array($class_name, $ignored_entities)) return null;
4242
return "{$class_name} created";
4343
}
44-
}
44+
}

app/Audit/ConcreteFormatters/EntityDeletionAuditLogFormatter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
* limitations under the License.
1616
**/
1717

18+
use App\Audit\AbstractAuditLogFormatter;
1819
use App\Audit\ConcreteFormatters\ChildEntityFormatters\IChildEntityAuditLogFormatter;
19-
use App\Audit\IAuditLogFormatter;
2020
use models\summit\SummitAttendeeBadgePrint;
2121
use ReflectionClass;
2222

2323
/**
2424
* Class EntityDeletionAuditLogFormatter
2525
* @package App\Audit\ConcreteFormatters
2626
*/
27-
class EntityDeletionAuditLogFormatter implements IAuditLogFormatter
27+
class EntityDeletionAuditLogFormatter extends AbstractAuditLogFormatter
2828
{
2929
/**
3030
* @var IChildEntityAuditLogFormatter
@@ -58,4 +58,4 @@ public function format($subject, $change_set): ?string {
5858

5959
return "{$class_name} deleted";
6060
}
61-
}
61+
}

app/Audit/ConcreteFormatters/EntityUpdateAuditLogFormatter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
* limitations under the License.
1313
**/
1414

15+
use App\Audit\AbstractAuditLogFormatter;
1516
use App\Audit\ConcreteFormatters\ChildEntityFormatters\IChildEntityAuditLogFormatter;
16-
use App\Audit\IAuditLogFormatter;
1717
use App\Models\Foundation\Summit\SelectionPlan;
1818
use App\Models\Utils\BaseEntity;
1919
use DateTime;
@@ -30,7 +30,7 @@
3030
* Class EntityUpdateAuditLogFormatter
3131
* @package App\Audit\ConcreteFormatters
3232
*/
33-
class EntityUpdateAuditLogFormatter implements IAuditLogFormatter
33+
class EntityUpdateAuditLogFormatter extends AbstractAuditLogFormatter
3434
{
3535
/**
3636
* @var IChildEntityAuditLogFormatter
@@ -159,4 +159,4 @@ public function format($subject, $change_set): ?string
159159

160160
return join("|", $res);
161161
}
162-
}
162+
}

app/Audit/ConcreteFormatters/SummitMemberScheduleAuditLogFormatter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
**/
14-
use App\Audit\IAuditLogFormatter;
14+
15+
use App\Audit\AbstractAuditLogFormatter;
1516
use App\Audit\Interfaces\IAuditStrategy;
1617
use models\main\SummitMemberSchedule;
1718

18-
class SummitMemberScheduleAuditLogFormatter implements IAuditLogFormatter
19+
class SummitMemberScheduleAuditLogFormatter extends AbstractAuditLogFormatter
1920
{
2021

2122
private string $event_type;

app/Audit/IAuditLogFormatter.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
*/
2121
interface IAuditLogFormatter
2222
{
23+
/**
24+
* @param $ctx
25+
* @return void
26+
*/
27+
public function setContext(AuditContext $ctx): void;
28+
2329
/**
2430
* @param $subject
2531
* @param array $change_set

app/Audit/IAuditLogFormatterFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
interface IAuditLogFormatterFactory
44
{
5-
public function make($subject, $eventType): ?IAuditLogFormatter;
5+
public function make(AuditContext $ctx, $subject, $eventType): ?IAuditLogFormatter;
66
}

0 commit comments

Comments
 (0)