From b2bcc639e500e3eacaa68fbe5f472ba519cf61fc Mon Sep 17 00:00:00 2001 From: Mathieu Ducrot Date: Wed, 28 Aug 2024 11:29:36 +0200 Subject: [PATCH 1/4] Add HistorizableInterface::getHistoryDiffFieldsToSkip to manage fields to skip logging on history --- src/Entity/Log/HistorizableInterface.php | 9 +++++++++ src/Entity/Log/HistorizableTrait.php | 5 +++++ src/EventListener/HistoryDoctrineListener.php | 9 ++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Entity/Log/HistorizableInterface.php b/src/Entity/Log/HistorizableInterface.php index bd64b8f..fe25daf 100644 --- a/src/Entity/Log/HistorizableInterface.php +++ b/src/Entity/Log/HistorizableInterface.php @@ -19,4 +19,13 @@ public function addHistory(array $history): self; * Allows you to activate or not the history on prePersist/preUpdate doctrine events for diffs */ public function isDoctrineListenerEnable(): bool; + + /** + * @return array Indexed array which contains field to skip storing on the history JSON by the HistoryDoctrineListener when doing update + * Example : + * [ + * "progressionData" => true, + * ] + */ + public function getHistoryDiffFieldsToSkip(): array; } diff --git a/src/Entity/Log/HistorizableTrait.php b/src/Entity/Log/HistorizableTrait.php index e69f0ec..3ca1c60 100644 --- a/src/Entity/Log/HistorizableTrait.php +++ b/src/Entity/Log/HistorizableTrait.php @@ -42,4 +42,9 @@ public function isDoctrineListenerEnable(): bool { return true; } + + public function getHistoryDiffFieldsToSkip(): array + { + return []; + } } diff --git a/src/EventListener/HistoryDoctrineListener.php b/src/EventListener/HistoryDoctrineListener.php index 7ef125e..fec35da 100644 --- a/src/EventListener/HistoryDoctrineListener.php +++ b/src/EventListener/HistoryDoctrineListener.php @@ -53,8 +53,15 @@ private function handleHistory(LifecycleEventArgs $args, string $code): void $statusDiff = null; $isHistorizableStatus = $entity instanceof HistorizableStatusInterface; + $diffFieldsToSkip = $entity->getHistoryDiffFieldsToSkip(); foreach ($entityData as $field => $change) { - if ($field === 'history' || $field === 'updatedAt' || $field === 'updatedAtMonth' || $field === 'updatedAtYear') { + if ( + $field === 'history' + || $field === 'updatedAt' + || $field === 'updatedAtMonth' + || $field === 'updatedAtYear' + || isset($diffFieldsToSkip[$field]) + ) { unset($entityData[$field]); continue; } From 89766246ebcb070f85c3af27f165e4fc513e0cb2 Mon Sep 17 00:00:00 2001 From: Mathieu Ducrot Date: Wed, 28 Aug 2024 11:30:38 +0200 Subject: [PATCH 2/4] Add .sso subdomain for RequestUtils::getContextFromHost for proper detection on localhost --- src/Utils/RequestUtils.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Utils/RequestUtils.php b/src/Utils/RequestUtils.php index a962a0a..7426cdc 100644 --- a/src/Utils/RequestUtils.php +++ b/src/Utils/RequestUtils.php @@ -16,11 +16,12 @@ public static function getContextFromHost(string $host, ?string $domain = null): // MDT fallback in case there is no subdomain and the host is different from the domain $toReturn = 'app'; if ( - str_starts_with($host, 'app.') || - str_starts_with($host, 'admin.') || - str_starts_with($host, 'api.') || - str_starts_with($host, 'extranet.') || - substr_count($host, '.') > 1 + str_starts_with($host, 'app.') + || str_starts_with($host, 'admin.') + || str_starts_with($host, 'api.') + || str_starts_with($host, 'extranet.') + || str_starts_with($host, 'sso.') + || substr_count($host, '.') > 1 ) { $toReturn = substr($host, 0, (int) strpos($host, '.')); } From 4d9c112a18d883a1cffa5278583b26388531e49c Mon Sep 17 00:00:00 2001 From: Mathieu Ducrot Date: Wed, 28 Aug 2024 11:32:43 +0200 Subject: [PATCH 3/4] Changed HistoryLogger::log to skip history log if it is an update without data (ex: diff detected in EntityChangeSet but the targetted fields are actually skip from getHistoryDiffFieldsToSkip) --- src/Logger/HistoryLogger.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Logger/HistoryLogger.php b/src/Logger/HistoryLogger.php index e70063b..70532f3 100644 --- a/src/Logger/HistoryLogger.php +++ b/src/Logger/HistoryLogger.php @@ -118,6 +118,16 @@ public function log(HistorizableInterface $entity, ?string $code = null, array $ } } + // If the history log is an update but without data we don't log it to the database. + if ( + $code === self::UPDATED_CODE + && !isset($history[self::DIFF_PROPERTY]) + && $this->title === null + && $this->comment === null + && $this->description === null + ) { + return; + } $entity->addHistory($history); if ($this->flushLog) { From e1ba8be6738164e2a374c8cb64da23ccc47699c8 Mon Sep 17 00:00:00 2001 From: Mathieu Ducrot Date: Wed, 28 Aug 2024 11:52:59 +0200 Subject: [PATCH 4/4] Update CHANGELOG for release v1.10.0 - (2024-08-28) --- CHANGELOG.md | 13 +++++++++++++ CHANGELOG_add_duplicate_value_fill_prefix.md | 3 --- CHANGELOG_add_nelmio_config.md | 3 --- CHANGELOG_add_open_days.md | 2 -- 4 files changed, 13 insertions(+), 8 deletions(-) delete mode 100644 CHANGELOG_add_duplicate_value_fill_prefix.md delete mode 100644 CHANGELOG_add_nelmio_config.md delete mode 100644 CHANGELOG_add_open_days.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 08b68f6..81bd0ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,18 @@ CHANGELOG for 1.x =================== +## v1.10.0 - (2024-08-28) +### Added +- `ArrayUtils::hasDuplicateValue` + tests (@lfortunier) +- `StringUtils::fillPrefix` Fill a prefix to value until specified length + tests (@lfortunier) +- `DateUtils::addWorkingDays` to calculate a date based on working days + tests (@lfortunier) +- `HistorizableInterface::getHistoryDiffFieldsToSkip` to manage fields to skip logging on history (@mathieu-ducrot) + +### Changed +- `README.md` update : add scripts to the list of scripts that can be executed in `script-src` Nelmio Security recommendations config. (@lfortunier) +- `README.md` update : Add missing sentry settings configuration (@lfortunier) +- `RequestUtils::getContextFromHost` Add .sso subdomain for proper detection on localhost (@mathieu-ducrot) +- `HistoryLogger::log` skip history log if it's an update without data (ex: diff detected in EntityChangeSet but the targetted fields are actually skip from getHistoryDiffFieldsToSkip @mathieu-ducrot) + ## v1.9.0 - (2024-07-11) ### Added - `FileableInterface`, `ImageableInterface`, `PdfInterface` and their trait to demonstrate how to properly configure VichUploader annotations/attributes diff --git a/CHANGELOG_add_duplicate_value_fill_prefix.md b/CHANGELOG_add_duplicate_value_fill_prefix.md deleted file mode 100644 index 7387d52..0000000 --- a/CHANGELOG_add_duplicate_value_fill_prefix.md +++ /dev/null @@ -1,3 +0,0 @@ -### Added -- `MonitoringApiControllerTrait` used to centralize ApiCall manipulation during api monitoring -- `ApiCallMonitor::logException` simplify logging exception for `ApiCall` using the `ProcessMonitor` diff --git a/CHANGELOG_add_nelmio_config.md b/CHANGELOG_add_nelmio_config.md deleted file mode 100644 index 1d5a821..0000000 --- a/CHANGELOG_add_nelmio_config.md +++ /dev/null @@ -1,3 +0,0 @@ -### Added -- Adding scripts to the list of scripts that can be executed in `script-src` Nelmio Security recommendations config. -- Add missing sentry settings to readme Sentry configuration part diff --git a/CHANGELOG_add_open_days.md b/CHANGELOG_add_open_days.md deleted file mode 100644 index bc2b728..0000000 --- a/CHANGELOG_add_open_days.md +++ /dev/null @@ -1,2 +0,0 @@ -### Added -- `DateUtils::addWorkingDays` to calculate a date based on working days