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
85 changes: 85 additions & 0 deletions app/Audit/AbstractAuditLogFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Audit;

use App\Audit\Utils\DateFormatter;

/**
* Copyright 2025 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -18,6 +20,12 @@
abstract class AbstractAuditLogFormatter implements IAuditLogFormatter
{
protected AuditContext $ctx;
protected string $event_type;

public function __construct(string $event_type)
{
$this->event_type = $event_type;
}

final public function setContext(AuditContext $ctx): void
{
Expand All @@ -41,5 +49,82 @@ protected function getUserInfo(): string
return sprintf("%s (%s)", $user_name, $user_id);
}

protected function formatAuditDate($date, string $format = 'Y-m-d H:i:s'): string
{
return DateFormatter::format($date, $format);
}

protected function getIgnoredFields(): array
{
return [
'last_created',
'last_updated',
'last_edited',
'created_by',
'updated_by'
];
}

protected function formatChangeValue($value): string
{
if (is_bool($value)) {
return $value ? 'true' : 'false';
}
if (is_null($value)) {
return 'null';
}
if ($value instanceof \DateTimeInterface) {
return $value->format('Y-m-d H:i:s');
}
if ($value instanceof \Doctrine\Common\Collections\Collection) {
$count = $value->count();
return sprintf('Collection[%d items]', $count);
}
if (is_object($value)) {
$className = get_class($value);
return sprintf('%s', $className);
}
if (is_array($value)) {
return sprintf('Array[%d items]', count($value));
}
return (string) $value;
}


protected function buildChangeDetails(array $change_set): string
{
$changed_fields = [];
$ignored_fields = $this->getIgnoredFields();

foreach ($change_set as $prop_name => $change_values) {
if (in_array($prop_name, $ignored_fields)) {
continue;
}

$old_value = $change_values[0] ?? null;
$new_value = $change_values[1] ?? null;

$formatted_change = $this->formatFieldChange($prop_name, $old_value, $new_value);
if ($formatted_change !== null) {
$changed_fields[] = $formatted_change;
}
}

if (empty($changed_fields)) {
return 'properties without changes registered';
}

$fields_summary = count($changed_fields) . ' field(s) modified: ';
return $fields_summary . implode(' | ', $changed_fields);
Comment on lines +117 to +118
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

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

The variable name 'fields_summary' is misleading because it contains both a count and a prefix string, not just a summary. Consider renaming to 'fields_prefix' or directly inline the concatenation to improve clarity.

Suggested change
$fields_summary = count($changed_fields) . ' field(s) modified: ';
return $fields_summary . implode(' | ', $changed_fields);
return sprintf('%d field(s) modified: %s', count($changed_fields), implode(' | ', $changed_fields));

Copilot uses AI. Check for mistakes.
}

protected function formatFieldChange(string $prop_name, $old_value, $new_value): ?string
{
$old_display = $this->formatChangeValue($old_value);
$new_display = $this->formatChangeValue($new_value);

return sprintf("Property \"%s\" has changed from \"%s\" to \"%s\"", $prop_name, $old_display, $new_display);
}

abstract public function format($subject, array $change_set): ?string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Audit\ConcreteFormatters\ChildEntityFormatters;

use models\summit\SummitAttendeeBadgePrint;
use App\Audit\Utils\DateFormatter;

/**
* Copyright 2023 OpenStack Foundation
Expand All @@ -29,7 +30,7 @@ class SummitAttendeeBadgePrintAuditLogFormatter implements IChildEntityAuditLogF
public function format($subject, string $child_entity_action_type, ?string $additional_info = ""): ?string {
if ($child_entity_action_type == IChildEntityAuditLogFormatter::CHILD_ENTITY_DELETION &&
$subject instanceof SummitAttendeeBadgePrint) {
$print_date = $subject->getPrintDate()->format('Y-m-d H:i:s');
$print_date = DateFormatter::format($subject->getPrintDate());
$view_type_name = 'N/A';
$view_type = $subject->getViewType();
if (!is_null($view_type)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(?IChildEntityAuditLogFormatter $child_entity_formatt
$this->child_entity_formatter = $child_entity_formatter;
}

protected function getIgnoredFields()
protected function getIgnoredFields(): array
{
return [
'last_created',
Expand Down Expand Up @@ -140,8 +140,8 @@ public function format($subject, $change_set): ?string
}

if ($old_value instanceof DateTime || $new_value instanceof DateTime) {
$old_value = $old_value != null ? $old_value->format('Y-m-d H:i:s') : "";
$new_value = $new_value != null ? $new_value->format('Y-m-d H:i:s') : "";
$old_value = $old_value != null ? $this->formatAuditDate($old_value) : "";
$new_value = $new_value != null ? $this->formatAuditDate($new_value) : "";
} else if (is_bool($old_value) || is_bool($new_value)) {
$old_value = $old_value ? 'true' : 'false';
$new_value = $new_value ? 'true' : 'false';
Expand Down
24 changes: 3 additions & 21 deletions app/Audit/ConcreteFormatters/FeaturedSpeakerAuditLogFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@

class FeaturedSpeakerAuditLogFormatter extends AbstractAuditLogFormatter
{
private string $event_type;

public function __construct(string $event_type)
{
$this->event_type = $event_type;
}

public function format($subject, array $change_set): ?string
{
if (!$subject instanceof FeaturedSpeaker) {
Expand Down Expand Up @@ -59,23 +52,12 @@ public function format($subject, array $change_set): ?string
);

case IAuditStrategy::EVENT_ENTITY_UPDATE:
$changed_fields = [];

if (isset($change_set['Order'])) {
$old_order = $change_set['Order'][0] ?? 'unknown';
$new_order = $change_set['Order'][1] ?? 'unknown';
$changed_fields[] = sprintf("display_order %s → %s", $old_order, $new_order);
}
if (isset($change_set['PresentationSpeakerID'])) {
$changed_fields[] = "speaker";
}

$fields_str = !empty($changed_fields) ? implode(', ', $changed_fields) : 'properties';
$change_details = $this->buildChangeDetails($change_set);
return sprintf(
"Featured speaker '%s' (%s) updated (%s changed) by user %s",
"Featured speaker '%s' (%s) updated: %s by user %s",
$speaker_name,
$speaker_id,
$fields_str,
$change_details,
$this->getUserInfo()
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace App\Audit\ConcreteFormatters\PresentationFormatters;

/**
* 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\AbstractAuditLogFormatter;
use App\Audit\Interfaces\IAuditStrategy;
use models\summit\PresentationActionType;
use Illuminate\Support\Facades\Log;

class PresentationActionTypeAuditLogFormatter extends AbstractAuditLogFormatter
{
public function format($subject, array $change_set): ?string
{
if (!$subject instanceof PresentationActionType) {
return null;
}

try {
$label = $subject->getLabel() ?? 'Unknown Action Type';
$id = $subject->getId() ?? 'unknown';

$summit = $subject->getSummit();
$summit_name = $summit ? ($summit->getName() ?? 'Unknown Summit') : 'Unknown Summit';

switch ($this->event_type) {
case IAuditStrategy::EVENT_ENTITY_CREATION:
return sprintf(
"Presentation Action Type '%s' (%d) created for Summit '%s' by user %s",
$label,
$id,
$summit_name,
$this->getUserInfo()
);

case IAuditStrategy::EVENT_ENTITY_UPDATE:
$change_details = $this->buildChangeDetails($change_set);
return sprintf(
"Presentation Action Type '%s' (%d) for Summit '%s' updated: %s by user %s",
$label,
$id,
$summit_name,
$change_details,
$this->getUserInfo()
);

case IAuditStrategy::EVENT_ENTITY_DELETION:
return sprintf(
"Presentation Action Type '%s' (%d) for Summit '%s' was deleted by user %s",
$label,
$id,
$summit_name,
$this->getUserInfo()
);
}
} catch (\Exception $ex) {
Log::warning("PresentationActionTypeAuditLogFormatter error: " . $ex->getMessage());
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace App\Audit\ConcreteFormatters\PresentationFormatters;

/**
* 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\AbstractAuditLogFormatter;
use App\Audit\Interfaces\IAuditStrategy;
use models\summit\PresentationLink;
use Illuminate\Support\Facades\Log;

class PresentationLinkAuditLogFormatter extends AbstractAuditLogFormatter
{
public function format($subject, array $change_set): ?string
{
if (!$subject instanceof PresentationLink) {
return null;
}

try {
$title = $subject->getName() ?? 'Unknown Link';
$id = $subject->getId() ?? 'unknown';
$class_name = $subject->getClassName();

$presentation = $subject->getPresentation();
$presentation_title = $presentation ? ($presentation->getTitle() ?? 'Unknown Presentation') : 'Unknown Presentation';

switch ($this->event_type) {
case IAuditStrategy::EVENT_ENTITY_CREATION:
return sprintf(
"Presentation Link '%s' (%d) of type %s created for presentation '%s' by user %s",
$title,
$id,
$class_name,
$presentation_title,
$this->getUserInfo()
);

case IAuditStrategy::EVENT_ENTITY_UPDATE:
$change_details = $this->buildChangeDetails($change_set);
return sprintf(
"Presentation Link '%s' (%d) for presentation '%s' updated: %s by user %s",
$title,
$id,
$presentation_title,
$change_details,
$this->getUserInfo()
);

case IAuditStrategy::EVENT_ENTITY_DELETION:
return sprintf(
"Presentation Link '%s' (%d) of type %s for presentation '%s' was deleted by user %s",
$title,
$id,
$class_name,
$presentation_title,
$this->getUserInfo()
);
}
} catch (\Exception $ex) {
Log::warning("PresentationLinkAuditLogFormatter error: " . $ex->getMessage());
}

return null;
}
}
Loading