From 18887bf7f9c06d968da8938a37df283fec74e6ed Mon Sep 17 00:00:00 2001 From: Eric Espie Date: Fri, 5 Jan 2024 11:21:59 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B07118=20-=20Act=20on=20guaranteed=20objec?= =?UTF-8?q?t=20deletion,=20with=20the=20object=20still=20accessible=20(EVE?= =?UTF-8?q?NT=5FDB=5FABOUT=5FTO=5FDELETE)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/cmdbabstract.class.inc.php | 10 ++++++++++ application/datamodel.application.xml | 17 +++++++++++++++++ core/dbobject.class.php | 10 ++++++++++ .../unitary-tests/core/CRUDEventTest.php | 7 +++++-- 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/application/cmdbabstract.class.inc.php b/application/cmdbabstract.class.inc.php index fb205851e..288b6a94a 100644 --- a/application/cmdbabstract.class.inc.php +++ b/application/cmdbabstract.class.inc.php @@ -5951,6 +5951,16 @@ JS $this->FireEvent(EVENT_DB_CHECK_TO_DELETE, ['deletion_plan' => $oDeletionPlan]); } + /** + * @return void + * @throws \CoreException + * @since 3.1.2 + */ + final protected function FireEventAboutToDelete(): void + { + $this->FireEvent(EVENT_DB_ABOUT_TO_DELETE); + } + /** * @return void * @throws \CoreException diff --git a/application/datamodel.application.xml b/application/datamodel.application.xml index 765e3563a..2e198d7fc 100644 --- a/application/datamodel.application.xml +++ b/application/datamodel.application.xml @@ -270,6 +270,23 @@ + + An object is about to be deleted from the database + + cmdbAbstractObject + + cmdbAbstractObject::OnDelete + + + The object about to be deleted + DBObject + + + Debug string + string + + + An object has been deleted into the database diff --git a/core/dbobject.class.php b/core/dbobject.class.php index 38da35356..3c9042048 100644 --- a/core/dbobject.class.php +++ b/core/dbobject.class.php @@ -4106,6 +4106,8 @@ abstract class DBObject implements iDisplay return; } + $this->SetReadOnly("No modification allowed before delete"); + $this->FireEventAboutToDelete(); $oKPI = new ExecutionKPI(); $this->OnDelete(); $oKPI->ComputeStatsForExtension($this, 'OnDelete'); @@ -6625,6 +6627,14 @@ abstract class DBObject implements iDisplay { } + /** + * @return void + * @since 3.1.2 + */ + protected function FireEventAboutToDelete(): void + { + } + /** * @return void * @since 3.1.0 diff --git a/tests/php-unit-tests/unitary-tests/core/CRUDEventTest.php b/tests/php-unit-tests/unitary-tests/core/CRUDEventTest.php index 024cfcc1b..67ba6e8f1 100644 --- a/tests/php-unit-tests/unitary-tests/core/CRUDEventTest.php +++ b/tests/php-unit-tests/unitary-tests/core/CRUDEventTest.php @@ -24,6 +24,7 @@ use Server; use Team; use UserRequest; use utils; +use const EVENT_DB_ABOUT_TO_DELETE; use const EVENT_DB_AFTER_DELETE; use const EVENT_DB_AFTER_WRITE; use const EVENT_DB_BEFORE_WRITE; @@ -422,9 +423,10 @@ class CRUDEventTest extends ItopDataTestCase // 1 delete for UserRequest, 3 delete for lnkFunctionalCIToTicket $this->assertEquals(4, self::$aEventCalls[EVENT_DB_CHECK_TO_DELETE]); + $this->assertEquals(4, self::$aEventCalls[EVENT_DB_ABOUT_TO_DELETE]); $this->assertEquals(4, self::$aEventCalls[EVENT_DB_AFTER_DELETE]); - $this->assertArrayNotHasKey(EVENT_DB_LINKS_CHANGED, self::$aEventCalls, 'no relation with the with_php_compute attribute !'); - $this->assertEquals(8, self::$iEventCalls); + $this->assertArrayNotHasKey(EVENT_DB_LINKS_CHANGED, self::$aEventCalls, 'Event not to be sent on delete'); + $this->assertEquals(12, self::$iEventCalls); } @@ -725,6 +727,7 @@ class CRUDEventReceiver extends ClassesWithDebug $this->oTestCase->EventService_RegisterListener(EVENT_DB_CHECK_TO_DELETE, [$this, 'OnEvent']); $this->oTestCase->EventService_RegisterListener(EVENT_DB_BEFORE_WRITE, [$this, 'OnEvent']); $this->oTestCase->EventService_RegisterListener(EVENT_DB_AFTER_WRITE, [$this, 'OnEvent']); + $this->oTestCase->EventService_RegisterListener(EVENT_DB_ABOUT_TO_DELETE, [$this, 'OnEvent']); $this->oTestCase->EventService_RegisterListener(EVENT_DB_AFTER_DELETE, [$this, 'OnEvent']); $this->oTestCase->EventService_RegisterListener(EVENT_DB_LINKS_CHANGED, [$this, 'OnEvent']);