Skip to content

Commit 3425eac

Browse files
Feature| New Formatters for activities (#477)
* feat: add news formatter for Event site and Event * feat: add user context and id for generic entities * fix: refactor for old formatters * feat: add new formatters * feat: add placeholder for process by jobs * fix: change event_type name
1 parent 1147975 commit 3425eac

29 files changed

Lines changed: 1485 additions & 85 deletions

app/Audit/AbstractAuditLogFormatter.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
abstract class AbstractAuditLogFormatter implements IAuditLogFormatter
2121
{
22-
protected AuditContext $ctx;
22+
protected ?AuditContext $ctx = null;
2323
protected string $event_type;
2424

2525
public function __construct(string $event_type)
@@ -34,6 +34,9 @@ final public function setContext(AuditContext $ctx): void
3434

3535
protected function getUserInfo(): string
3636
{
37+
if (app()->runningInConsole()) {
38+
return 'Worker Job';
39+
}
3740
if (!$this->ctx) {
3841
return 'Unknown (unknown)';
3942
}
@@ -126,5 +129,5 @@ protected function formatFieldChange(string $prop_name, $old_value, $new_value):
126129
return sprintf("Property \"%s\" has changed from \"%s\" to \"%s\"", $prop_name, $old_display, $new_display);
127130
}
128131

129-
abstract public function format($subject, array $change_set): ?string;
132+
abstract public function format(mixed $subject, array $change_set): ?string;
130133
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace App\Audit\ConcreteFormatters\ChildEntityFormatters;
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+
use models\summit\RSVPAnswer;
19+
20+
class RSVPAnswerAuditLogFormatter implements IChildEntityAuditLogFormatter
21+
{
22+
public function format($subject, string $child_entity_action_type, ?string $additional_info = ""): ?string
23+
{
24+
if (!$subject instanceof RSVPAnswer) {
25+
return null;
26+
}
27+
28+
try {
29+
$questionId = $subject->getQuestionId();
30+
$value = $subject->getValue();
31+
$question = $subject->getQuestion();
32+
$questionLabel = $question?->getLabel() ?? sprintf("Question #%d", $questionId);
33+
34+
switch ($child_entity_action_type) {
35+
case IChildEntityAuditLogFormatter::CHILD_ENTITY_CREATION:
36+
return sprintf(
37+
"RSVP Answer added for question '%s' with value '%s'",
38+
$questionLabel,
39+
$value
40+
);
41+
42+
case IChildEntityAuditLogFormatter::CHILD_ENTITY_UPDATE:
43+
if (!empty($additional_info)) {
44+
return sprintf(
45+
"RSVP Answer for question '%s' updated: %s",
46+
$questionLabel,
47+
$additional_info
48+
);
49+
}
50+
return sprintf(
51+
"RSVP Answer for question '%s' updated to '%s'",
52+
$questionLabel,
53+
$value
54+
);
55+
56+
case IChildEntityAuditLogFormatter::CHILD_ENTITY_DELETION:
57+
return sprintf(
58+
"RSVP Answer removed for question '%s' (had value '%s')",
59+
$questionLabel,
60+
$value
61+
);
62+
}
63+
} catch (\Exception $ex) {
64+
return null;
65+
}
66+
67+
return null;
68+
}
69+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace App\Audit\ConcreteFormatters;
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+
use App\Audit\AbstractAuditLogFormatter;
19+
use App\Audit\Interfaces\IAuditStrategy;
20+
use models\main\Company;
21+
use Illuminate\Support\Facades\Log;
22+
23+
class CompanyAuditLogFormatter extends AbstractAuditLogFormatter
24+
{
25+
public function format($subject, array $change_set): ?string
26+
{
27+
if (!$subject instanceof Company) {
28+
return null;
29+
}
30+
31+
try {
32+
$name = $subject->getName() ?? 'Unknown Company';
33+
$id = $subject->getId() ?? 'unknown';
34+
$city = $subject->getCity() ?? 'N/A';
35+
$country = $subject->getCountry() ?? 'N/A';
36+
$display_on_site = $subject->isDisplayOnSite() ? 'yes' : 'no';
37+
38+
switch ($this->event_type) {
39+
case IAuditStrategy::EVENT_ENTITY_CREATION:
40+
return sprintf(
41+
"Company '%s' (%d) created located in %s, %s, display on site: %s by user %s",
42+
$name,
43+
$id,
44+
$city,
45+
$country,
46+
$display_on_site,
47+
$this->getUserInfo()
48+
);
49+
50+
case IAuditStrategy::EVENT_ENTITY_UPDATE:
51+
$change_details = $this->buildChangeDetails($change_set);
52+
return sprintf(
53+
"Company '%s' (%d) updated: %s by user %s",
54+
$name,
55+
$id,
56+
$change_details,
57+
$this->getUserInfo()
58+
);
59+
60+
case IAuditStrategy::EVENT_ENTITY_DELETION:
61+
return sprintf(
62+
"Company '%s' (%d) located in %s, %s was deleted by user %s",
63+
$name,
64+
$id,
65+
$city,
66+
$country,
67+
$this->getUserInfo()
68+
);
69+
}
70+
} catch (\Exception $ex) {
71+
Log::warning("CompanyAuditLogFormatter error: " . $ex->getMessage());
72+
}
73+
74+
return null;
75+
}
76+
}

app/Audit/ConcreteFormatters/EntityCollectionUpdateAuditLogFormatter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Audit\ConcreteFormatters\ChildEntityFormatters\IChildEntityAuditLogFormatter;
66
use App\Audit\AbstractAuditLogFormatter;
7+
use App\Audit\Interfaces\IAuditStrategy;
78
use Illuminate\Support\Facades\Log;
89
use ReflectionException;
910

@@ -34,6 +35,7 @@ class EntityCollectionUpdateAuditLogFormatter extends AbstractAuditLogFormatter
3435

3536
public function __construct(?IChildEntityAuditLogFormatter $child_entity_formatter)
3637
{
38+
parent::__construct(IAuditStrategy::EVENT_COLLECTION_UPDATE);
3739
$this->child_entity_formatter = $child_entity_formatter;
3840
}
3941

app/Audit/ConcreteFormatters/EntityCreationAuditLogFormatter.php

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

55
use App\Audit\AbstractAuditLogFormatter;
6+
use App\Audit\Interfaces\IAuditStrategy;
67
use ReflectionClass;
78

89
/**
@@ -25,6 +26,11 @@
2526
*/
2627
class EntityCreationAuditLogFormatter extends AbstractAuditLogFormatter
2728
{
29+
public function __construct()
30+
{
31+
parent::__construct(IAuditStrategy::EVENT_ENTITY_CREATION);
32+
}
33+
2834
protected function getCreationIgnoredEntities(): array {
2935
return [
3036
'PresentationAction',
@@ -39,6 +45,10 @@ public function format($subject, $change_set): ?string {
3945
$class_name = (new ReflectionClass($subject))->getShortName();
4046
$ignored_entities = $this->getCreationIgnoredEntities();
4147
if (in_array($class_name, $ignored_entities)) return null;
42-
return "{$class_name} created";
48+
49+
$entity_id = method_exists($subject, 'getId') ? $subject->getId() : 'N/A';
50+
$user_info = $this->getUserInfo();
51+
52+
return "{$class_name} (ID: {$entity_id}) created by {$user_info}";
4353
}
4454
}

app/Audit/ConcreteFormatters/EntityDeletionAuditLogFormatter.php

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

33
namespace App\Audit\ConcreteFormatters;
44

5+
use App\Audit\Interfaces\IAuditStrategy;
6+
57
/**
68
* Copyright 2022 OpenStack Foundation
79
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -31,8 +33,9 @@ class EntityDeletionAuditLogFormatter extends AbstractAuditLogFormatter
3133
*/
3234
private $child_entity_formatter;
3335

34-
public function __construct(?IChildEntityAuditLogFormatter $child_entity_formatter)
36+
public function __construct(?IChildEntityAuditLogFormatter $child_entity_formatter = null)
3537
{
38+
parent::__construct(IAuditStrategy::EVENT_ENTITY_DELETION);
3639
$this->child_entity_formatter = $child_entity_formatter;
3740
}
3841

@@ -56,6 +59,8 @@ public function format($subject, $change_set): ?string {
5659
->format($subject, IChildEntityAuditLogFormatter::CHILD_ENTITY_DELETION);
5760
}
5861

59-
return "{$class_name} deleted";
62+
$entity_id = method_exists($subject, 'getId') ? $subject->getId() : 'unknown';
63+
$user_info = $this->getUserInfo();
64+
return "{$class_name} (ID: {$entity_id}) deleted by {$user_info}";
6065
}
6166
}

app/Audit/ConcreteFormatters/EntityUpdateAuditLogFormatter.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php namespace App\Audit\ConcreteFormatters;
2+
3+
use App\Audit\Interfaces\IAuditStrategy;
24
/**
35
* Copyright 2022 OpenStack Foundation
46
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -37,8 +39,9 @@ class EntityUpdateAuditLogFormatter extends AbstractAuditLogFormatter
3739
*/
3840
private $child_entity_formatter;
3941

40-
public function __construct(?IChildEntityAuditLogFormatter $child_entity_formatter)
42+
public function __construct(?IChildEntityAuditLogFormatter $child_entity_formatter = null)
4143
{
44+
parent::__construct(IAuditStrategy::EVENT_ENTITY_UPDATE);
4245
$this->child_entity_formatter = $child_entity_formatter;
4346
}
4447

@@ -157,6 +160,10 @@ public function format($subject, $change_set): ?string
157160

158161
if (count($res) == 0) return null;
159162

160-
return join("|", $res);
163+
$entity_id = method_exists($subject, 'getId') ? $subject->getId() : 'N/A';
164+
$user_info = $this->getUserInfo();
165+
$message = join("|", $res);
166+
167+
return "{$class_name} (ID: {$entity_id}) updated by {$user_info}: {$message}";
161168
}
162169
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace App\Audit\ConcreteFormatters;
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+
use App\Audit\AbstractAuditLogFormatter;
19+
use App\Audit\Interfaces\IAuditStrategy;
20+
use models\summit\PresentationCategory;
21+
use Illuminate\Support\Facades\Log;
22+
23+
class PresentationCategoryAuditLogFormatter extends AbstractAuditLogFormatter
24+
{
25+
public function format($subject, array $change_set): ?string
26+
{
27+
if (!$subject instanceof PresentationCategory) {
28+
return null;
29+
}
30+
31+
try {
32+
$title = $subject->getTitle() ?? 'Unknown Category';
33+
$code = $subject->getCode() ?? 'N/A';
34+
$id = $subject->getId() ?? 'unknown';
35+
$summit = $subject->getSummit();
36+
$summit_name = $summit ? ($summit->getName() ?? 'Unknown Summit') : 'Unknown Summit';
37+
38+
switch ($this->event_type) {
39+
case IAuditStrategy::EVENT_ENTITY_CREATION:
40+
return sprintf(
41+
"Presentation Category '%s' (%s) (%d) created for Summit '%s' by user %s",
42+
$title,
43+
$code,
44+
$id,
45+
$summit_name,
46+
$this->getUserInfo()
47+
);
48+
49+
case IAuditStrategy::EVENT_ENTITY_UPDATE:
50+
$change_details = $this->buildChangeDetails($change_set);
51+
return sprintf(
52+
"Presentation Category '%s' (%s) (%d) for Summit '%s' updated: %s by user %s",
53+
$title,
54+
$code,
55+
$id,
56+
$summit_name,
57+
$change_details,
58+
$this->getUserInfo()
59+
);
60+
61+
case IAuditStrategy::EVENT_ENTITY_DELETION:
62+
return sprintf(
63+
"Presentation Category '%s' (%s) (%d) for Summit '%s' was deleted by user %s",
64+
$title,
65+
$code,
66+
$id,
67+
$summit_name,
68+
$this->getUserInfo()
69+
);
70+
}
71+
} catch (\Exception $ex) {
72+
Log::warning("PresentationCategoryAuditLogFormatter error: " . $ex->getMessage());
73+
}
74+
75+
return null;
76+
}
77+
}

0 commit comments

Comments
 (0)